added support for token re-use
[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 void as3_set_in (FILE *  in_str );
32 extern int a3_parse();
33 extern int as3_lex();
34 extern int as3_lex_destroy();
35
36 void as3_setverbosity(int level)
37 {
38     as3_verbosity=level;
39 }
40 void as3_add_include_dir(char*dir)
41 {
42     add_include_dir(dir);
43 }
44
45 static char registry_initialized = 0;
46 static char parser_initialized = 0;
47
48 //#define STORE_TOKENS
49
50 #ifdef STORE_TOKENS
51 static mem_t tokens;
52 #endif
53
54 int a3_lex()
55 {
56     as3_tokencount++;
57 #ifdef STORE_TOKENS
58     if(as3_pass==1) {
59         int token = as3_lex();
60         /* FIXME: current_file needs to be stored, too */
61         mem_put(&tokens, &token, sizeof(token));
62         mem_put(&tokens, &a3_lval, sizeof(a3_lval));
63         mem_put(&tokens, &current_line, sizeof(current_line));
64         mem_put(&tokens, &current_column, sizeof(current_column));
65         return token;
66     } else {
67         int token;
68         mem_get(&tokens, &token, sizeof(token));
69         mem_get(&tokens, &a3_lval, sizeof(a3_lval));
70         mem_get(&tokens, &current_line, sizeof(current_line));
71         mem_get(&tokens, &current_column, sizeof(current_column));
72         return token;
73     }
74 #else
75     return as3_lex();
76 #endif
77 }
78
79 void as3_parse_file(char*filename) 
80 {
81     if(!registry_initialized) {
82         registry_initialized = 1;
83         registry_init();
84     }
85     if(!parser_initialized) {
86         parser_initialized = 1;
87         initialize_parser();
88 #if defined(STORE_TOKENS)
89         mem_init(&tokens);
90 #endif
91     }
92
93 #ifdef STORE_TOKENS
94     tokens.pos = 0;
95     tokens.read_pos = 0;
96 #endif
97
98     FILE*fi = enter_file2(filename, 0);
99
100     /* pass 1 */
101     as3_pass = 1;
102     as3_tokencount=0;
103     as3_set_in(fi);
104     initialize_file(filename);
105     a3_parse();
106     as3_lex_destroy();
107     finish_file();
108     
109 #ifdef STORE_TOKENS
110     tokens.read_pos = 0;
111 #endif
112
113     /* pass 2 */
114     as3_pass = 2;
115     as3_tokencount=0;
116     enter_file(filename, 0);
117     fseek(fi, 0, SEEK_SET);
118     as3_set_in(fi);
119     initialize_file(filename);
120     a3_parse();
121     as3_lex_destroy();
122     finish_file();
123
124     fclose(fi);
125
126 }
127
128 static void*as3code = 0;
129 void* as3_getcode()
130 {
131     if(parser_initialized) {
132         parser_initialized = 0;
133         as3code = finish_parser();
134     }
135     return as3code;
136 }
137 char* as3_getglobalclass()
138 {
139     return as3_globalclass;
140 }
141
142 void as3_destroy() 
143 {
144     if(parser_initialized) {
145         parser_initialized = 0;
146         swf_FreeABC(finish_parser());
147 #ifdef STORE_TOKENS
148         mem_clear(&tokens);
149 #endif
150     }
151     if(as3_globalclass) {
152         free(as3_globalclass);as3_globalclass=0;
153     }
154 }
155