new function as3_parse_bytearray
[swftools.git] / lib / as3 / compiler.c
1 /* compiler.h
2
3    Compiler for parsing Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008/2009 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include "tokenizer.h"
25 #include "files.h"
26 #include "parser.h"
27 #include "parser.tab.h"
28 #include "compiler.h"
29
30 /* flex/bison definitions */
31 extern int a3_parse();
32 extern int as3_lex();
33 extern int as3_lex_destroy();
34
35 void as3_setverbosity(int level)
36 {
37     as3_verbosity=level;
38 }
39 void as3_add_include_dir(char*dir)
40 {
41     add_include_dir(dir);
42 }
43
44 static char registry_initialized = 0;
45 static char parser_initialized = 0;
46
47 //#define STORE_TOKENS
48
49 #ifdef STORE_TOKENS
50 static mem_t tokens;
51 #endif
52
53 int a3_lex()
54 {
55     as3_tokencount++;
56 #ifdef STORE_TOKENS
57     if(as3_pass==1) {
58         int token = as3_lex();
59         /* FIXME: current_file needs to be stored, too */
60         mem_put(&tokens, &token, sizeof(token));
61         mem_put(&tokens, &a3_lval, sizeof(a3_lval));
62         mem_put(&tokens, &current_line, sizeof(current_line));
63         mem_put(&tokens, &current_column, sizeof(current_column));
64         return token;
65     } else {
66         int token;
67         mem_get(&tokens, &token, sizeof(token));
68         mem_get(&tokens, &a3_lval, sizeof(a3_lval));
69         mem_get(&tokens, &current_line, sizeof(current_line));
70         mem_get(&tokens, &current_column, sizeof(current_column));
71         return token;
72     }
73 #else
74     return as3_lex();
75 #endif
76 }
77
78 static void as3_parse(const char*name, const char*filename, void*mem, int length)
79 {
80     if(!registry_initialized) {
81         registry_initialized = 1;
82         registry_init();
83     }
84     if(!parser_initialized) {
85         parser_initialized = 1;
86         initialize_parser();
87 #if defined(STORE_TOKENS)
88         mem_init(&tokens);
89 #endif
90     }
91
92 #ifdef STORE_TOKENS
93     tokens.pos = 0;
94     tokens.read_pos = 0;
95 #endif
96
97     FILE*fi = 0;
98     if(filename) {
99         fi = enter_file2(name, filename, 0);
100         as3_file_input(fi);
101     } else {
102         enter_file(name, name, 0);
103         as3_buffer_input(mem, length);
104     }
105
106     /* pass 1 */
107     as3_pass = 1;
108     as3_tokencount=0;
109     initialize_file(name);
110     a3_parse();
111     as3_lex_destroy();
112     finish_file();
113     
114 #ifdef STORE_TOKENS
115     tokens.read_pos = 0;
116 #endif
117
118     if(filename) {
119         fclose(fi);
120         fi = enter_file2(name, filename, 0);
121         as3_file_input(fi);
122     } else {
123         enter_file(name, name, 0);
124         as3_buffer_input(mem, length);
125     }
126
127     /* pass 2 */
128     as3_pass = 2;
129     as3_tokencount=0;
130     initialize_file(name);
131     a3_parse();
132     as3_lex_destroy();
133     finish_file();
134
135     if(filename) {
136         fclose(fi);
137     }
138 }
139
140 void as3_parse_bytearray(const char*name, void*mem, int length)
141 {
142     as3_parse(name, 0, mem, length);
143 }
144
145 void as3_parse_file(const char*filename) 
146 {
147     char*fullfilename = find_file(filename);
148     if(!fullfilename)
149         return; // not found
150     as3_parse(filename, fullfilename, 0,0);
151 }
152
153 static void*as3code = 0;
154 void* as3_getcode()
155 {
156     if(parser_initialized) {
157         parser_initialized = 0;
158         as3code = finish_parser();
159     }
160     return as3code;
161 }
162 char* as3_getglobalclass()
163 {
164     return as3_globalclass;
165 }
166
167 void as3_destroy() 
168 {
169     if(parser_initialized) {
170         parser_initialized = 0;
171         swf_FreeABC(finish_parser());
172 #ifdef STORE_TOKENS
173         mem_clear(&tokens);
174 #endif
175     }
176     if(as3_globalclass) {
177         free(as3_globalclass);as3_globalclass=0;
178     }
179 }
180