MCPcopy Create free account
hub / github.com/dengsgo/math-engine / parseFunCallerOrConst

Method parseFunCallerOrConst

engine/ast.go:126–180  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

124}
125
126func (a *AST) parseFunCallerOrConst() ExprAST {
127 name := a.currTok.Tok
128 a.getNextToken()
129 // call func
130 if a.currTok.Tok == "(" {
131 f := FunCallerExprAST{}
132 if _, ok := defFunc[name]; !ok {
133 a.Err = errors.New(
134 fmt.Sprintf("function `%s` is undefined\n%s",
135 name,
136 ErrPos(a.source, a.currTok.Offset)))
137 return f
138 }
139 a.getNextToken()
140 exprs := make([]ExprAST, 0)
141 if a.currTok.Tok == ")" {
142 // function call without parameters
143 // ignore the process of parameter resolution
144 } else {
145 exprs = append(exprs, a.ParseExpression())
146 for a.currTok.Tok != ")" && a.getNextToken() != nil {
147 if a.currTok.Type == COMMA {
148 continue
149 }
150 exprs = append(exprs, a.ParseExpression())
151 }
152 }
153 def := defFunc[name]
154 if def.argc >= 0 && len(exprs) != def.argc {
155 a.Err = errors.New(
156 fmt.Sprintf("wrong way calling function `%s`, parameters want %d but get %d\n%s",
157 name,
158 def.argc,
159 len(exprs),
160 ErrPos(a.source, a.currTok.Offset)))
161 }
162 a.getNextToken()
163 f.Name = name
164 f.Arg = exprs
165 return f
166 }
167 // call const
168 if v, ok := defConst[name]; ok {
169 return NumberExprAST{
170 Val: v,
171 Str: strconv.FormatFloat(v, 'f', 0, 64),
172 }
173 } else {
174 a.Err = errors.New(
175 fmt.Sprintf("const `%s` is undefined\n%s",
176 name,
177 ErrPos(a.source, a.currTok.Offset)))
178 return NumberExprAST{}
179 }
180}
181
182func (a *AST) parsePrimary() ExprAST {
183 switch a.currTok.Type {

Callers 1

parsePrimaryMethod · 0.95

Calls 3

getNextTokenMethod · 0.95
ParseExpressionMethod · 0.95
ErrPosFunction · 0.85

Tested by

no test coverage detected