summaryrefslogtreecommitdiff
path: root/src/parser.y
blob: bbe48403e6e8c6708bf55df92d3ca4580ee0a429 (plain)
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
%{
#include <stdint.h>
#include <stdio.h>
#include <bu/string.h>

#include "gamebuilder.h"
#include "astnode.h"

typedef void *yyscan_t;

%}


%locations
%pure-parser
/* %define api.pure */
%debug
%error-verbose

%parse-param	{ yyscan_t yyscanner }
%lex-param		{ yysacn_t yyscanner }

%parse-param	{ GameBuilder &bld }

%union {
	int64_t iValue;
	double dValue;
	Bu::String *sValue;
	bool bValue;
}

%{
int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner );
void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *error )
{
	printf("%d:%d-%d:%d: %s\n",
		llocp->first_line, llocp->first_column,
		llocp->last_line, llocp->last_column, error
		);
}
%}

%token tokGame
%token tokGlobal
%token tokFunction
%token tokSituation
%token tokSetup
%token tokEnter
%token tokAnd
%token tokOr
%token tokWhile
%token tokFor
%token tokEach
%token tokDo
%token tokIn
%token tokIf
%token tokThen "then"
%token tokElse
%token tokNot
%token tokCommand
%token tokOption
%token tokPlayer
%token tokLtEq
%token tokGtEq
%token tokCmp
%token tokReturn
%token tokIgnore
%token tokPlusAssign
%token tokMinusAssign
%token tokTimesAssign
%token tokDivideAssign
%token <sValue> tokSituationName
%token <sValue> tokIdent
%token tokGoto
%token <sValue> tokString
%token <iValue> tokInt
%token <dValue> tokFloat
%token <bValue> tokBool
%token tokNull

%destructor { delete $$; printf("string deleted.\n"); } tokString tokSituationName

%token eos 0 "end of stream"

%left NOT
%right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign
%left tokIn tokAnd tokOr
%left '-' '+'
%left '*' '/'
%right '<' '>' tokLtEq tokGtEq tokCmp
%left '(' ')' '[' ']'
%left NEG

%%
input: gameDecls globalDecl bodyDecl
	 ;

gameDecls:
		 | gameDecls tokGame '.' tokIdent '=' literal ';' { bld.setGameParam( *($4) ); }
		 ;

globalDecl:
		  | tokGlobal '{' { bld.beginGlobal(); }  globalExprList '}'
		  	{ bld.closeGlobal(); }
		  ;

globalExprList:
			  | globalExprList cmpltGlobalExpr
			  ;

cmpltGlobalExpr: globalExpr ';'
			   | commandDecl
			   | ignoreDecl
			   ;

globalExpr: tokIdent '=' expr
		  ;

bodyDecl:
		| bodyDecl situation
		| bodyDecl function
		;

situationMode: tokSituation tokSituationName { bld.beginSituation( *($2), Situation::inputCommand ); }
			 | tokCommand tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputCommand ); }
			 | tokOption tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputOption ); }
			 ;

situation: situationMode '{' situationMembers '}' { bld.endSituation(); }
		 ;

situationMembers:
				| situationMembers situationModeFunc
				| situationMembers commandDecl
				| situationMembers optionDecl
				| situationMembers ignoreDecl
				;

situationModeFunc: situationMode '{' cmpltExprList '}' {
				 	bld.closeSituationMode();
				 }
				 ;

situationMode: tokSetup { bld.beginSituationMode( Situation::modeSetup ); }
			 | tokEnter { bld.beginSituationMode( Situation::modeEnter ); }
			 ;

function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' { bld.endFunctionParams(); } '{' cmpltExprList '}' { bld.endFunction(); }
		;

funcParamList:
			 | tokIdent { bld.addFunctionParam( *($1) ); } funcParamListEx
			 ;

funcParamListEx:
			   | funcParamListEx ',' tokIdent { bld.addFunctionParam( *($3) ); }
			   ;

cmpltExprList:
			 | cmpltExprList cmpltExpr
			 ;

cmpltExpr: expr ';' { bld.endCmpltExpr(); }
		 | tokReturn '(' expr ')' ';' { bld.addNode( AstNode::tReturn ); bld.endCmpltExpr();}
		 | tokReturn '(' ')' ';' { bld.addNode( AstNode::tReturn ); bld.endCmpltExpr();}
		 | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); bld.endCmpltExpr();}
		 | ifbase
		 | tokFor tokEach {
		 		bld.addNode( AstNode::tForEach );
				bld.addNode( AstNode::tScope );
		 	} forIterator tokIn {
				bld.closeNode();
				bld.addNode( AstNode::tScope );
			} expr tokDo '{' {
				bld.closeNode();
				bld.addNode( AstNode::tScope );
			} cmpltExprList '}' {
				bld.closeNode();
				bld.closeNode();
			}
		 | tokWhile {
		 		bld.addNode( AstNode::tWhile );
				bld.addNode( AstNode::tScope );
			} expr {
				bld.closeNode();
				bld.addNode( AstNode::tScope );
			} tokDo '{' cmpltExprList '}' {
				bld.closeNode();
				bld.closeNode();
			}
		 ;

