From 09170e390db190f74f535bb467fea26fb5aba59f Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 12 Aug 2022 10:11:43 -0500 Subject: [PATCH] Created base for parser The parser now handles file opening and invokes the lexer. --- main.go | 18 ++---------------- parser/parser.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ parser/tree.go | 8 ++++++++ 3 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 parser/parser.go create mode 100644 parser/tree.go diff --git a/main.go b/main.go index e7676a3..1703906 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,9 @@ package arf -import "os" import "io" -import "path/filepath" -// import "github.com/sashakoshka/arf/lexer" +import "github.com/sashakoshka/arf/parser" func CompileModule (modulePath string, output io.Writer) (err error) { - moduleFiles, err := os.ReadDir(modulePath) - if err != nil { return err } - - // var moduleTokens []lexer.Token - for _, entry := range moduleFiles { - if filepath.Ext(entry.Name()) != ".arf" || entry.IsDir() { - continue - } - - // tokens, err := lexer.Tokenize() - // if err != nil { return err } - } - + _, err = parser.Parse(modulePath) return } diff --git a/parser/parser.go b/parser/parser.go new file mode 100644 index 0000000..3c877a0 --- /dev/null +++ b/parser/parser.go @@ -0,0 +1,49 @@ +package parser + +import "os" +import "path/filepath" +import "github.com/sashakoshka/arf/file" +import "github.com/sashakoshka/arf/lexer" + +// ParsingOperation holds information about an ongoing parsing operation. +type ParsingOperation struct { + modulePath string + token lexer.Token + tokens []lexer.Token + tokenIndex int +} + +// Parse reads the files located in the module specified by modulePath, and +// converts them into an abstract syntax tree. +func Parse (modulePath string) (tree *SyntaxTree, err error) { + parser := ParsingOperation { modulePath: modulePath } + tree, err = parser.parse() + return +} + +// parse runs the parsing operation. +func (parser *ParsingOperation) parse () (tree *SyntaxTree, err error) { + if parser.modulePath[len(parser.modulePath) - 1] != '/' { + parser.modulePath += "/" + } + + var moduleFiles []os.DirEntry + moduleFiles, err = os.ReadDir(parser.modulePath) + if err != nil { return } + + for _, entry := range moduleFiles { + if filepath.Ext(entry.Name()) != ".arf" || entry.IsDir() { + continue + } + + var sourceFile *file.File + sourceFile, err = file.Open(parser.modulePath + entry.Name()) + if err != nil { return } + + var tokens []lexer.Token + tokens, err = lexer.Tokenize(sourceFile) + if err != nil { return } + parser.tokens = append(parser.tokens, tokens...) + } + return +} diff --git a/parser/tree.go b/parser/tree.go new file mode 100644 index 0000000..1616abb --- /dev/null +++ b/parser/tree.go @@ -0,0 +1,8 @@ +package parser + +// SyntaxTree represents an abstract syntax tree. It covers an entire module. It +// can be expected to be syntactically correct, but it might not be semantically +// correct (because it has not been analyzed yet.) +type SyntaxTree struct { + +}