-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recipe.g4
135 lines (111 loc) · 3.39 KB
/
Recipe.g4
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
grammar Recipe;
@header {
#include "../wci/intermediate/TypeSpec.h"
using namespace wci::intermediate;
}
program : header mainBlock;
mainBlock: block;
block: data code?;
header : HEADER_SYM IDENTIFIER call?;
data: DECLARATION_SYM declList;
//Declaration section
declList: (decl) ;
decl locals [ TypeSpec *type = nullptr ]: '-' number dtype=(INT_TYPE | FLOAT_TYPE | ARR_FLOAT_TYPE| ARR_INT_TYPE) OF IDENTIFIER;
//Code section
code: CODE_SYM codeLineList?;
codeLineList: (codeLine) ;
codeLine: INTEGER '.' statementList PERIOD;
statementList: statement (THEN statement)*;
statement:
| 'Done' |'Wait'
| addSubStm
| mulStm
| divStm
| incDecStm
| whileStm
| untilStm
| functionCall
| ifStm
| printStm
| printCharStm
| returnStm
| assignmentStm
| jumpStm
;
assignmentStm: ASSIGN variable 'as' operand;
jumpStm: GOTO INTEGER;
//function
functionCall: FUNCTION IDENTIFIER 'for' variable call?;
call: 'with' variable ((COMMA | (COMMA? AND)) variable)*;
returnStm: RETURN variable;
//arithmetics
addSubStm: op=(ADD | SUB) variable ((COMMA | (COMMA? AND)) variable)* DEST variable;
mulStm: MUL variable ((COMMA | (COMMA? AND)) variable)* DEST variable;
divStm: DIV variable ('and again'? DEST 'size of'? (variable | number)) ;
incDecStm: op=(INC | DEC) variable ('for' number ('min' | 'mins'))?;
//Logic
ifStm: IF conditionList COMMA statementList (COMMA ELSE statementList)?;
whileStm: WHILE conditionList COMMA WHILE_DO statementList;
untilStm: REPEAT statementList UNTIL conditionList;
printStm: PRINT variable ((COMMA | (COMMA? AND)) variable)*;
printCharStm: PRINT_CHAR variable ((COMMA | (COMMA? AND)) variable)*;
//Logic expressions
conditionList: condition (andCond | orCond)*;
condition: operand IS NOT? ((comp=(GT | LT | EQ | GE | LE))? operand );
operand locals [ TypeSpec *type = nullptr ]: variable | constant;
constant: number | trueSym;
trueSym: TRUE_SYM;
andCond: AND condition ;
orCond: OR condition;
number locals [ TypeSpec *type = nullptr ]
:INTEGER # int
| INTEGER '.' INTEGER # float;
variable locals [ TypeSpec *type = nullptr ]: (INTEGER ARRAY_ELEM OF)? IDENTIFIER;
//Keywords
GOTO: [Gg]'o to step';
PERIOD: '.';
COMMA: ','(' '|NEWLINE);
FUNCTION: 'See recipe';
INT_TYPE: 'kg' | 'L';
FLOAT_TYPE: 'g' | 'mL';
ARR_INT_TYPE: 'pieces';
ARR_FLOAT_TYPE: 'packs';
ARRAY_ELEM: ('st'|'nd'|'th')('piece'|'pack');
OF: 'of';
HEADER_SYM: 'Recipe:';
DECLARATION_SYM: 'Ingredients:';
CODE_SYM: 'Directions:';
RETURN: 'Serve';
ASSIGN: [Tt]'reat''ing'?;
IF: [Ii]'f';
THEN: 'then';
ELSE: 'otherwise';
WHILE: [Ww]'hile';
WHILE_DO: 'keep';
REPEAT: [Rr]'epeat''edly'?;
UNTIL: 'until';
ADD: [Aa]'dd''ing'?;
SUB: [Rr] 'emov'('e'|'ing');
MUL: [Mm]'ix''ing'?;
DIV: [Dd]'ivid'('e'|'ing');
STEP_SYM: 'step';
AND: 'and';
OR: 'or';
DEST: ('in''to'? )| 'to' | 'from';
PRINT: [Cc]'heck''ing'?'on'?;
PRINT_CHAR: [Ww]'eight''ing'?;
IS: 'is';
TRUE_SYM: 'done';
NOT: 'not';
GT: 'more than';
LT: 'less than';
EQ: 'as much as';
GE: 'more than or equal to';
LE: 'less than or equal to';
INC: [Bb]'ak'('e'|'ing');
DEC: [Cc]'hill''ing'?;
WS: [ \t] -> skip ;
NEWLINE : '\r'? '\n' -> skip;
COMMENT: '(' .*? ')' -> skip;
IDENTIFIER: [a-zA-Z][a-zA-Z0-9_-]*;
INTEGER: [0-9] ;