-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.lark
85 lines (72 loc) · 3.14 KB
/
grammar.lark
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
// EBNF Grammar for D8 assembly files
// Python Lark: https://github.com/lark-parser/lark
program: line+
line: [directive | label | instruction] [comment] "\n"
directive: "."(reset | origin | define | string | byte | array)
reset: "reset" expression
origin: "origin" expression
define: "define" SYMBOL expression
string: "string" SYMBOL ESCAPED_STRING
byte: "byte" SYMBOL expression
array: "array" SYMBOL "{" expression ("," expression)* "}"
label: SYMBOL ":" [instruction]
instruction: "ADC" REG "," REG "," REG -> adc
| "ADD" REG "," REG "," REG -> add
| "AND" REG "," REG "," REG -> and
| "BCC" expression -> bcc
| "BCS" expression -> bcs
| "BEQ" expression -> beq
| "BNE" expression -> bne
| "BRA" expression -> bra
| "BSR" expression -> bsr
| "CLC" -> clc
| "CLR" REG -> clr
| "CMP" REG "," REG -> cmp
| "DEC" REG ["," REG] -> dec
| "INC" REG ["," REG] -> inc
| "LD" REG "," "#" expression -> ldi
| "LD" REG "," "&" expression -> ldi
| "LD" REG "," expression -> ldd
| "LD" REG "," "X" "," expression -> ldx
| "LD" REG "," "SP" "," expression -> ldsp
| "MOV" REG "," REG -> mov
| "NOP" -> nop
| "NOT" REG ["," REG] -> not
| "OR" REG "," REG "," REG -> or
| "PSH" REG -> psh
| "PUL" REG -> pul
| "ROLC" REG ["," REG] -> rolc
| "RORC" REG ["," REG] -> rorc
| "RTS" -> rts
| "ST" REG "," expression -> std
| "ST" REG "," "X" "," expression -> stx
| "ST" REG "," "SP" "," expression -> stsp
| "SEC" -> sec
| "STOP" -> stop
| "SBB" REG "," REG "," REG -> sbb
| "XOR" REG "," REG "," REG -> xor
comment: COMMENT_STRING
COMMENT_STRING: /;.*/
?expression: product
| expression "<<" expression -> lshift
| expression ">>" expression -> rshift
| expression "+" product -> add
| expression "-" product -> sub
?product: atom
| product "*" product -> mul
| product "/" product -> div
?atom: literal
| "-" atom -> neg
| SYMBOL -> symbol
| "(" expression ")"
?literal: INT -> integer
| HEX -> hex
| "\"" CHAR "\"" -> char
HEX: /0x[0-9a-fA-F]+/
CHAR: /[ -~]/
REG: "A" | "B" | "C" | "D" | "E" | "PAGE" | "X" | "SP"
%import common.INT
%import common.ESCAPED_STRING
%import common.CNAME -> SYMBOL
%import common.WS_INLINE
%ignore WS_INLINE