diff --git a/src/compiler/compiler.go b/src/compiler/compiler.go index 9e46bb0..d0d59f7 100644 --- a/src/compiler/compiler.go +++ b/src/compiler/compiler.go @@ -4,38 +4,25 @@ import ( "fmt" "log" "os" - "github.com/jorenchik/mdemory/src/compiler/lexer" + "github.com/jorenchik/mdemory/src/compiler/parser" ) func main() { log.Println("Compilation started") - file, err := os.ReadFile("/home/jorenchik/Code/mdemory/src/compiler/input.mdem") + file, err := os.ReadFile( + "/home/jorenchik/Code/mdemory/src/compiler/input.mdem", + ) if (err != nil) { - log.Fatalf("Cannot open the input file: %s", err.Error()) + log.Fatalf( + "Cannot open the input file: %s", + err.Error(), + ) return } fileContents := string(file) - tokens, err := lexer.TokenizeMdem([]rune(fileContents)) - if (err != nil) { - fmt.Printf("%s\n", err.Error()) - return - } - if (true) { - log.Println("Lexer output:") - for _, el := range tokens { - fmt.Print(el.ToString()) - } - } - - automata = parserAutomata() - err = validateGrammar(tokens) - if (err != nil) { - log.Fatal(err.Error()) - } - - questions, err := ParseQuestions(tokens) + questions, err := parser.ParseQuestions(fileContents) if (err != nil) { log.Fatal(err.Error()) } diff --git a/src/compiler/parser.go b/src/compiler/parser/parser.go similarity index 92% rename from src/compiler/parser.go rename to src/compiler/parser/parser.go index 0ed4144..5364d8e 100644 --- a/src/compiler/parser.go +++ b/src/compiler/parser/parser.go @@ -1,4 +1,4 @@ -package main +package parser import ( "fmt" @@ -127,7 +127,8 @@ func parserAutomata() map[lexer.TokenType][]lexer.TokenType { return automata } -func validateGrammar(tokens []lexer.Token) error { +func ValidateGrammar(tokens []lexer.Token) error { + automata = parserAutomata() for i := 0; i < len(tokens) - 1; i++ { token := tokens[i] nextToken := tokens[i + 1] @@ -146,7 +147,23 @@ func validateGrammar(tokens []lexer.Token) error { return nil } -func ParseQuestions(tokens []lexer.Token) ([]Question, error) { +func ParseQuestions(fileContents string) ([]Question, error) { + tokens, err := lexer.TokenizeMdem([]rune(fileContents)) + if (err != nil) { + return nil, err + } + if (true) { + log.Println("Lexer output:") + for _, el := range tokens { + fmt.Print(el.ToString()) + } + } + + err = ValidateGrammar(tokens) + if (err != nil) { + log.Fatal(err.Error()) + } + questions := []Question{} section := "" i := 0