-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.pegjs
393 lines (304 loc) · 13.5 KB
/
grammar.pegjs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{
function twoargs(left, right) {
if(right.length === 0) return left;
const r = right[0];
const newel = { type: r.type, left, right: r.right };
return twoargs( newel, right.slice(1));
}
function program(items) { return { type: "program", items }; }
function include(filename, binary) { return { type: "include", filename, binary }; }
function pragma(keyword, expr,items) { return { type: "pragma", keyword, expr, items }; }
function constnode(id,expr) { return { type: "const", id, expr }; }
function assignment(id,expr) { return { type: "assignment", id, expr }; }
function ifnode(cond, ramoThen, ramoElse) { return { type: "if", cond, ramoThen, ramoElse } }
function ifsingle(cond, ramoThen) { return { type: "ifsingle", cond, ramoThen } }
function donode(body, dotype, cond) { return { type: "do", body, dotype, cond } }
function repeat(body, repeattype, cond) { return { type: "repeat", body, repeattype, cond } }
function whilenode(body, cond) { return { type: "while", body, cond } }
function fornode(start,valstart,end,step,body) { return { type: "for", start, valstart, end, step, body }; }
function sub(id, body) { return { type: "sub", id, body }; }
function exitnode(exittype) { return { type: "exit", exittype }; }
function macro(id, body) { return { type: "macro", id, body }; }
function bitmap(value) { return { type: "bitmap", value }; }
function sprite(value) { return { type: "sprite", value }; }
function float(values) { return { type: "float", values }; }
function constnode(id, value) { return { type: "const", value }; }
function flagcond(rcond) { return { type: "flagcond", rcond }; }
function registercond(register, rcond) { return { type: "registercond", register, rcond }; }
function eqcond(leftvalue, op, value) { return { type: "eqcond", leftvalue, op, value }; }
function immediateExpression(expr) { return { type: "immediate", expr }; }
function instructions(list) { return { type: "instructions", list } }
function instruction(opcode, args) { return { type: "instruction", opcode, args }; }
function opcode(opcode) { return { type: "opcode", opcode }; }
function argumentsnode(args) { return { type: "arguments", args }; }
function label(name) { return { type: "label", name}; }
function bytedef(bytetype, list) { return { type: "byte", bytetype, list: list }; }
function dim(id, size, spec) { return { type: "dim", id, size, spec }; }
}
// =====================================================================================
program
= g:(global_item)* { if(g===undefined) return program([]); else return program(g.filter(e=>e!==null).map(e=>e)) }
global_item
= basic
/ include
/ macro
/ sub
/ item
items = item*
item = emptyspace
/ pragmaif
/ emptyline
/ label
/ if
/ doloop
/ repeat
/ while
/ for
/ bitmap
/ sprite
/ float
/ const
/ exit
/ bytedef
/ assignment
/ dim
/ i:istructions eol { return i }
DO = "do"i
LOOP = "loop"i
REPEAT = "repeat"i
UNTIL = "until"i
WHILE = "while"i
WEND = "wend"i / ("end"i __ "while"i)
FOR = "for"i
TO = "to"i
STEP = "step"i
NEXT = "next"i
SUB = "sub"i
END_SUB = "end"i __ "sub"i
MACRO = "macro"i
END_MACRO = "end"i __ "macro"i
IF = "if"i
THEN = "then"i
ELSE = "else"i
END_IF = "end"i _ "if"i
BASIC_START = "basic"i __ "start"i
BASIC_END = ("basic"i __ "end"i / "end"i __ "basic"i)
INCLUDE = "include"i
BYTE = "byte"i / "word"i / "defb"i / "defw"i { return text() }
keywords = DO / LOOP /
REPEAT / UNTIL /
WHILE / WEND /
FOR / NEXT /
END_SUB /
END_MACRO /
IF / ELSE / END_IF /
BYTE
// ===============================================
include = INCLUDE binary:( __ "binary"i )? __ filename:Expression eol { return include(filename, binary!==null); }
pragmaif = keyword:('#ifdef'i/'#ifndef'i/'#if'i) __ expr:Expression __ THEN __ items:istructions { return pragma(keyword,expr,items) }
/ keyword:('#ifdef'i/'#ifndef'i/'#if'i) __ expr:Expression eol _ items:items _ '#end'i_'if'i eol { return pragma(keyword,expr,items) }
// ===============================================
assignment = !keywords id:id _ "=" _ expr:Expression eol { return assignment(id, expr) }
if
= ifsingle
/ ifmultiple
ifsingle
= IF __ cond:Cond __ THEN __ ramoThen:istructions eolnocolon
{ return ifsingle(cond, ramoThen) }
ifmultiple
= IF __ cond:Cond __ THEN eol _ ramoThen:items _ ramoElse:else _ END_IF eol
{ return ifnode(cond, ramoThen, ramoElse) }
else
= (ELSE eol _ ramoElse:items { return ramoElse } )?
doloop
= DO eol _ body:items _ LOOP __ dotype:(WHILE / UNTIL / IF) __ cond:Cond eol
{ return donode(body, dotype, cond) }
repeat
= REPEAT eol _ body:items _ repeattype:(WHILE / UNTIL) __ cond:Cond eol
{ return repeat(body, repeattype, cond) }
while
= WHILE __ cond:Cond eol _ body:items _ WEND eol
{ return whilenode(body, cond) }
for
= FOR __ start:Expression _ "=" valstart:opExpression __ TO __ end:opExpression step:(__ step:STEP stepExpr:opExpression { return step })? eol
_ body:items
_ NEXT eol
{ return fornode(start,valstart,end,step,body) }
sub
= SUB __ id:id _ "()" eol _ body:items _ END_SUB eol
{ return sub(id, body) }
exit = "exit"i __ exittype:(SUB / DO / WHILE / FOR / REPEAT) eol
{ return exitnode(exittype) }
macro
= MACRO __ id:id eol __ body:items _ END_MACRO
{ return macro(id, body) }
bitmapvalue = ("." / "-" / [0-9a-zA-Z])+
bitmap = "bitmap"i __ value:bitmapvalue eol { return bitmap(value) }
sprite = "sprite"i __ value:bitmapvalue eol { return sprite(value) }
float = "float"i __ values:Expressions eol { return float(values) }
const = "const"i __ id:id _ "=" _ expr:Expression eol { return constnode(id,expr); }
dim = "dim"i __ id:id __ "as"i __ size:("byte"i / "word"i / "integer"i / "char"i)
spec:(
absolute:(__ "at" __ expr:Expression { return {absolute: expr} }) /
zeropage:(__ "in" __ "zero"_"page" { return {zeropage: true} }) /
init: (__ "init" __ expr:Expression { return {init: expr} })?
)
{ return dim(id, size, {...spec}) }
// ===============================================
Cond = using:(
"(using a)"i __ { return "a" } /
"(using x)"i __ { return "x" } /
"(using y)"i __ { return "y" })?
signed:(
"(signed)"i __ { return "signed" } /
"(unsigned)"i __ { return "unsigned" } )?
cond:(
flagcond /
!flagcond rcond:registercond { return rcond } /
!flagcond eqcond:eqcond { return eqcond }
)
{ return { type: "cond", using, signed, cond } }
flagcond = "Z"i _ "=" _ "1" { return flagcond("z=1") } /
"ZERO"i { return flagcond("z=1") } /
"EQUAL"i { return flagcond("z=1") } /
"Z"i _ "=" _ "0" { return flagcond("z=0") } /
"NOT"i __ "ZERO"i { return flagcond("z=0") } /
"NOT"i __ "EQUAL"i { return flagcond("z=0") } /
"C"i _ "=" _ "1" { return flagcond("c=1") } /
"CARRY"i { return flagcond("c=1") } /
"C"i _ "=" _ "0" { return flagcond("c=0") } /
"NOT"i __ "CARRY"i { return flagcond("c=0") } /
"NEGATIVE"i { return flagcond("n=1") } /
"SIGN"i { return flagcond("n=1") } /
"N"i _ "=" _ "1" { return flagcond("n=1") } /
"S"i _ "=" _ "1" { return flagcond("n=1") } /
"NOT"i __ "NEGATIVE"i { return flagcond("n=0") } /
"NOT"i _ "SIGN"i { return flagcond("n=0") } /
"N"i _ "=" _ "0" { return flagcond("n=0") } /
"S"i _ "=" _ "0" { return flagcond("n=0") } /
"V"i _ "=" _ "1" { return flagcond("v=1") } /
"OVERFLOW"i { return flagcond("v=1") } /
"V"i _ "=" _ "0" { return flagcond("v=0") } /
"NOT"i __ "OVERFLOW"i { return flagcond("v=0") }
registercond = register:(register / memory) __ "is"i __ rcond:(
"zero"i { return "zero" } /
"not"i __ "zero"i { return "not zero" } /
"negative"i { return "negative" } /
"not"i __ "negative"i { return "not negative" }
)
{ return registercond(register, rcond)}
register = ("a"i / "x"i / "y"i) { return text().toUpperCase() }
memory = expr:Expression
eqcond = leftvalue:(register / memory) _ op:condop _ value:opExpression
{ return eqcond(leftvalue, op, value) }
condop = "=" / "==" / "!=" / "<>" / "<" / ">" / "<=" / ">="
opExpression = ( Expression / "#" expr:Expression { return immediateExpression(expr) } )
// ===============================================
istructions = head:istruction tail:(_":"_ istruction)*
{
if(tail.length==0) return head;
const list = [head, ...(tail.map(e=>e[3]))];
return instructions(list);
}
istruction
= !keywords opcode:opcode args:(__ arguments)? { return instruction(opcode, args == null ? null : args[1]); }
opcode
= id { return opcode(text()) }
arguments
= a:arg b:(_ "," _ arg)* { return [a, ...(b.map(e=>e[3]))]; }
arg
= opExpression
emptyline "empty line" = eol { return null }
emptyspace "empty space" = [ \t]+ { return null }
SingleLineComment
= ("//" / ";") (![\n] .)*
label
= name:id ":" eol { return label(name.id) }
id =
[_a-zA-Z]+ [_a-zA-Z0-9]* { return text() }
_ "whitespace"
= [ \t]* { return null }
__ "space"
= [ \t]+ { return null }
eol
= _ SingleLineComment? (":"/[\n]) { return null }
eolnocolon
= _ SingleLineComment? [\n] { return null }
// ===============================================
bytedef = bytetype:BYTE __ list:Expressions { return bytedef(bytetype, list) }
// ===============================================
basic
= BASIC_START eol
lines:basiclines
_ BASIC_END eol { return { type: "basic", lines: lines.filter(e=>e!==null) } }
basiclines
= basicline*
basicline
= _ num:Integer __ testo:basictext eol { return { type: "basicline", num: num, testo: testo } }
/ eol { return null }
basictext
= [^\n]* { return text() }
// ===============================================
// =====================================================================================
// https://en.cppreference.com/w/c/language/operator_precedence
// =====================================================================================
Expression = expr:Level12
Level12 = left:Level11 right:(_ op:'||' _ right:Level11 { return { type: op, right }})*
{ return twoargs(left,right) }
Level11 = left:Level10 right:(_ op:'&&' _ right:Level10 { return { type: op, right }})*
{ return twoargs(left,right) }
Level10 = left:Level4 right:(_ op:'|' _ right:Level4 { return { type: op, right }})*
{ return twoargs(left,right) }
Level9 = left:Level8 right:(_ op:'^' _ right:Level8 { return { type: op, right }})*
{ return twoargs(left,right) }
Level8 = left:Level5 right:(_ op:'&' _ right:Level5 { return { type: op, right }})*
{ return twoargs(left,right) }
Level5 = left:Level4 right:(_ op:('<<'/'>>') _ right:Level4 { return { type: op, right }})*
{ return twoargs(left,right) }
Level4 = left:Level3 right:(_ op:('+'/'-') _ right:Level3 { return { type: op, right }})*
{ return twoargs(left,right)}
Level3 = left:Level2 right:(_ op:('*' / '/' / '%' / "MOD") _ right:Level2 { return { type: op, right }})*
{ return twoargs(left,right)}
Level2 = '-' _ expr:Level1 { return { type: "unaryminus", expr } }
/ '+' _ Level1 { return { type: "unaryplus", expr } }
/ '!' _ Level1 { return { type: "not", expr } }
/ '~' _ Level1 { return { type: "bitwisenot", expr } }
/ Level1
Level1 = id:id props:('.' property:Expression)+ { return { type: "dot", id, props } }
/ id:id args:( _ '(' args:Expressions ')' { return args })? { return { type: "id", id, args: args === null ? undefined : args } }
/ Primary
Primary = '(' _ expr:Expression _ ')' { return { type: "()", expr } }
/ '[' _ expr:Expression _ ']' { return { type: "[]", expr } }
/ num:Integer { return { type: "integer", num } }
/ num:HexNumber { return { type: "integer", num: parseInt(num, 16) } }
/ num:BinNumber { return { type: "integer", num: parseInt(num, 2) } }
/ str:String { return { type: "string", str } }
Expressions = head:Expression tail:(_","_ Expression)*
{ if(tail.length==0) return [ head ];
else return [head, ...(tail.map(e=>e[3]))] }
// =====================================================================================
QuoteChar = ('"'/"'")
String = QuoteChar testo:(!QuoteChar .)* QuoteChar { return testo.map(e=>e[1]).join(""); }
Integer "integer"
= !BinPrefix [0-9]+ !HexSuffix { return parseInt(text(), 10); }
HexNumber
= HexPrefix number:[0-9a-fA-F]+ { return number.join(""); }
/ number:[0-9a-fA-F]+ HexSuffix { return number.join(""); }
HexPrefix = "&h"i / "$" / "0x"i
HexSuffix = "h"i
BinNumber = BinPrefix number:[0-1]+ { return number.join(""); }
BinPrefix = "0b"i
// =====================================================================================
/*
continue?
dim?
goto, gosub?
else if
*/
/*
expressions
selfmodlabel
MOD
HIBYTE
LOBYTE
*/