refactored relocation
[swftools.git] / lib / as3 / main.c
1 /* main.c
2
3    Flash ActionScript 3.0 compiler
4
5    Part of the swftools package.
6
7    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
8  
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <memory.h>
27 #include "../rfxswf.h"
28 #include "../os.h"
29 #include "files.h"
30 #include "common.h"
31 #include "tokenizer.h"
32 #include "parser.tab.h"
33 #include "parser.h"
34 #include "compiler.h"
35 #include "import.h"
36
37 void test_lexer(char*filename)
38 {
39     char*fullfilename = find_file(filename, 1);
40     enter_file(filename, fullfilename, 0);
41     FILE*fi = fopen(fullfilename, "rb");
42     as3_file_input(fi);
43     while(1) {
44         int token = as3_lex();
45         if(token==T_EOF)
46             break;
47         if(token>=32 && token<256) {
48             printf("'%c'\n", token);
49         } else {
50             printf("%s\n", token2string(token, a3_lval));
51         }
52     }
53 }
54
55 extern int a3_debug;
56 int main(int argn, char*argv[])
57 {
58     char*filename = 0;
59     char buf[512];
60     
61     if(argn<=1) {
62         fprintf(stderr, "please supply a filename\n");
63         exit(1);
64     }
65     filename=argv[argn-1];
66
67 #ifdef BISONDEBUG
68     a3_debug = 1; //if bison was called with -t
69 #endif
70    
71     as3_add_include_dir(getcwd(buf, 512));
72
73     registry_init();
74
75     int t=0;
76     char*mainclass = 0;
77     for(t=1;t<argn-1;t++) {
78         if(!strcmp(argv[t], "-lex")) {
79             test_lexer(filename);
80             return 0;
81         }
82         if(!strcmp(argv[t], "-M")) {
83             mainclass = argv[++t];
84         }
85         if(!strcmp(argv[t], "-v")) {
86             as3_verbosity++;
87         }
88         if(!strcmp(argv[t], "-q")) {
89             as3_verbosity--;
90         }
91         if(!strcmp(argv[t], "-D")) {
92             char*c = argv[t+1];
93             if(!strstr(c, "::"))
94                 printf("Error: compile definition must contain \"::\"\n");
95             as3_set_define(c);
96         }
97         if(!strcmp(argv[t], "-R")) {
98             as3_set_option("recurse","1");
99         }
100         if(!strcmp(argv[t], "-I")) {
101             as3_add_include_dir(argv[++t]);
102         }
103         if(!strcmp(argv[t], "-l")) {
104             as3_import_file(argv[++t]);
105         }
106     }
107
108     //extern int avm2_debug;
109     //avm2_debug = 1;
110
111     //memfile_t*m = memfile_open(filename);
112     //as3_parse_bytearray(filename, m->data, m->len);
113     //memfile_close(m);
114     if(!strcmp(filename, ".")) {
115         as3_parse_directory(".");
116     } else {
117         as3_parse_file(filename);
118     }
119
120     void*code = as3_getcode();
121
122     SWF swf;
123     memset(&swf, 0, sizeof(swf));
124     swf.fileVersion = 9;
125     swf.frameRate = 0x2500;
126     swf.movieSize.xmin = swf.movieSize.ymin = 0;
127     swf.movieSize.xmax = 20*20;
128     swf.movieSize.ymax = 10*20;
129
130
131     TAG*tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
132     swf_SetU8(tag, 0xff);
133     swf_SetU8(tag, 0xff);
134     swf_SetU8(tag, 0xff);
135         
136     tag = as3_getassets(tag);
137
138     tag = swf_InsertTag(tag, ST_DOABC);
139     if(!swf.firstTag && tag) swf.firstTag = tag;
140     swf_WriteABC(tag, code);
141
142     if(!mainclass)
143         mainclass = as3_getglobalclass();
144
145     if(mainclass) {
146         tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
147         swf_SetU16(tag, 1);
148         swf_SetU16(tag, 0);
149         swf_SetString(tag, mainclass);
150     } else {
151         printf("Warning: no global public MovieClip subclass\n");
152     }
153
154     tag = swf_InsertTag(tag, ST_SHOWFRAME);
155     tag = swf_InsertTag(tag, ST_END);
156
157     swf_FreeABC(code);
158
159     int f = open("abc.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
160     swf_WriteSWF(f,&swf);
161     close(f);
162
163     swf_FreeTags(&swf);
164
165     return 0;
166 }