-
Notifications
You must be signed in to change notification settings - Fork 1
/
redcode.jison
93 lines (79 loc) · 1.82 KB
/
redcode.jison
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
/* Redcode Grammar
* Author: Stephen Halter
* Created: 2-8-10
* The grammar was derived from http://corewar.co.uk/icws94.htm the
* Annotated Draft of the Proposed 1994 Core War Standard
*/
/* tokens */
%lex
%%
[ \t]+ /* skip whitespace */
";"[^\n\r] /* skip comment */
("DAT"|"MOV"|"ADD"|"SUB"|"MUL"|
"DIV"|"MOD"|"JMP"|"JMZ"|"JMN"|
"DJN"|"CMP"|"SLT"|"SPL"|"ORG"|
"EQU"|"END") return 'OPCODE';
("AB"|"BA"|"A"|"B"|"F"|"X"|"I") return 'MODIFIER';
("#"|"$"|"@"|"<"|">") return 'MODE';
[a-zA-Z_]\w* return 'LABEL';
[+-]?\d+ return 'NUMBER';
"." return '.';
"," return ',';
"(" return '(';
")" return ')';
"+" return '+';
"-" return '-';
"*" return '*';
"/" return '/';
"%" return '%';
(\n|\r|\n\r|\r\n) return 'NEWLINE';
<<EOF>> return 'EOF';
/lex
%start redcode
%%
redcode
: instruction
| eol
| instruction redcode
| eol redcode
;
instruction
: label_list operation mode expr
| label_list operation mode expr ',' mode expr
| operation mode expr
| operation mode expr ',' mode expr
;
label_list
: label
| label label_list
| label NEWLINE label_list
;
label
: LABEL
| MODIFIER
;
operation
: OPCODE
| OPCODE '.' MODIFIER
;
mode
: MODE
|
;
expr
: term
| term '+' expr
| term '-' expr
| term '*' expr
| term '/' expr
| term '%' expr
;
term
: label
| NUMBER
| '(' expr ')'
;
eol
: NEWLINE
| EOF
;