Initial import of prototype.
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/.
18 #include "SemanticAnalysis.h"
23 SemanticAnalysis::SemanticAnalysis(ParseTree *tree)
29 SemanticAnalysis::analyze()
31 for (size_t i = 0; i < tree_->statements()->length(); i++) {
32 Statement *statement = tree_->statements()->at(i);
33 statement->accept(this);
40 SemanticAnalysis::visit(NameProxy *name)
42 name->setType(name->binding()->type());
46 IsPrimitiveType(Type *type, PrimitiveType primitive)
51 if (!type->isPrimitive())
54 return primitive == type->primitive();
58 SemanticAnalysis::visit(Assignment *node)
60 node->expression()->accept(this);
62 if (node->lvalue()->isIndexExpression()) {
63 node->lvalue()->accept(this);
65 node->setType(node->lvalue()->type());
67 NameProxy *proxy = node->lvalue()->toNameProxy();
68 if (!proxy->binding()->kind() == BoundName::VARIABLE) {
69 fprintf(stderr, "error\n");
73 Variable *var = proxy->binding()->toVariable();
74 node->setType(var->type());
79 SemanticAnalysis::visit(BinaryExpression *node)
84 SemanticAnalysis::visit(ReturnStatement *node)
86 node->expression()->accept(this);
92 SemanticAnalysis::visit(ForStatement *node)
94 if (node->declaration())
95 node->declaration()->accept(this);
96 if (node->condition())
97 node->condition()->accept(this);
99 node->update()->accept(this);
100 node->body()->accept(this);
104 SemanticAnalysis::visit(BlockStatement *node)
106 for (size_t i = 0; i < node->statements()->length(); i++) {
107 Statement *statement = node->statements()->at(i);
108 statement->accept(this);
113 SemanticAnalysis::visit(VariableDeclaration *node)
117 Type *type = Type::NewPrimitive(tag);
121 if (node->postDimensions()) {
122 if (node->postDimensions()->size() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
127 for (size_t i = node->postDimensions()->size() - 1; i < node->postDimensions()->size(); i--) {
128 Expression *expr = node->postDimensions()->at(i);
131 if (!IsPrimitiveType(expr->type(), PrimitiveType_Int32)) {
132 typeError(expr, "integer");
137 if (expr->isIntegerLiteralExpression()) {
138 size = expr->toIntegerLiteralExpression()->value();
143 if (size >= INT_MAX / sizeof(int)) {
149 type = Type::NewUnsafeArray(type, size);
153 node->variable()->setType(type);
157 SemanticAnalysis::visit(IntegerLiteralExpression *node)
159 Type *type = Type::NewPrimitive(NULL);
164 SemanticAnalysis::visit(ExpressionStatement *node)
166 node->expression()->accept(this);
170 SemanticAnalysis::visit(FunctionStatement *node)
172 node->body()->accept(this);
176 SemanticAnalysis::visit(CallExpression *node)
181 SemanticAnalysis::visit(IfStatement *node)
186 SemanticAnalysis::visit(IndexExpression *node)
188 node->left()->accept(this);
189 node->right()->accept(this);
191 if (!node->right()->type(), PrimitiveType_Int32) {
192 typeError(node->right(), "integer");
195 if (!node->left()->type() || !node->left()->type()->isArray()) {
196 typeError(node->left(), "array");
199 node->setType(node->left()->type()->contained());