Many amenities can be mapped as areas.
[potlatch2.git] / it / sephiroth / expr / SWFCompiler.as
1 package it.sephiroth.expr
2 {
3         import flash.utils.ByteArray;
4         
5         import hxasm.Context;
6         import hxasm.Index;
7         import hxasm.OpCode;
8         import hxasm.Output;
9         import hxasm.Writer;
10         
11         import it.sephiroth.expr.ast.IExpression;
12         
13         public class SWFCompiler
14         {
15                 private var _max_stack: int;
16                 private var _tnumber: Index;
17                 private var _tany: Index;
18                 private var _context: Context;
19                 
20                 private var _symbols: SymbolTable;
21                 private var _expression: IExpression;
22                 
23                 public function SWFCompiler( expression: IExpression, symbols: SymbolTable )
24                 {
25                         _max_stack = 0;
26                         
27                         _symbols = symbols;
28                         _expression = expression;
29                 }
30                 
31                 public function compile(): ByteArray
32                 {
33                         var name: String;
34                         
35                         _context = new Context();
36                         
37                         _tnumber = _context.type( "Number" );
38                         _tany = _context.type( "*" );
39                         _context.beginClass( "CompiledExpression" );
40                         
41                         var m: * = _context.beginMethod( "execute", [], _tnumber );
42                         
43                         for each( name in _symbols.symbolNames )
44                         {
45                                 _context.defineField( name, _tany );
46                         }
47                         
48                         var c: SWFContext = new SWFContext( _context );
49                         
50                         for each( name in _symbols.symbolNames )
51                         {
52                                 c.storeReg( name );
53                         }
54                         
55                         _expression.compile( c );
56                         
57                         m.maxStack = c.maxStack;
58                          
59                         _context.op( OpCode.ORet );
60                         
61                         _context.finalize();
62                         
63                         var output: Output = new Output();
64                         
65                         Writer.write( output, _context );
66                         
67                         return output.getBytes();
68                         
69                 }
70
71         }
72 }