Convert AST std::vector to PoolList.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
22 #include "SemanticAnalysis.h"
24 #include "BytecodeCompiler.h"
26 #include "Interpreter.h"
28 #include "RootScopes.h"
57 Compile(const char *stream, size_t length)
59 if (!ZONE()->initialize())
62 RootScope roots(ZONE());
63 CompileContext cc(ZONE());
65 Parser parser(ZONE(), cc, stream, length);
66 ParseTree *tree = parser.parse();
71 if (!Scope::Bind(tree))
74 SemanticAnalysis semantics(ZONE(), cc, tree);
75 if (!semantics.analyze())
78 Scope::Allocate(tree);
80 BytecodeCompiler compiler(ZONE(), tree, NULL);
81 Module *module = compiler.compile();
85 printf("\n ------------ \n\n");
87 Disassemble(module->getFunction(1)->code(), stdout);
88 //Disassemble(module->getFunction(1), stdout);
90 printf("\n ------------ \n\n");
92 Function *main = module->getFunction(1);
94 Top top = ZONE()->stack().begin();
95 LocalTop localTop(top);
97 Frame *frame = ZONE()->stack().pushFrame(main);
102 if (!Interpret(ZONE(), frame, &result))
105 printf("Result: %d\n", result.i32);
110 int main(int argc, char **argv)
113 fprintf(stderr, "Usage: <file>\n");
117 AutoVec<char> bytes(NULL);
120 File fp = fopen(argv[1], "rb");
122 fprintf(stderr, "Can't open file: \"%s\"\n", argv[1]);
126 if (fseek(fp, 0, SEEK_END)) {
127 fprintf(stderr, "Error seeking file!\n");
130 long size = ftell(fp);
132 fprintf(stderr, "Error getting file length!\n");
136 length = size_t(size);
137 bytes = new char[length + 1];
138 if (fread(bytes, 1, length, fp) != length) {
139 fprintf(stderr, "Error reading file!\n");
144 Compile(bytes, length);