6a664a348624d89c59e78430ece5c7bc0c9de871
[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 "tokenizer.h"
31 #include "parser.tab.h"
32 #include "parser.h"
33 #include "compiler.h"
34 #include "import.h"
35
36 void test_lexer(char*filename)
37 {
38     char*fullfilename = find_file(filename);
39     enter_file(filename, fullfilename, 0);
40     FILE*fi = fopen(fullfilename, "rb");
41     as3_file_input(fi);
42     while(1) {
43         int token = as3_lex();
44         if(token==T_EOF)
45             break;
46         if(token>=32 && token<256) {
47             printf("'%c'\n", token);
48         } else {
49             printf("%s\n", token2string(token, a3_lval));
50         }
51     }
52 }
53
54 int main(int argn, char*argv[])
55 {
56     char*filename = 0;
57     char buf[512];
58     
59     if(argn<=1) {
60         fprintf(stderr, "please supply a filename\n");
61         exit(1);
62     }
63     filename=argv[argn-1];
64    
65     as3_add_include_dir(getcwd(buf, 512));
66
67     int t=0;
68     for(t=1;t<argn-1;t++) {
69         if(!strcmp(argv[t], "-lex")) {
70             test_lexer(filename);
71             return 0;
72         }
73         if(!strcmp(argv[t], "-v")) {
74             as3_verbosity++;
75         }
76         if(!strcmp(argv[t], "-q")) {
77             as3_verbosity--;
78         }
79         if(!strcmp(argv[t], "-I")) {
80             as3_add_include_dir(argv[++t]);
81         }
82     }
83
84     //extern int avm2_debug;
85     //avm2_debug = 1;
86
87     registry_init();
88
89     //memfile_t*m = memfile_open(filename);
90     //as3_parse_bytearray(filename, m->data, m->len);
91     //memfile_close(m);
92     as3_parse_file(filename);
93
94     void*code = as3_getcode();
95
96     SWF swf;
97     memset(&swf, 0, sizeof(swf));
98     swf.fileVersion = 9;
99     swf.frameRate = 0x2500;
100     swf.movieSize.xmin = swf.movieSize.ymin = 0;
101     swf.movieSize.xmax = 20*20;
102     swf.movieSize.ymax = 10*20;
103     TAG*tag = swf.firstTag = swf_InsertTag(0, ST_DOABC);
104     swf_WriteABC(tag, code);
105
106     if(as3_getglobalclass()) {
107         tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
108         swf_SetU16(tag, 1);
109         swf_SetU16(tag, 0);
110         swf_SetString(tag, as3_getglobalclass());
111     } else {
112         printf("Warning: no global public MovieClip subclass\n");
113     }
114
115     tag = swf_InsertTag(tag, ST_SHOWFRAME);
116     tag = swf_InsertTag(tag, ST_END);
117
118     swf_FreeABC(code);
119
120     int f = open("abc.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
121     swf_WriteSWF(f,&swf);
122     close(f);
123
124     swf_FreeTags(&swf);
125
126     return 0;
127 }