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
167
168
169
170
171
172
173
174
175
176
177
178
179
std := load('../vendor/std')

log := std.log
f := std.format
map := std.map
each := std.each
filter := std.filter
clone := std.clone
append := std.append

Tokenize := load('tokenize')
Tok := Tokenize.Tok
tkString := Tokenize.tkString

Parse := load('parse')
Node := Parse.Node
ident? := Parse.ident?
ndString := Parse.ndString

decl? := expr => expr.type = Node.BinaryExpr & expr.op = Tok.DefineOp & ident?(expr.left)

analyzeSubexpr := (node, ctx, tail?) => node.type :: {
	Node.ExprList -> (
		ctx := clone(ctx)

		ctx.decls := {}
		node.exprs := map(
			node.exprs
			(n, i) => analyzeSubexpr(n, ctx, i + 1 = len(node.exprs))
		)

		` do not re-declare function parameters `
		node.decls := filter(keys(ctx.decls), decl => ctx.args.(decl) = ())
		node
	)
	Node.FnLiteral -> (
		ctx := clone(ctx)

		` we ought only count as "recursion" when a function directly calls
		itself -- we do not count references to itself in other callbacks,
		which may be called asynchronously. `
		ctx.enclosingFnLit :: {
			node -> ()
			_ -> ctx.enclosingFn := ()
		}

		ctx.decls := {}
		ctx.args := {}
		each(node.args, n => n.type :: {
			Node.Ident -> ctx.args.(n.val) := true
		})

		node.body := analyzeSubexpr(node.body, ctx, true)

		` do not re-declare function parameters `
		node.decls := filter(keys(ctx.decls), decl => ctx.args.(decl) = ())
		node
	)
	Node.MatchExpr -> (
		node.condition := analyzeSubexpr(node.condition, ctx, false)
		node.clauses := map(node.clauses, n => analyzeSubexpr(n, ctx, true))
		node
	)
	Node.MatchClause -> (
		node.target := analyzeSubexpr(node.target, ctx, false)
		node.expr := analyzeSubexpr(node.expr, ctx, true)
		node
	)
	Node.FnCall -> (
		node.fn := analyzeSubexpr(node.fn, ctx, false)
		node.args := map(node.args, n => analyzeSubexpr(n, ctx, false))

		simpleName? := node.fn.type = Node.Ident
		recursiveCall? := (ctx.enclosingFn :: {
			() -> false
			_ -> node.fn.val = ctx.enclosingFn.val
		})

		simpleName? & recursiveCall? & tail? :: {
			true -> (
				ctx.enclosingFn.recurred? := true

				{
					type: Node.FnCall
					fn: {
						type: Node.Ident
						val: '__ink_trampoline'
					}
					args: append([{
						type: Node.Ident
						val: '__ink_trampolined_' + node.fn.val
					}], node.args)
				}
			)
			_ -> node
		}
	)
	Node.BinaryExpr -> (
		defn? := node.op = Tok.DefineOp
		simpleName? := node.left.type = Node.Ident
		fnLiteral? := node.right.type = Node.FnLiteral

		defn? & simpleName? & fnLiteral? :: {
			true -> (
				fnCtx := clone(ctx)
				fnCtx.enclosingFn := node.left
				fnCtx.enclosingFnLit := node.right

				node.left := analyzeSubexpr(node.left, ctx, false)
				node.right := analyzeSubexpr(node.right, fnCtx, false)

				fnCtx.enclosingFn.recurred? :: {
					true -> (
						trampolinedFnName := '__ink_trampolined_' + fnCtx.enclosingFn.val

						ctx.decls.(trampolinedFnName) := true

						node.right := {
							type: Node.FnLiteral
							args: clone(node.right.args)
							decls: []
							body: {
								type: Node.ExprList
								decls: []
								exprs: [
									{
										type: Node.BinaryExpr
										op: Tok.DefineOp
										left: {
											type: Node.Ident
											val: trampolinedFnName
										}
										right: node.right
									}
									{
										type: Node.FnCall
										fn: {
											type: Node.Ident
											val: '__ink_resolve_trampoline'
										}
										args: append([{
											type: Node.Ident
											val: trampolinedFnName
										}], clone(node.right.args))
									}
								]
							}
						}
					)
				}
			)
			_ -> (
				node.left := analyzeSubexpr(node.left, ctx, false)
				node.right := analyzeSubexpr(node.right, ctx, false)
			)
		}

		decl?(node) :: {
			true -> ctx.decls.(node.left.val) := true
		}

		node
	)
	Node.UnaryExpr -> node.left := analyzeSubexpr(node.left, ctx, false)
	Node.ObjectLiteral -> node.entries := map(node.entries, e => analyzeSubexpr(e, ctx, false))
	Node.ObjectEntry -> (
		node.key := analyzeSubexpr(node.key, ctx, false)
		node.val := analyzeSubexpr(node.val, ctx, false)
		node
	)
	Node.ListLiteral -> node.exprs := map(node.exprs, e => analyzeSubexpr(e, ctx, false))
	_ -> node
}

analyze := node => analyzeSubexpr(node, {
	decls: {}
	args: {}
}, false)