Parse syntax parsing. Program -> IntDeclaration | ExpressionStatement | AssignmentStatement IntDeclaration -> 'int' Id ( = Additive) '\n' ExpressionStatement -> Additive '\n' Additive -> Multiplicative ( (+ | -) Multiplicative)* Multiplicative -> Primary ((* | /) Primary)* Primary -> IntLiteral | Id
(script string)
| 33 | // Primary -> IntLiteral | Id | Additive |
| 34 | // AssignmentStatement -> Identifier = Additive |
| 35 | func Parse(script string) (*ASTNode, error) { |
| 36 | tokenTypes := token.Tokenize(script) |
| 37 | reader := NewTokenReader(tokenTypes) |
| 38 | |
| 39 | root := NewASTNode(Program, App) |
| 40 | var err error |
| 41 | for reader.Peek() != nil { |
| 42 | child, _ := IntDeclare(reader) |
| 43 | |
| 44 | if child == nil { |
| 45 | child, err = ExpressionStatement(reader) |
| 46 | } |
| 47 | |
| 48 | if child == nil { |
| 49 | child, err = AssignmentStatement(reader) |
| 50 | } |
| 51 | |
| 52 | if child != nil { |
| 53 | root.AddChild(child) |
| 54 | } else { |
| 55 | return nil, errors.New("syntax err: Invalid statement") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | //cal.PrintASTNode(root, "") |
| 60 | |
| 61 | return root, err |
| 62 | } |
| 63 | |
| 64 | // ExpressionStatement -> 1+2*3 '\n' |
| 65 | func ExpressionStatement(reader *TokenReader) (*ASTNode, error) { |
nothing calls this directly
no test coverage detected