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
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
37 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
39 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
41 if(ctx->instr_size == ctx->instr_cnt) {
44 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
48 ctx->code->instrs = new_instr;
52 ctx->code->instrs[ctx->instr_cnt].op = op;
53 return ctx->instr_cnt++;
56 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
58 func->code_off = ctx->instr_cnt;
60 if(push_instr(ctx, OP_ret) == -1)
64 FIXME("statements compilation not implemented\n");
71 void release_vbscode(vbscode_t *code)
73 list_remove(&code->entry);
74 heap_free(code->source);
75 heap_free(code->instrs);
79 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
83 ret = heap_alloc(sizeof(*ret));
87 ret->source = heap_strdupW(source);
93 ret->instrs = heap_alloc(32*sizeof(instr_t));
100 ctx->instr_size = 32;
102 ret->global_code.code_ctx = ret;
104 list_init(&ret->entry);
108 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
113 hres = parse_script(&ctx.parser, src);
117 ctx.code = alloc_vbscode(&ctx, src);
119 return E_OUTOFMEMORY;
121 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
123 release_vbscode(ctx.code);
127 list_add_tail(&script->code_list, &ctx.code->entry);