vbscript: Added function call statement parsing beginning implementation.
[wine] / dlls / vbscript / compile.c
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 #include <assert.h>
20
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28
29 typedef struct {
30     parser_ctx_t parser;
31
32     unsigned instr_cnt;
33     unsigned instr_size;
34     vbscode_t *code;
35 } compile_ctx_t;
36
37 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
38 {
39     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
40
41     if(ctx->instr_size == ctx->instr_cnt) {
42         instr_t *new_instr;
43
44         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
45         if(!new_instr)
46             return -1;
47
48         ctx->code->instrs = new_instr;
49         ctx->instr_size *= 2;
50     }
51
52     ctx->code->instrs[ctx->instr_cnt].op = op;
53     return ctx->instr_cnt++;
54 }
55
56 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
57 {
58     func->code_off = ctx->instr_cnt;
59
60     if(push_instr(ctx, OP_ret) == -1)
61         return E_OUTOFMEMORY;
62
63     if(stat) {
64         FIXME("statements compilation not implemented\n");
65         return E_NOTIMPL;
66     }
67
68     return S_OK;
69 }
70
71 void release_vbscode(vbscode_t *code)
72 {
73     list_remove(&code->entry);
74     heap_free(code->source);
75     heap_free(code->instrs);
76     heap_free(code);
77 }
78
79 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
80 {
81     vbscode_t *ret;
82
83     ret = heap_alloc(sizeof(*ret));
84     if(!ret)
85         return NULL;
86
87     ret->source = heap_strdupW(source);
88     if(!ret->source) {
89         heap_free(ret);
90         return NULL;
91     }
92
93     ret->instrs = heap_alloc(32*sizeof(instr_t));
94     if(!ret->instrs) {
95         release_vbscode(ret);
96         return NULL;
97     }
98
99     ctx->instr_cnt = 0;
100     ctx->instr_size = 32;
101
102     ret->global_code.code_ctx = ret;
103
104     list_init(&ret->entry);
105     return ret;
106 }
107
108 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
109 {
110     compile_ctx_t ctx;
111     HRESULT hres;
112
113     hres = parse_script(&ctx.parser, src);
114     if(FAILED(hres))
115         return hres;
116
117     ctx.code = alloc_vbscode(&ctx, src);
118     if(!ctx.code)
119         return E_OUTOFMEMORY;
120
121     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
122     if(FAILED(hres)) {
123         release_vbscode(ctx.code);
124         return hres;
125     }
126
127     list_add_tail(&script->code_list, &ctx.code->entry);
128     *ret = ctx.code;
129     return S_OK;
130 }