MCPcopy Create free account
hub / github.com/crossoverJie/gscript / IntDeclare

Function IntDeclare

syntax/syntax.go:115–153  ·  view source on GitHub ↗

IntDeclare int a=10+1*2

(tokenReader *TokenReader)

Source from the content-addressed store, hash-verified

113
114// IntDeclare int a=10+1*2
115func 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)*
156func AdditiveLoop(tokenReader *TokenReader) (*ASTNode, error) {

Callers 10

TestCalculatorFunction · 0.85
TestCalculator2Function · 0.85
TestCalculator3Function · 0.85
TestCalculator4Function · 0.85
TestCalculator5Function · 0.85
TestCalculator6Function · 0.85
TestCalculator7Function · 0.85
TestCalculator8Function · 0.85
TestCalculator9Function · 0.85
ParseFunction · 0.85

Calls 7

AddChildMethod · 0.95
NewASTNodeFunction · 0.85
AdditiveLoopFunction · 0.85
TokenTypeMethod · 0.80
ReadMethod · 0.80
ValueMethod · 0.80
PeekMethod · 0.45

Tested by 9

TestCalculatorFunction · 0.68
TestCalculator2Function · 0.68
TestCalculator3Function · 0.68
TestCalculator4Function · 0.68
TestCalculator5Function · 0.68
TestCalculator6Function · 0.68
TestCalculator7Function · 0.68
TestCalculator8Function · 0.68
TestCalculator9Function · 0.68