| 283 | } |
| 284 | |
| 285 | func (expr *term) Evaluate(ctx *ExecutionContext) (*Value, *Error) { |
| 286 | f1, err := expr.factor1.Evaluate(ctx) |
| 287 | if err != nil { |
| 288 | return nil, err |
| 289 | } |
| 290 | if expr.factor2 != nil { |
| 291 | f2, err := expr.factor2.Evaluate(ctx) |
| 292 | if err != nil { |
| 293 | return nil, err |
| 294 | } |
| 295 | switch expr.opToken.Val { |
| 296 | case "*": |
| 297 | if f1.IsFloat() || f2.IsFloat() { |
| 298 | // Result will be float |
| 299 | return AsValue(f1.Float() * f2.Float()), nil |
| 300 | } |
| 301 | // Result will be int |
| 302 | return AsValue(f1.Integer() * f2.Integer()), nil |
| 303 | case "/": |
| 304 | if f1.IsFloat() || f2.IsFloat() { |
| 305 | // Result will be float |
| 306 | return AsValue(f1.Float() / f2.Float()), nil |
| 307 | } |
| 308 | // Result will be int |
| 309 | return AsValue(f1.Integer() / f2.Integer()), nil |
| 310 | case "%": |
| 311 | // Result will be int |
| 312 | return AsValue(f1.Integer() % f2.Integer()), nil |
| 313 | default: |
| 314 | return nil, ctx.Error("unimplemented", expr.opToken) |
| 315 | } |
| 316 | } else { |
| 317 | return f1, nil |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func (expr *power) Evaluate(ctx *ExecutionContext) (*Value, *Error) { |
| 322 | p1, err := expr.power1.Evaluate(ctx) |