|
| 1 | +package evaluator |
| 2 | + |
| 3 | +import ( |
| 4 | + "monkeygo/ast" |
| 5 | + "monkeygo/lexer" |
| 6 | + "monkeygo/object" |
| 7 | + "monkeygo/parser" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDefineMacros(t *testing.T) { |
| 12 | + |
| 13 | + input := ` |
| 14 | + let number = 1; |
| 15 | + let function = fn(x, y) { x + y }; |
| 16 | + let mymacro = macro(x, y) { x + y; }; |
| 17 | + ` |
| 18 | + |
| 19 | + env := object.NewEnvironment() |
| 20 | + program := testParseProgram(input) |
| 21 | + |
| 22 | + DefineMacros(program, env) |
| 23 | + |
| 24 | + if len(program.Statements) != 2 { |
| 25 | + t.Fatalf("Wrong number of statements. got=%d", |
| 26 | + len(program.Statements)) |
| 27 | + } |
| 28 | + |
| 29 | + _, ok := env.Get("number") |
| 30 | + if ok { |
| 31 | + t.Fatalf("number should not be defined") |
| 32 | + } |
| 33 | + _, ok = env.Get("function") |
| 34 | + if ok { |
| 35 | + t.Fatalf("function should not be defined") |
| 36 | + } |
| 37 | + |
| 38 | + obj, ok := env.Get("mymacro") |
| 39 | + if !ok { |
| 40 | + t.Fatalf("macro not in environment.") |
| 41 | + } |
| 42 | + |
| 43 | + macro, ok := obj.(*object.Macro) |
| 44 | + if !ok { |
| 45 | + t.Fatalf("object is not Macro. got=%T (%+v)", obj, obj) |
| 46 | + } |
| 47 | + |
| 48 | + if len(macro.Parameters) != 2 { |
| 49 | + t.Fatalf("Wrong number of macro parameters. got=%d", |
| 50 | + len(macro.Parameters)) |
| 51 | + } |
| 52 | + |
| 53 | + if macro.Parameters[0].String() != "x" { |
| 54 | + t.Fatalf("parameter is not 'x'. got=%q", macro.Parameters[0]) |
| 55 | + } |
| 56 | + if macro.Parameters[1].String() != "y" { |
| 57 | + t.Fatalf("parameter is not 'y'. got=%q", macro.Parameters[1]) |
| 58 | + } |
| 59 | + |
| 60 | + expectedBody := "(x + y)" |
| 61 | + |
| 62 | + if macro.Body.String() != expectedBody { |
| 63 | + t.Fatalf("body is not %q. got=%q", expectedBody, macro.Body.String()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func testParseProgram(input string) *ast.Program { |
| 68 | + l := lexer.New(input) |
| 69 | + p := parser.New(l) |
| 70 | + return p.ParseProgram() |
| 71 | +} |
| 72 | + |
| 73 | +//展开宏 |
| 74 | +func TestExpandMacros(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + input string |
| 77 | + expected string |
| 78 | + }{ |
| 79 | + { |
| 80 | + ` |
| 81 | + let infixExpression = macro() { quote(1 + 2); }; |
| 82 | +
|
| 83 | + infixExpression(); |
| 84 | + `, |
| 85 | + `(1 + 2)`, |
| 86 | + }, |
| 87 | + { |
| 88 | + ` |
| 89 | + let reverse = macro(a, b) { quote(unquote(b) - unquote(a)); }; |
| 90 | +
|
| 91 | + reverse(2 + 2, 10 - 5); |
| 92 | + `, |
| 93 | + `(10 - 5) - (2 + 2)`, |
| 94 | + }, |
| 95 | + { |
| 96 | + ` |
| 97 | + let unless = macro(condition, consequence, alternative) { |
| 98 | + quote(if (!(unquote(condition))) { |
| 99 | + unquote(consequence); |
| 100 | + } else { |
| 101 | + unquote(alternative); |
| 102 | + }); |
| 103 | + }; |
| 104 | +
|
| 105 | + unless(10 > 5, puts("not greater"), puts("greater")); |
| 106 | + `, |
| 107 | + `if (!(10 > 5)) { puts("not greater") } else { puts("greater") }`, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + expected := testParseProgram(tt.expected) |
| 113 | + program := testParseProgram(tt.input) |
| 114 | + |
| 115 | + env := object.NewEnvironment() |
| 116 | + DefineMacros(program, env) |
| 117 | + expanded := ExpandMacros(program, env) |
| 118 | + |
| 119 | + if expanded.String() != expected.String() { |
| 120 | + t.Errorf("not equal. want=%q, got=%q", |
| 121 | + expected.String(), expanded.String()) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments