added cleanup code
[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
34 void test_lexer()
35 {
36     while(1) {
37         int token = avm2_lex();
38         if(token==T_EOF)
39             break;
40         if(token>=32 && token<256) {
41             printf("'%c'\n", token);
42         } else {
43             printf("%s\n", token2string(token, avm2_lval));
44         }
45     }
46 }
47
48 int main(int argn, char*argv[])
49 {
50     char*filename = 0;
51     char buf[512];
52     void*code=0;
53     
54     if(argn<=1) {
55         fprintf(stderr, "please supply a filename\n");
56         exit(1);
57     }
58     filename=argv[1];
59     
60     add_include_dir(getcwd(buf, 512));
61     
62     char*fullfilename = enter_file(filename, 0);
63     FILE*fi = fopen(fullfilename, "rb");
64     if(!fi) {
65         perror(fullfilename);
66         return 1;
67     }
68     avm2_set_in(fi);
69
70     registry_init();
71     initialize_state();
72
73     if(argn>2 && !strcmp(argv[2], "-lex")) {
74         test_lexer();
75         return 0;
76     }
77     avm2_parse();
78     code = finalize_state();
79     avm2_lex_destroy();
80     fclose(fi);
81
82     SWF swf;
83     memset(&swf, 0, sizeof(swf));
84     swf.fileVersion = 9;
85     swf.frameRate = 0x2500;
86     swf.movieSize.xmin = swf.movieSize.ymin = 0;
87     swf.movieSize.xmax = 20*20;
88     swf.movieSize.ymax = 10*20;
89     TAG*tag = swf.firstTag = swf_InsertTag(0, ST_DOABC);
90     swf_WriteABC(tag, code);
91
92     if(globalclass) {
93         tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
94         swf_SetU16(tag, 1);
95         swf_SetU16(tag, 0);
96         swf_SetString(tag, globalclass);
97         free(globalclass);globalclass=0;
98     } else {
99         printf("Warning: no global public MovieClip subclass\n");
100     }
101
102     tag = swf_InsertTag(tag, ST_SHOWFRAME);
103     tag = swf_InsertTag(tag, ST_END);
104
105     swf_FreeABC(code);
106
107     int f = open("abc.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
108     swf_WriteSWF(f,&swf);
109     close(f);
110
111     swf_FreeTags(&swf);
112
113     return 0;
114 }