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

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

typedef void *yyscan_t;

%}


%locations
%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
%token tokElse
%token tokNot
%token tokCommand
%token tokPlayer
%token tokLtEq
%token tokGtEq
%token tokCmp
%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 '-' '+'
%left '*' '/'
%left tokIn tokAnd tokOr
%right '<' '>' tokLtEq tokGtEq tokCmp
%left '(' ')' '[' ']'
%left NEG

%%
input: gameDecls globalDecl bodyDecl
	 ;

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

globalDecl:
		  | tokGlobal '{' globalExprList '}'
		  ;

globalExprList:
			  | globalExprList cmpltGlobalExpr
			  ;

cmpltGlobalExpr: globalExpr ';'
			   | commandDecl
			   ;

globalExpr: tokIdent '=' expr
		  ;

bodyDecl:
		| bodyDecl situation
		| bodyDecl function
		;

situation: tokSituation tokSituationName { bld.beginSituation( *($2) ); } '{' situationMembers '}' { bld.endSituation(); }
		 ;

situationMembers:
				| situationMembers situationModeFunc
				| situationMembers commandDecl
				;

situationModeFunc: situationMode '{' cmpltExprList '}'
				 ;

situationMode: tokSetup
			 | tokEnter
			 ;

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

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

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

cmpltExprList:
			 | cmpltExprList cmpltExpr
			 ;

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

forIterator: tokIdent
		   | tokIdent ':' tokIdent

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
	  ;

varRef: tokIdent { bld.addVarRef( *($1) ); }
	  | tokPlayer '.' tokIdent
	  | tokGlobal '.' tokIdent
	  | tokSituation '.' tokIdent
	  ;

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 '(' listValues ')'
	| 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 ']'
	| '[' ']'
	| '[' listValues ']'
	| '{' '}'
	| '{' dictValues '}'
	| tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); }
	| '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); }
	;

listValues: expr
		  | listValues ',' expr
		  ;

dictValues: expr ':' expr
		  | dictValues ',' expr ':' expr
		  ;

commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}'
		   ;

commandParamList:
				| commandParamList tokString
				| commandParamList tokIdent
				;
%%
/*
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;
}
*/