forIterator: tokIdent { bld.addVarRef( *($1), sidLocal ); }
		   | tokIdent ':' tokIdent {
		   		bld.addVarRef( *($1), sidLocal );
		   		bld.addVarRef( *($3), sidLocal );
			}
		   ;

ifbase: tokIf {
	  		bld.addNode( AstNode::tIf );
			bld.addNode( AstNode::tScope );
		} expr {
	  		bld.closeNode();
			bld.addNode( AstNode::tScope );
		} tokThen '{' cmpltExprList '}' {
			bld.closeNode();
		} ifnext {
			bld.closeNode();
		}
		;

ifnext:
	  | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' {
	  		bld.closeNode();
	  	}
	  | tokElse { bld.addNode( AstNode::tScope ); } ifbase {
	  		bld.closeNode();
	  	}
	  ;

varRef: tokIdent { bld.addVarRef( *($1), sidLocal ); }
	  | tokPlayer '.' tokIdent { bld.addVarRef( *($3), sidPlayer ); }
	  | tokGlobal '.' tokIdent { bld.addVarRef( *($3), sidGlobal ); }
	  | tokSituation '.' tokIdent { bld.addVarRef( *($3), sidSituation ); }
	  ;

literal: tokInt { bld.addLiteral( Variable( $1 ) ); }
	   | tokFloat { bld.addLiteral( Variable( $1 ) ); }
	   | tokString { bld.addLiteral( Variable( *($1) ) ); }
	   | tokBool { bld.addLiteral( Variable( $1 ) ); }
	   | tokNull { bld.addLiteral( Variable( Variable::tNull ) ); }
	   | tokSituationName { bld.addLiteral( Variable::newSituationName( *($1) ) ); }
	   ;

expr: literal
	| tokIdent '(' funcCallParams ')' { bld.addFuncCall( *($1) ); }
	| varRef
	| varRef '=' expr { bld.addNode( AstNode::tStore ); }
	| varRef tokPlusAssign expr { bld.addNode( AstNode::tPlusStore ); } 
	| varRef tokMinusAssign expr { bld.addNode( AstNode::tMinusStore ); }
	| varRef tokTimesAssign expr { bld.addNode( AstNode::tMultiplyStore ); }
	| varRef tokDivideAssign expr { bld.addNode( AstNode::tDivideStore ); }
	| expr '+' expr { bld.addNode( AstNode::tPlus ); }
	| expr '-' expr { bld.addNode( AstNode::tMinus ); }
	| expr '/' expr { bld.addNode( AstNode::tDivide ); }
	| expr '*' expr { bld.addNode( AstNode::tMultiply ); }
	| expr tokAnd expr { bld.addNode( AstNode::tAnd ); }
	| expr tokOr expr { bld.addNode( AstNode::tOr ); }
	| expr tokIn expr { bld.addNode( AstNode::tIn ); }
	| expr tokCmp expr { bld.addNode( AstNode::tComp ); }
	| expr '<' expr { bld.addNode( AstNode::tCompLt ); }
	| expr '>' expr { bld.addNode( AstNode::tCompGt ); }
	| expr tokLtEq expr { bld.addNode( AstNode::tCompLtEq ); }
	| expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); }
	| '(' expr ')'
	| expr '[' expr ']' { bld.addNode( AstNode::tIndex ); }
	| expr '[' expr ']' tokPlusAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tPlusStore ); } 
	| expr '[' expr ']' tokMinusAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tMinusStore ); }
	| expr '[' expr ']' tokTimesAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tMultiplyStore ); }
	| expr '[' expr ']' tokDivideAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tDivideStore ); }
	| expr '[' expr ']' '=' expr { bld.addNode( AstNode::tInsert ); }
	| '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); }
	| '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']'
	| '{' '}' { bld.addLiteral( Variable( Variable::tDictionary ) ); }
	| '{' { bld.addLiteral( Variable( Variable::tDictionary ) ); } dictValues '}'
	| tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); }
	| '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); }
	;

funcCallParams:
			  | expr funcCallParamsEx { bld.stackMod( AstNode::tPop ); }
			  ;

funcCallParamsEx:
				| funcCallParamsEx ',' expr { bld.stackMod( AstNode::tPop ); }
				;

listValues: expr { bld.addNode( AstNode::tAppend ); }
		  | listValues ',' expr { bld.addNode( AstNode::tAppend ); }
		  ;

dictValues: expr ':' expr { bld.addNode( AstNode::tInsert ); }
		  | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); }
		  ;

optionDecl: tokOption ':' tokString { bld.beginOption( *$3 ); } '{'
		  		cmpltExprList '}' { bld.closeOption(); }
		  ;

commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); }
		   		commandParamList '{' { bld.endCommandParams(); } cmpltExprList '}' { bld.closeCommand(); }
		   ;

commandParamList:
				| commandParamList tokString { bld.addCommandLiteral( *$2 ); }
				| commandParamList tokIdent { bld.addCommandParam( *$2 ); }
				;

ignoreDecl: tokIgnore ':' tokString ignoreList ';'
		  ;

ignoreList:
		  | ignoreList tokString
		  ;
%%
/*
void yylex_init( yyscan_t * );
void yylex_destroy( yyscan_t );

int main()
{
	yyscan_t scanner;

	yylex_init ( &scanner );
	yyparse( scanner );
	yylex_destroy ( scanner );	
	return 0;
}
*/