-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.txt
235 lines (216 loc) · 3.62 KB
/
README.txt
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Syntax:
SCRIPT -> {FUNC}
FUNC -> ID '(' [FUNC_ARGS] ')' '{' INST '}'
FUNC_ARGS -> ID {',' ID}
INST -> ';'
INST -> EXPR ';'
INST -> 'return' [ EXPR ] ';'
INST -> 'if' '(' EXPR ')' INST ['else' INST]
INST -> 'for' '(' ID [ ',' ID] ':' EXPR ')' INST
INST -> 'while' '(' EXPR ')' INST
INST -> '{' { INST } '}'
INST -> 'break'
INST -> 'continue'
EXPR -> ORR_EXPR [ '=' EXPR ]
ORR_EXPR -> AND_EXPR { '||' AND_EXPR }
AND_EXPR -> CMP_EXPR { '&&' CMP_EXPR }
CMP_EXPR -> SUM_EXPR { '<' SUM_EXPR }
CMP_EXPR -> SUM_EXPR { '<=' SUM_EXPR }
CMP_EXPR -> SUM_EXPR { '>' SUM_EXPR }
CMP_EXPR -> SUM_EXPR { '>=' SUM_EXPR }
CMP_EXPR -> SUM_EXPR { '==' SUM_EXPR }
CMP_EXPR -> SUM_EXPR { '!=' SUM_EXPR }
SUM_EXPR -> MUL_EXPR { '+' MUL_EXPR }
SUM_EXPR -> MUL_EXPR { '-' MUL_EXPR }
MUL_EXPR -> SNG_EXPR { '*' SNG_EXPR }
MUL_EXPR -> SNG_EXPR { '/' SNG_EXPR }
MUL_EXPR -> SNG_EXPR { '%' SNG_EXPR }
SNG_EXPR -> '+' SNG_EXPR
SNG_EXPR -> '-' SNG_EXPR
SNG_EXPR -> '!' SNG_EXPR
SNG_EXPR -> REF_EXPR
REF_EXPR -> TOP_EXPR { REF_TAIL }
REF_TAIL -> '(' ARGS ')'
REF_TAIL -> '.' ID
ARGS -> [EXPR {',' EXPR}]
TOP_EXPR -> ID
TOP_EXPR -> NUM
TOP_EXPR -> STR
TOP_EXPR -> '(' EXPR ')'
Example:
sword() {
s = weapon();
s.name = "sword";
s.damage = "1d10";
return s;
}
granade_boom(g)
{
a = explosion_area(g.position, 5);
// TODO: add owner here
d = damage_effect("3d6", "fire");
add_area_effect(d, a);
delete(g);
}
granade_start(g)
{
if (a.activated) {
info("Granade is already activated.");
return;
}
a.activated = 1;
// schedule boom in 10 seconds
add_event(g, granade_boom, 10);
}
granade()
{
g = item();
g.name = "granade";
g.activated = 0;
g.on_use = granade_start;
return g;
}
====== Machine ========
Support for commands:
movsp #n
push previous frame pointer on stack, move sp by #n units
ret #a, #b
pop $0 (aka FP) to fp_
copy #b registers to fp[-#a] position
set FP to fp_
retv #b
pop $0 (aka FP) to fp_
pop $-1 (aka n_args) to n_args
copy #b registers to fp[-#n_args] position
set FP to fp_
push $r
push content of register $r on stack
push #n
push integer (12bit) on stack
push *n
push large integer (indexed by 12bit) on stack
push "str"
push string (indexed by 12bit) on stack
pushn #n
push nil #n times on stack
pop $r
pop to regiter $r
popn #n
pop #n times and ignore results
call #n
call global function of index #n
calli #n
call code at offset cur+#n
callb #n
call builin function #n
jz #n
pop stack, if zero pc = pc + #n
jnz #n
pop stack, if non-zero pc = pc + #n
jmp #n
pc = pc + #n
push $r
push #n
push *n
push "str"
pushn #n
pop $r
callg #func
call #func
callb #func
ret #n
jz label
jnz label
jmp label
How it may look like:
a)
x, x_ = 1, 0;
while (x < 1000) {
print(x);
x, x_ = x + x_, x;
}
maps to
push #0
push #1
pop $0
pop $1
L1: push $0
push #1000
call <
jz L2
push $0
call print
push $0
push $1
call +
push $0
pop $1
pop $2
jmp L1
L2:
b)
g.name = "granade";
==>
push "granade"
push "name"
push $0 ; assume that g is in 0
call .
call =
Result 17 instruction == 34 bytes :)
Requirements:
- returning tuples
- functions with variable number of arguments
-
c)
fib(a,b) : 2;
main() {
x, x_ = 1, 0;
while (x < 100)
x, x_ = fib(x, x_);
}
fib(a,b) {
return a + b, a;
}
===>
main: movsp #2
push #0
push #1
pop $1
pop $2
L1: push #100
push $1
call <
jz L2
push $2
push $1
calli fib
pop $1
pop $2
jmp L1
L2: ret #0, #2
fib: push $-1
push $-2
call +
push $-1
ret #2, #2
d)
info(...) {
for (i = 1; i <= arg[0]; ++i)
print(arg[i]);
return i;
}
===>
info: movsp #1
push #1
pop $1
L1: push #0
call arg[]
push $1
call <=
ifz L2
push $0
call arg[]
call print
jmp L1
L2: push $1
retv #1