vbscript: Added if statement compiler implementation.
[wine] / dlls / vbscript / parser.y
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 %{
20
21 #include "vbscript.h"
22 #include "parse.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
27
28
29 #define YYLEX_PARAM ctx
30 #define YYPARSE_PARAM ctx
31
32 static int parser_error(const char*);
33
34  static void parse_complete(parser_ctx_t*,BOOL);
35
36 static void source_add_statement(parser_ctx_t*,statement_t*);
37
38 static void *new_expression(parser_ctx_t*,expression_type_t,size_t);
39 static expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
40 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
41 static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
42 static expression_t *new_double_expression(parser_ctx_t*,double);
43 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
44 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
45
46 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
47
48 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
49 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
50 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
51  static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
52
53 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
54 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
55
56 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
57
58 %}
59
60 %pure_parser
61 %start Program
62
63 %union {
64     const WCHAR *string;
65     statement_t *statement;
66     expression_t *expression;
67     member_expression_t *member;
68     elseif_decl_t *elseif;
69     dim_decl_t *dim_decl;
70     LONG lng;
71     BOOL bool;
72     double dbl;
73 }
74
75 %token tEOF tNL tREM tEMPTYBRACKETS
76 %token tTRUE tFALSE
77 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
78 %token tIS tLTEQ tGTEQ tMOD
79 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
80 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
81 %token tWHILE tWEND tDO tLOOP tUNTIL
82 %token tBYREF tBYVAL
83 %token tOPTION tEXPLICIT
84 %token tSTOP
85 %token tNOTHING tEMPTY tNULL
86 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
87 %token tERROR tNEXT tON tRESUME tGOTO
88 %token <string> tIdentifier tString
89 %token <lng> tLong tShort
90 %token <dbl> tDouble
91
92 %type <statement> Statement StatementNl StatementsNl IfStatement Else_opt
93 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
94 %type <expression> ConcatExpression AdditiveExpression ModExpression
95 %type <expression> NotExpression UnaryExpression
96 %type <member> MemberExpression
97 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
98 %type <bool> OptionExplicit_opt
99 %type <elseif> ElseIfs_opt ElseIfs ElseIf
100 %type <dim_decl> DimDeclList
101
102 %%
103
104 Program
105     : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }
106
107 OptionExplicit_opt
108     : /* empty */                { $$ = FALSE; }
109     | tOPTION tEXPLICIT tNL      { $$ = TRUE; }
110
111 SourceElements
112     : /* empty */
113     | SourceElements StatementNl    { source_add_statement(ctx, $2); }
114
115 StatementsNl
116     : StatementNl                           { $$ = $1; }
117     | StatementNl StatementsNl              { $1->next = $2; $$ = $1; }
118
119 StatementNl
120     : Statement tNL                 { $$ = $1; }
121
122 Statement
123     : MemberExpression ArgumentList_opt     { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
124     | tCALL MemberExpression Arguments_opt  { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
125     | MemberExpression Arguments_opt '=' Expression
126                                             { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
127     | tDIM DimDeclList                      { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
128     | IfStatement                           { $$ = $1; }
129
130 MemberExpression
131     : tIdentifier                           { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
132     | CallExpression '.' tIdentifier        { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
133
134 DimDeclList /* FIXME: Support arrays */
135     : tIdentifier                           { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
136     | tIdentifier ',' DimDeclList           { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
137
138 IfStatement
139     : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
140                                             { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
141     /* FIXME: short if statement */
142
143 ElseIfs_opt
144     : /* empty */                           { $$ = NULL; }
145     | ElseIfs                               { $$ = $1; }
146
147 ElseIfs
148     : ElseIf                                { $$ = $1; }
149     | ElseIf ElseIfs                        { $1->next = $2; $$ = $1; }
150
151 ElseIf
152     : tELSEIF Expression tTHEN tNL StatementsNl
153                                             { $$ = new_elseif_decl(ctx, $2, $5); }
154
155 Else_opt
156     : /* empty */                           { $$ = NULL; }
157     | tELSE tNL StatementsNl                { $$ = $3; }
158
159 Arguments_opt
160     : EmptyBrackets_opt             { $$ = NULL; }
161     | '(' ArgumentList ')'          { $$ = $2; }
162
163 ArgumentList_opt
164     : EmptyBrackets_opt             { $$ = NULL; }
165     | ArgumentList                  { $$ = $1; }
166
167 ArgumentList
168     : Expression                    { $$ = $1; }
169     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
170
171 EmptyBrackets_opt
172     : /* empty */
173     | tEMPTYBRACKETS
174
175 Expression
176     : NotExpression                 { $$ = $1; }
177
178 NotExpression
179     : EqualityExpression            { $$ = $1; }
180     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
181
182 EqualityExpression
183     : ConcatExpression                          { $$ = $1; }
184     | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
185     | EqualityExpression tNEQ ConcatExpression  { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
186
187 ConcatExpression
188     : AdditiveExpression                        { $$ = $1; }
189     | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
190
191 AdditiveExpression
192     : ModExpression                             { $$ = $1; }
193     | AdditiveExpression '+' ModExpression      { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
194     | AdditiveExpression '-' ModExpression      { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
195
196 ModExpression
197     : UnaryExpression /* FIXME */   { $$ = $1; }
198
199
200 UnaryExpression
201     : LiteralExpression             { $$ = $1; }
202     | CallExpression                { $$ = $1; }
203     | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
204
205 CallExpression
206     : PrimaryExpression                 { $$ = $1; }
207     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
208
209 LiteralExpression
210     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
211     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
212     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
213     | tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
214     | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
215     | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
216     | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
217     | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
218     | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
219
220 PrimaryExpression
221     : '(' Expression ')'            { $$ = $2; }
222
223 %%
224
225 static int parser_error(const char *str)
226 {
227     return 0;
228 }
229
230 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
231 {
232     if(ctx->stats) {
233         ctx->stats_tail->next = stat;
234         ctx->stats_tail = stat;
235     }else {
236         ctx->stats = ctx->stats_tail = stat;
237     }
238 }
239
240 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
241 {
242     ctx->parse_complete = TRUE;
243     ctx->option_explicit = option_explicit;
244 }
245
246 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
247 {
248     expression_t *expr;
249
250     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
251     if(expr) {
252         expr->type = type;
253         expr->next = NULL;
254     }
255
256     return expr;
257 }
258
259 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
260 {
261     bool_expression_t *expr;
262
263     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
264     if(!expr)
265         return NULL;
266
267     expr->value = value;
268     return &expr->expr;
269 }
270
271 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
272 {
273     string_expression_t *expr;
274
275     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
276     if(!expr)
277         return NULL;
278
279     expr->value = value;
280     return &expr->expr;
281 }
282
283 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
284 {
285     int_expression_t *expr;
286
287     expr = new_expression(ctx, type, sizeof(*expr));
288     if(!expr)
289         return NULL;
290
291     expr->value = value;
292     return &expr->expr;
293 }
294
295 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
296 {
297     double_expression_t *expr;
298
299     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
300     if(!expr)
301         return NULL;
302
303     expr->value = value;
304     return &expr->expr;
305 }
306
307 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
308 {
309     unary_expression_t *expr;
310
311     expr = new_expression(ctx, type, sizeof(*expr));
312     if(!expr)
313         return NULL;
314
315     expr->subexpr = subexpr;
316     return &expr->expr;
317 }
318
319 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
320 {
321     binary_expression_t *expr;
322
323     expr = new_expression(ctx, type, sizeof(*expr));
324     if(!expr)
325         return NULL;
326
327     expr->left = left;
328     expr->right = right;
329     return &expr->expr;
330 }
331
332 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
333 {
334     member_expression_t *expr;
335
336     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
337     if(!expr)
338         return NULL;
339
340     expr->obj_expr = obj_expr;
341     expr->identifier = identifier;
342     expr->args = NULL;
343     return expr;
344 }
345
346 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
347 {
348     statement_t *stat;
349
350     stat = parser_alloc(ctx, size);
351     if(stat) {
352         stat->type = type;
353         stat->next = NULL;
354     }
355
356     return stat;
357 }
358
359 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
360 {
361     call_statement_t *stat;
362
363     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
364     if(!stat)
365         return NULL;
366
367     stat->expr = expr;
368     return &stat->stat;
369 }
370
371 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
372 {
373     assign_statement_t *stat;
374
375     stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
376     if(!stat)
377         return NULL;
378
379     stat->member_expr = left;
380     stat->value_expr = right;
381     return &stat->stat;
382 }
383
384 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
385 {
386     dim_decl_t *decl;
387
388     decl = parser_alloc(ctx, sizeof(*decl));
389     if(!decl)
390         return NULL;
391
392     decl->name = name;
393     decl->next = next;
394     return decl;
395 }
396
397 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
398 {
399     dim_statement_t *stat;
400
401     stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
402     if(!stat)
403         return NULL;
404
405     stat->dim_decls = decls;
406     return &stat->stat;
407 }
408
409 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
410 {
411     elseif_decl_t *decl;
412
413     decl = parser_alloc(ctx, sizeof(*decl));
414     if(!decl)
415         return NULL;
416
417     decl->expr = expr;
418     decl->stat = stat;
419     decl->next = NULL;
420     return decl;
421 }
422
423 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
424         statement_t *else_stat)
425 {
426     if_statement_t *stat;
427
428     stat = new_statement(ctx, STAT_IF, sizeof(*stat));
429     if(!stat)
430         return NULL;
431
432     stat->expr = expr;
433     stat->if_stat = if_stat;
434     stat->elseifs = elseif_decl;
435     stat->else_stat = else_stat;
436     return &stat->stat;
437 }
438
439 void *parser_alloc(parser_ctx_t *ctx, size_t size)
440 {
441     void *ret;
442
443     ret = vbsheap_alloc(&ctx->heap, size);
444     if(!ret)
445         ctx->hres = E_OUTOFMEMORY;
446     return ret;
447 }
448
449 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
450 {
451     ctx->code = ctx->ptr = code;
452     ctx->end = ctx->code + strlenW(ctx->code);
453
454     vbsheap_init(&ctx->heap);
455
456     ctx->parse_complete = FALSE;
457     ctx->hres = S_OK;
458
459     ctx->last_token = tNL;
460     ctx->last_nl = 0;
461     ctx->stats = ctx->stats_tail = NULL;
462     ctx->option_explicit = FALSE;
463
464     parser_parse(ctx);
465
466     if(FAILED(ctx->hres))
467         return ctx->hres;
468     if(!ctx->parse_complete) {
469         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
470         return E_FAIL;
471     }
472
473     return S_OK;
474 }
475
476 void parser_release(parser_ctx_t *ctx)
477 {
478     vbsheap_free(&ctx->heap);
479 }