|
1 | 1 | package evaluator |
2 | 2 |
|
3 | | -import "strings" |
| 3 | +import ( |
| 4 | + "foxygo.at/evy/pkg/parser" |
| 5 | +) |
4 | 6 |
|
5 | 7 | func Run(input string, print func(string)) { |
6 | | - print(strings.ToUpper(input)) |
| 8 | + p := parser.New(input) |
| 9 | + prog := p.Parse() |
| 10 | + e := &Evaluator{print: print} |
| 11 | + e.builtins = newBuiltins(e) |
| 12 | + val := e.Eval(prog, NewScope()) |
| 13 | + if isError(val) { |
| 14 | + print(val.String()) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type Evaluator struct { |
| 19 | + print func(string) |
| 20 | + builtins map[string]Builtin |
| 21 | +} |
| 22 | + |
| 23 | +func (e *Evaluator) Eval(node parser.Node, scope *Scope) Value { |
| 24 | + switch node := node.(type) { |
| 25 | + case *parser.Program: |
| 26 | + return e.evalProgram(node, scope) |
| 27 | + case *parser.Declaration: |
| 28 | + return e.evalDeclaration(node, scope) |
| 29 | + case *parser.Var: |
| 30 | + v := e.evalVar(node, scope) |
| 31 | + return v |
| 32 | + case *parser.Term: |
| 33 | + return e.evalTerm(node, scope) |
| 34 | + case *parser.NumLiteral: |
| 35 | + return &Num{Val: node.Value} |
| 36 | + case *parser.StringLiteral: |
| 37 | + return &String{Val: node.Value} |
| 38 | + case *parser.Bool: |
| 39 | + return &Bool{Val: node.Value} |
| 40 | + case *parser.FunctionCall: |
| 41 | + return e.evalFunctionCall(node, scope) |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func (e *Evaluator) evalProgram(program *parser.Program, scope *Scope) Value { |
| 47 | + var result Value |
| 48 | + for _, statement := range program.Statements { |
| 49 | + result = e.Eval(statement, scope) |
| 50 | + if isError(result) { |
| 51 | + return result |
| 52 | + } |
| 53 | + } |
| 54 | + return result |
| 55 | +} |
| 56 | + |
| 57 | +func (e *Evaluator) evalDeclaration(decl *parser.Declaration, scope *Scope) Value { |
| 58 | + val := e.Eval(decl.Value, scope) |
| 59 | + if isError(val) { |
| 60 | + return val |
| 61 | + } |
| 62 | + scope.Set(decl.Var.Name, val) |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (e *Evaluator) evalFunctionCall(funcCall *parser.FunctionCall, scope *Scope) Value { |
| 67 | + args := e.evalTerms(funcCall.Arguments, scope) |
| 68 | + if len(args) == 1 && isError(args[0]) { |
| 69 | + return args[0] |
| 70 | + } |
| 71 | + builtin, ok := e.builtins[funcCall.Name] |
| 72 | + if !ok { |
| 73 | + return newError("cannot find builtin function " + funcCall.Name) |
| 74 | + } |
| 75 | + return builtin(args) |
| 76 | +} |
| 77 | + |
| 78 | +func (e *Evaluator) evalVar(v *parser.Var, scope *Scope) Value { |
| 79 | + if val, ok := scope.Get(v.Name); ok { |
| 80 | + return val |
| 81 | + } |
| 82 | + return newError("cannot find variable " + v.Name) |
| 83 | +} |
| 84 | + |
| 85 | +func (e *Evaluator) evalTerm(term parser.Node, scope *Scope) Value { |
| 86 | + return e.Eval(term, scope) |
| 87 | +} |
| 88 | + |
| 89 | +func (e *Evaluator) evalTerms(terms []parser.Node, scope *Scope) []Value { |
| 90 | + result := make([]Value, len(terms)) |
| 91 | + |
| 92 | + for i, t := range terms { |
| 93 | + evaluated := e.Eval(t, scope) |
| 94 | + if isError(evaluated) { |
| 95 | + return []Value{evaluated} |
| 96 | + } |
| 97 | + result[i] = evaluated |
| 98 | + } |
| 99 | + |
| 100 | + return result |
7 | 101 | } |
0 commit comments