-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.go
166 lines (140 loc) · 3.99 KB
/
evaluator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package evaluator
import (
"fmt"
"github.com/hamzamogni/math_parser/ast"
"github.com/hamzamogni/math_parser/object"
)
func Eval(node ast.Node) object.Object {
switch node := node.(type) {
case *ast.Program:
return evalProgram(node)
case *ast.ExpressionStatement:
return Eval(node.Expression)
case *ast.NumberLiteral:
return &object.Number{Value: node.Value}
case *ast.PrefixExpression:
right := Eval(node.Right)
if isError(right) {
return right
}
return evalPrefixExpression(node.Operator, right)
case *ast.InfixExpression:
left := Eval(node.Left)
if isError(left) {
return left
}
right := Eval(node.Right)
if isError(right) {
return right
}
return evalInfixExpression(node.Operator, left, right)
case *ast.Identifier:
return evalIdentifier(node)
case *ast.CallExpression:
function := Eval(node.Function)
if isError(function) {
return function
}
args := evalExpressions(node.Arguments)
if len(args) == 1 && isError(args[0]) {
return args[0]
}
return applyFunction(function, args)
}
return nil
}
func newError(format string, a ...interface{}) *object.Error {
return &object.Error{Message: fmt.Sprintf(format, a...)}
}
func applyFunction(fn object.Object, args []object.Object) object.Object {
switch fn := fn.(type) {
case *object.Builtin:
return fn.Fn(args...)
default:
return nil
}
}
func isError(obj object.Object) bool {
if obj != nil {
return obj.Type() == object.ERROR_OBJ
}
return false
}
func evalExpressions(exps []ast.Expression) []object.Object {
var result []object.Object
for _, e := range exps {
evaluated := Eval(e)
result = append(result, evaluated)
}
return result
}
func evalProgram(program *ast.Program) object.Object {
var result object.Object
for _, statement := range program.Statements {
result = Eval(statement)
if result.Type() == object.ERROR_OBJ {
return result
}
}
return result
}
func evalPrefixExpression(operator string, right object.Object) object.Object {
switch operator {
case "-":
return evalMinusPrefixOperatorExpression(right)
case "+":
return evalPrefixPrefixOperatorExpression(right)
default:
return newError("unknown operator: %s%s", operator, right.Type())
}
}
func evalPrefixPrefixOperatorExpression(right object.Object) object.Object {
if right.Type() != object.NUMBER_OBJ {
return newError("unknown operator: +%s", right.Type())
}
value := right.(*object.Number).Value
return &object.Number{Value: value}
}
func evalMinusPrefixOperatorExpression(right object.Object) object.Object {
if right.Type() != object.NUMBER_OBJ {
return newError("unknown operator: -%s", right.Type())
}
value := right.(*object.Number).Value
return &object.Number{Value: -value}
}
func evalInfixExpression(operator string, left, right object.Object) object.Object {
if left.Type() != right.Type() {
return newError("type mismatch: %s %s %s", left.Type(), operator, right.Type())
}
if left.Type() == object.NUMBER_OBJ && right.Type() == object.NUMBER_OBJ {
return evalNumberInfixExpression(operator, left, right)
}
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
}
func evalNumberInfixExpression(operator string, left, right object.Object) object.Object {
leftVal := left.(*object.Number).Value
rightVal := right.(*object.Number).Value
switch operator {
case "+":
return &object.Number{Value: leftVal + rightVal}
case "-":
return &object.Number{Value: leftVal - rightVal}
case "*":
return &object.Number{Value: leftVal * rightVal}
case "**":
return &object.Number{Value: builtins["pow"].Fn(left, right).(*object.Number).Value}
case "/":
if right.Inspect() == "0.00" {
return newError("division by zero")
}
return &object.Number{Value: leftVal / rightVal}
default:
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
}
}
func evalIdentifier(node *ast.Identifier) object.Object {
if builtin, ok := builtins[node.Value]; ok {
return builtin
}
return newError("Unexpected identifier: " + node.Value)
}