1 /* compiler.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2009 */
2 
3 /*
4 
5 Copyright (c) 2005-2009 Made to Order Software Corp.
6 
7 Permission is hereby granted, free of charge, to any
8 person obtaining a copy of this software and
9 associated documentation files (the "Software"), to
10 deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify,
12 merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom
14 the Software is furnished to do so, subject to the
15 following conditions:
16 
17 The above copyright notice and this permission notice
18 shall be included in all copies or substantial
19 portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
22 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
23 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
25 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31 
32 */
33 
34 #include	"assembler.h"
35 
36 
37 namespace sswf
38 {
39 namespace asas
40 {
41 //using namespace as;
42 
43 
44 
45 
46 //**********************************************************************
47 //***  PUBLIC COMPILER  ************************************************
48 //**********************************************************************
Compiler(void)49 Compiler::Compiler(void)
50 {
51 	f_options = &f_default_options;
52 	f_input_retriever = 0;
53 }
54 
~Compiler()55 Compiler::~Compiler()
56 {
57 }
58 
59 
60 
61 
SetInputRetriever(as::InputRetriever & input)62 void Compiler::SetInputRetriever(as::InputRetriever& input)
63 {
64 	f_input_retriever = &input;
65 }
66 
67 
SetOptions(as::Options & options)68 void Compiler::SetOptions(as::Options& options)
69 {
70 	f_options = &options;
71 }
72 
73 
Compile(as::NodePtr & program,const char * code,unsigned long line,const char * filename)74 int Compiler::Compile(as::NodePtr& program, const char *code, unsigned long line, const char *filename)
75 {
76 	as::String wcode;
77 	wcode.FromUTF8(code, strlen(code));
78 	as::StringInput input(filename);
79 	input.Set(wcode.Get(), wcode.GetLength(), line);
80 	f_options->SetOption(as::AS_OPTION_EXTENDED_ESCAPE_SEQUENCES, 1);
81 	f_options->SetOption(as::AS_OPTION_EXTENDED_OPERATORS, 1);
82 	f_options->SetOption(as::AS_OPTION_EXTENDED_STATEMENTS, 1);
83 	as::Parser *parser = as::Parser::CreateParser();
84 	parser->SetOptions(*f_options);
85 	parser->SetInput(input);
86 	program = parser->Parse();
87 	int errcnt = input.ErrCount();
88 	as::Compiler *compiler = as::Compiler::CreateCompiler(f_input_retriever);
89 	compiler->SetOptions(*f_options);
90 //fprintf(stderr, "] Compile... (Lexer result: %d)\n", errcnt);
91 	errcnt += compiler->Compile(program);
92 	as::Optimizer *optimizer = as::Optimizer::CreateOptimizer();
93 	optimizer->SetOptions(*f_options);
94 //fprintf(stderr, "] Optimize... (Compile result: %d)\n", errcnt);
95 	errcnt += optimizer->Optimize(program);
96 //fprintf(stderr, "] All done... (Optimizer result: %d)\n", errcnt);
97 	return errcnt;
98 }
99 
100 
101 
102 
103 
104 
105 
106 };	// namespace asas
107 };	// namespace sswf
108