vbscript: Added compiler support for |not| expression.
[wine/multimedia.git] / dlls / vbscript / compile.c
blobc64c30c1315d8fd0a104fe7cdf5e81675cda828c
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <assert.h>
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
29 typedef struct {
30 parser_ctx_t parser;
32 unsigned instr_cnt;
33 unsigned instr_size;
34 vbscode_t *code;
35 } compile_ctx_t;
37 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
39 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
41 assert(id < ctx->instr_cnt);
42 return ctx->code->instrs + id;
45 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
47 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
49 if(ctx->instr_size == ctx->instr_cnt) {
50 instr_t *new_instr;
52 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
53 if(!new_instr)
54 return -1;
56 ctx->code->instrs = new_instr;
57 ctx->instr_size *= 2;
60 ctx->code->instrs[ctx->instr_cnt].op = op;
61 return ctx->instr_cnt++;
64 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
66 unsigned ret;
68 ret = push_instr(ctx, op);
69 if(ret == -1)
70 return E_OUTOFMEMORY;
72 instr_ptr(ctx, ret)->arg1.lng = arg;
73 return S_OK;
76 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
78 unsigned ret;
80 ret = push_instr(ctx, op);
81 if(ret == -1)
82 return E_OUTOFMEMORY;
84 instr_ptr(ctx, ret)->arg1.str = arg;
85 return S_OK;
88 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
90 if(!ctx->code->bstr_pool_size) {
91 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
92 if(!ctx->code->bstr_pool)
93 return NULL;
94 ctx->code->bstr_pool_size = 8;
95 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
96 BSTR *new_pool;
98 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
99 if(!new_pool)
100 return NULL;
102 ctx->code->bstr_pool = new_pool;
103 ctx->code->bstr_pool_size *= 2;
106 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
107 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
108 return NULL;
110 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
113 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
115 unsigned instr;
116 BSTR bstr;
118 bstr = alloc_bstr_arg(ctx, arg1);
119 if(!bstr)
120 return E_OUTOFMEMORY;
122 instr = push_instr(ctx, op);
123 if(instr == -1)
124 return E_OUTOFMEMORY;
126 instr_ptr(ctx, instr)->arg1.bstr = bstr;
127 instr_ptr(ctx, instr)->arg2.uint = arg2;
128 return S_OK;
131 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
133 unsigned arg_cnt = 0;
134 HRESULT hres;
136 while(args) {
137 hres = compile_expression(ctx, args);
138 if(FAILED(hres))
139 return hres;
141 arg_cnt++;
142 args = args->next;
145 *ret = arg_cnt;
146 return S_OK;
149 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr)
151 unsigned arg_cnt = 0;
152 HRESULT hres;
154 hres = compile_args(ctx, expr->args, &arg_cnt);
155 if(FAILED(hres))
156 return hres;
158 if(expr->obj_expr) {
159 FIXME("obj_expr not implemented\n");
160 hres = E_NOTIMPL;
161 }else {
162 hres = push_instr_bstr_uint(ctx, OP_icallv, expr->identifier, arg_cnt);
165 return hres;
168 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
170 HRESULT hres;
172 hres = compile_expression(ctx, expr->subexpr);
173 if(FAILED(hres))
174 return hres;
176 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
179 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
181 switch(expr->type) {
182 case EXPR_BOOL:
183 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
184 case EXPR_NOT:
185 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
186 case EXPR_STRING:
187 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
188 default:
189 FIXME("Unimplemented expression type %d\n", expr->type);
190 return E_NOTIMPL;
193 return S_OK;
196 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
198 HRESULT hres;
200 while(stat) {
201 switch(stat->type) {
202 case STAT_CALL:
203 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr);
204 break;
205 default:
206 FIXME("Unimplemented statement type %d\n", stat->type);
207 hres = E_NOTIMPL;
210 if(FAILED(hres))
211 return hres;
212 stat = stat->next;
215 return S_OK;
218 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
220 HRESULT hres;
222 func->code_off = ctx->instr_cnt;
224 hres = compile_statement(ctx, stat);
225 if(FAILED(hres))
226 return hres;
228 if(push_instr(ctx, OP_ret) == -1)
229 return E_OUTOFMEMORY;
231 return S_OK;
234 void release_vbscode(vbscode_t *code)
236 unsigned i;
238 list_remove(&code->entry);
240 for(i=0; i < code->bstr_cnt; i++)
241 SysFreeString(code->bstr_pool[i]);
243 heap_free(code->bstr_pool);
244 heap_free(code->source);
245 heap_free(code->instrs);
246 heap_free(code);
249 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
251 vbscode_t *ret;
253 ret = heap_alloc(sizeof(*ret));
254 if(!ret)
255 return NULL;
257 ret->source = heap_strdupW(source);
258 if(!ret->source) {
259 heap_free(ret);
260 return NULL;
263 ret->instrs = heap_alloc(32*sizeof(instr_t));
264 if(!ret->instrs) {
265 release_vbscode(ret);
266 return NULL;
269 ctx->instr_cnt = 0;
270 ctx->instr_size = 32;
272 ret->option_explicit = ctx->parser.option_explicit;
274 ret->bstr_pool = NULL;
275 ret->bstr_pool_size = 0;
276 ret->bstr_cnt = 0;
278 ret->global_code.code_ctx = ret;
280 list_init(&ret->entry);
281 return ret;
284 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
286 compile_ctx_t ctx;
287 HRESULT hres;
289 hres = parse_script(&ctx.parser, src);
290 if(FAILED(hres))
291 return hres;
293 ctx.code = alloc_vbscode(&ctx, src);
294 if(!ctx.code)
295 return E_OUTOFMEMORY;
297 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
298 if(FAILED(hres)) {
299 release_vbscode(ctx.code);
300 return hres;
303 list_add_tail(&script->code_list, &ctx.code->entry);
304 *ret = ctx.code;
305 return S_OK;