forked from leafo/moonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.moon
More file actions
149 lines (116 loc) · 3.24 KB
/
Copy pathstatement.moon
File metadata and controls
149 lines (116 loc) · 3.24 KB
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
module "moonscript.compile", package.seeall
util = require "moonscript.util"
require "moonscript.compile.format"
dump = require "moonscript.dump"
transform = require "moonscript.transform"
import reversed from util
import ntype from require "moonscript.types"
import concat, insert from table
export line_compile
line_compile =
raw: (node) => @add node[2]
lines: (node) =>
for line in *node[2]
@add_raw line
declare: (node) =>
names = node[2]
undeclared = @declare names
if #undeclared > 0
with @line "local "
\append_list [@name name for name in *undeclared], ", "
-- this overrides the existing names with new locals, used for local keyword
declare_with_shadows: (node) =>
names = node[2]
@declare names
with @line "local "
\append_list [@name name for name in *names], ", "
assign: (node) =>
_, names, values = unpack node
undeclared = @declare names
declare = "local "..concat(undeclared, ", ")
has_fndef = false
i = 1
while i <= #values
if ntype(values[i]) == "fndef"
has_fndef = true
i = i +1
with @line!
if #undeclared == #names and not has_fndef
\append declare
else
@add declare if #undeclared > 0
\append_list [@value name for name in *names], ", "
\append " = "
\append_list [@value v for v in *values], ", "
return: (node) =>
@line "return ", if node[2] != "" then @value node[2]
break: (node) =>
"break"
if: (node) =>
cond, block = node[2], node[3]
root = with @block @line "if ", @value(cond), " then"
\stms block
current = root
add_clause = (clause)->
type = clause[1]
i = 2
next = if type == "else"
@block "else"
else
i += 1
@block @line "elseif ", @value(clause[2]), " then"
next\stms clause[i]
current.next = next
current = next
add_clause cond for cond in *node[4,]
root
repeat: (node) =>
cond, block = unpack node, 2
with @block "repeat", @line "until ", @value cond
\stms block
while: (node) =>
_, cond, block = unpack node
out = if is_non_atomic cond
with @block "while true do"
\stm {"if", {"not", cond}, {{"break"}}}
else
@block @line "while ", @value(cond), " do"
out\stms block
out
for: (node) =>
_, name, bounds, block = unpack node
loop = @line "for ", @name(name), " = ", @value({"explist", unpack bounds}), " do"
with @block loop
\declare {name}
\stms block
-- for x in y ...
-- {"foreach", {names...}, {exp...}, body}
foreach: (node) =>
_, names, exps, block = unpack node
loop = with @line!
\append "for "
\append_list [@name name for name in *names], ", "
\append " in "
\append_list [@value exp for exp in *exps], ","
\append " do"
with @block loop
\declare names
\stms block
export: (node) =>
_, names = unpack node
if type(names) == "string"
if names == "*"
@export_all = true
elseif names == "^"
@export_proper = true
else
@declare names
nil
run: (code) =>
code\call self
nil
group: (node) =>
@stms node[2]
do: (node) =>
with @block!
\stms node[2]