(ctx *ExecutionContext)
| 225 | } |
| 226 | |
| 227 | func (expr *simpleExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) { |
| 228 | t1, err := expr.term1.Evaluate(ctx) |
| 229 | if err != nil { |
| 230 | return nil, err |
| 231 | } |
| 232 | result := t1 |
| 233 | |
| 234 | if expr.negate { |
| 235 | result = result.Negate() |
| 236 | } |
| 237 | |
| 238 | if expr.negativeSign { |
| 239 | if result.IsNumber() { |
| 240 | switch { |
| 241 | case result.IsFloat(): |
| 242 | result = AsValue(-1 * result.Float()) |
| 243 | case result.IsInteger(): |
| 244 | result = AsValue(-1 * result.Integer()) |
| 245 | default: |
| 246 | return nil, ctx.Error("Operation between a number and a non-(float/integer) is not possible", nil) |
| 247 | } |
| 248 | } else { |
| 249 | return nil, ctx.Error("Negative sign on a non-number expression", expr.GetPositionToken()) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if expr.term2 != nil { |
| 254 | t2, err := expr.term2.Evaluate(ctx) |
| 255 | if err != nil { |
| 256 | return nil, err |
| 257 | } |
| 258 | switch expr.opToken.Val { |
| 259 | case "+": |
| 260 | if result.IsString() || t2.IsString() { |
| 261 | // Result will be a string |
| 262 | return AsValue(result.String() + t2.String()), nil |
| 263 | } |
| 264 | if result.IsFloat() || t2.IsFloat() { |
| 265 | // Result will be a float |
| 266 | return AsValue(result.Float() + t2.Float()), nil |
| 267 | } |
| 268 | // Result will be an integer |
| 269 | return AsValue(result.Integer() + t2.Integer()), nil |
| 270 | case "-": |
| 271 | if result.IsFloat() || t2.IsFloat() { |
| 272 | // Result will be a float |
| 273 | return AsValue(result.Float() - t2.Float()), nil |
| 274 | } |
| 275 | // Result will be an integer |
| 276 | return AsValue(result.Integer() - t2.Integer()), nil |
| 277 | default: |
| 278 | return nil, ctx.Error("Unimplemented", expr.GetPositionToken()) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return result, nil |
| 283 | } |
| 284 |
no test coverage detected