aboutsummaryrefslogtreecommitdiff
path: root/miniMAL/step3_env.json
blob: e72de0767d7527c8f5606b43ef7c6f4cced81884 (plain)
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
["do",

["load-file", ["`", "miniMAL-core.json"]],
["load-file", ["`", "types.json"]],
["load-file", ["`", "reader.json"]],
["load-file", ["`", "printer.json"]],
["load-file", ["`", "env.json"]],

["def", "READ", ["fn", ["strng"],
  ["read-str", "strng"]]],

["def", "eval-ast", ["fn", ["ast", "env"],
  ["if", ["symbol?", "ast"],
    ["env-get", "env", "ast"],
  ["if", ["list?", "ast"],
    ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"],
  ["if", ["vector?", "ast"],
    ["vectorl", ["map", ["fn", ["x"], ["EVAL", "x", "env"]], "ast"]],
  ["if", ["map?", "ast"],
    ["let", ["new-hm", ["hash-map"]],
      ["do",
        ["map", ["fn", ["k"], ["set", "new-hm",
                                ["EVAL", "k", "env"],
                                ["EVAL", ["get", "ast", "k"], "env"]]],
                ["keys", "ast"]],
        "new-hm"]],
  "ast"]]]]]],

["def", "LET", ["fn", ["env", "args"],
  ["if", [">", ["count", "args"], 0],
    ["do",
      ["env-set", "env", ["nth", "args", 0],
                         ["EVAL", ["nth", "args", 1], "env"]],
      ["LET", "env", ["rest", ["rest", "args"]]]]]]],

["def", "EVAL", ["fn", ["ast", "env"],
  ["if", ["not", ["list?", "ast"]],
        ["eval-ast", "ast", "env"],
        ["let", ["a0", ["get", ["first", "ast"], ["`", "val"]]],
          ["if", ["=", ["`", "def!"], "a0"],
            ["env-set", "env", ["nth", "ast", 1],
                               ["EVAL", ["nth", "ast", 2], "env"]],
          ["if", ["=", ["`", "let*"], "a0"],
            ["let", ["let-env", ["env-new", "env"]],
              ["do",
                ["LET", "let-env", ["nth", "ast", 1]],
                ["EVAL", ["nth", "ast", 2], "let-env"]]],
            ["let", ["el", ["eval-ast", "ast", "env"],
                     "f", ["first", "el"],
                     "args", ["rest", "el"]],
              ["apply", "f", "args"]]]]]]]],

["def", "PRINT", ["fn", ["exp"],
  ["pr-str", "exp", true]]],


["def", "repl-env", ["env-new"]],
["env-set", "repl-env", ["symbol", ["`", "+"]], "+"],
["env-set", "repl-env", ["symbol", ["`", "-"]], "-"],
["env-set", "repl-env", ["symbol", ["`", "*"]], "*"],
["def", "div", ["fn", ["a", "b"], ["parseInt", ["/", "a", "b"]]]],
["env-set", "repl-env", ["symbol", ["`", "/"]], "div"],

["def", "rep", ["fn", ["strng"],
  ["try",
    ["PRINT", ["EVAL", ["READ", "strng"], "repl-env"]],
    ["catch", "exc",
      ["str", ["`", "Error: "], [".", "exc", ["`", "toString"]]]]]]],

["repl", ["`", "user> "], "rep"],

null

]