~technomancy/antifennel

ref: 393e16365b2fe09ba4ab8b0d775446a52d50f35f antifennel/anticompiler.fnl -rw-r--r-- 5.0 KiB
393e1636Phil Hagelberg Clarify limitations; fix a couple bugs. 2 months ago
                                                                                
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
(local fennel (require :fennel))
(local view (require :fennelview))
(local {: list : sym} fennel)

(fn map [tbl f]
  (let [out []]
    (each [_ v (ipairs tbl)]
      (table.insert out (f v)))
    out))

(fn p [x] (print (view x))) ; debugging

(local chunk-mt ["CHUNK"]) ; not doing anything w this yet; maybe useful later?
(fn chunk [contents]
  (setmetatable contents chunk-mt))

(fn function [compile {: vararg : params : body}]
  (list (sym :fn)
        (map params compile)
        (unpack (map body compile))))

(fn declare-function [compile ast]
  (doto (function compile ast)
    (table.insert 2 (compile ast.id))))

(fn local-declaration [compile {: names : expressions}]
  (list (sym :var)
        (if (= 1 (# names))
            (sym (. names 1 :name))
            (list (unpack (map names compile))))
        (if (= 1 (# expressions))
            (compile (. expressions 1))
            (list (sym :values) (unpack (map expressions compile))))))

(fn vals [compile {: arguments}]
  (if (= 1 (# arguments))
      (compile (. arguments 1))
      (list (sym :values) (map arguments compile))))

(fn binary [compile {: left : right : operator} ast]
  (list (sym operator)
        (compile left)
        (compile right)))

(fn unary [compile {: argument : operator} ast]
  (list (sym operator)
        (compile argument)))

(fn call [compile {: arguments : callee}]
  (list (compile callee) (unpack (map arguments compile))))

(fn send [compile {: receiver : method : arguments}]
  (list (sym ":")
        (compile receiver)
        method.name
        (unpack (map arguments compile))))

(fn member [compile {: object : property : computed}]
  (if computed
      (list (sym ".") (compile object) (compile property))
      (sym (.. (tostring (compile object)) "." property.name))))

(fn if* [compile {: tests : cons : alternate}]
  (if alternate
      (list (sym :if)
            (compile (. tests 1))
            (if (= 1 (# (. cons 1)))
                (compile (. cons 1 1))
                (list (sym :do) (unpack (map (. cons 1) compile))))
            (if (= 1 (# alternate))
                (compile (. alternate 1))
                (list (sym :do) (unpack (map alternate compile)))))
      (list (sym :when)
            (compile (. tests 1))
            (unpack (map (. cons 1) compile)))))

(fn concat [compile {: terms}]
  (list (sym "..")
        (compile (. terms 1))
        (compile (. terms 2))))

(fn each* [compile {: namelist : explist : body}]
  (let [binding (map namelist.names compile)]
    (each [_ form (ipairs (map explist compile))]
      (table.insert binding form))
    (list (sym :each)
          binding
          (unpack (map body compile)))))

(fn assignment [compile {: left : right}]
  (list (sym :set)
        (if (= 1 (# left))
            (sym (. left 1 :name))
            (list (unpack (map left compile))))
        (if (= 1 (# right))
            (compile (. right 1))
            (list (sym :values) (map right compile)))))

(fn while* [compile {: test : body}]
  (list (sym :while)
        (compile test)
        (unpack (map body compile))))

(fn for* [compile {: init : last : step : body}]
  (list (sym :for)
        [(compile init.id) (compile init.value) (compile last)
         (and step (compile step))]
        (unpack (map body compile))))

(fn table* [compile {: keyvals}]
  (let [out {}]
    (each [i [v k] (pairs keyvals)]
      (if k
          (tset out (compile k) (compile v))
          (tset out i (compile v))))
    out))

(fn do* [compile {: body}]
  (list (sym :do)
        (unpack (map body compile))))

(fn break [compile ast]
  (list (sym :lua) :break))

(fn unsupported [{: kind}]
  (error (.. kind " is not supported.")))

(fn compile [ast]
  (when (os.getenv "DEBUG") (print ast.kind))
  (match ast.kind
    "Chunk" (chunk (map ast.body compile)) ; top-level container of expressions
    "LocalDeclaration" (local-declaration compile ast)
    "FunctionExpression" (function compile ast)
    "FunctionDeclaration" (declare-function compile ast)
    "BinaryExpression" (binary compile ast)
    "ExpressionStatement" (compile ast.expression)
    "CallExpression" (call compile ast)
    "Identifier" (sym ast.name)
    "Literal" ast.value
    "SendExpression" (send compile ast)
    "MemberExpression" (member compile ast)
    "IfStatement" (if* compile ast)
    "ConcatenateExpression" (concat compile ast)
    "ForInStatement" (each* compile ast)
    "LogicalExpression" (binary compile ast)
    "AssignmentExpression" (assignment compile ast)
    "WhileStatement" (while* compile ast)
    "ForStatement" (for* compile ast)
    "UnaryExpression" (unary compile ast)
    "Table" (table* compile ast)
    "BreakStatement" (break compile ast)
    "DoStatement" (do* compile ast)
    "Vararg" (sym "...")

    ;; TODO: confirm it's in the tail position; otherwise compile to lua special
    "ReturnStatement" (vals compile ast)

    "RepeatStatement" (unsupported ast)
    "GotoStatement" (unsupported ast)
    "LabelStatement" (unsupported ast)
    nil (sym :nil)
    _ (error (.. "Unknown node: " (view ast)))))