IntDeclare int a=10+1*2
(tokenReader *TokenReader)
| 113 | |
| 114 | // IntDeclare int a=10+1*2 |
| 115 | func IntDeclare(tokenReader *TokenReader) (*ASTNode, error) { |
| 116 | var node *ASTNode |
| 117 | tokenType := tokenReader.Peek() |
| 118 | if tokenType == nil || tokenType.TokenType() != token.Int { |
| 119 | return nil, errors.New("syntax err: invalid statement") |
| 120 | } |
| 121 | tokenType = tokenReader.Read() |
| 122 | |
| 123 | // parse identifier |
| 124 | tokenType = tokenReader.Peek() |
| 125 | if tokenType == nil { |
| 126 | return nil, errors.New("invalid statement, miss Identifier") |
| 127 | } |
| 128 | tokenType = tokenReader.Read() |
| 129 | // add into AST node |
| 130 | node = NewASTNode(IntDeclaration, tokenType.Value()) |
| 131 | |
| 132 | // parse Assignment= |
| 133 | tokenType = tokenReader.Peek() |
| 134 | if tokenType == nil || tokenType.TokenType() != token.Assignment { |
| 135 | return nil, errors.New("syntax err: invalid statement, miss Assignment") |
| 136 | } |
| 137 | tokenType = tokenReader.Read() |
| 138 | // parse Additive |
| 139 | child, err := AdditiveLoop(tokenReader) |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | node.AddChild(child) |
| 144 | |
| 145 | // parse end |
| 146 | tokenType = tokenReader.Peek() |
| 147 | if tokenType == nil || tokenType.TokenType() != token.Enter { |
| 148 | return nil, errors.New("syntax err: invalid statement, miss enter") |
| 149 | } |
| 150 | tokenType = tokenReader.Read() |
| 151 | |
| 152 | return node, nil |
| 153 | } |
| 154 | |
| 155 | // AdditiveLoop Additive -> Multiplicative ( (+ | -) Multiplicative)* |
| 156 | func AdditiveLoop(tokenReader *TokenReader) (*ASTNode, error) { |