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