Permalink
Browse files

untested object instantiation, fields, constructors

  • Loading branch information...
1 parent 2f106c9 commit 3edd6dfe29afef5b794688bca216ba3f71e5995d @andrewrk committed Dec 6, 2010
Showing with 248 additions and 68 deletions.
  1. +4 −0 TODO
  2. +229 −38 code_generation.cpp
  3. +2 −1 code_generation.h
  4. +1 −1 main.cpp
  5. +1 −1 parser.h
  6. +3 −19 semantic_checker.cpp
  7. +1 −3 semantic_checker.h
  8. +3 −3 symbol_table.cpp
  9. +4 −2 symbol_table.h
View
4 TODO
@@ -1,4 +1,8 @@
Project 3 Release
+test fields, assignment into and reading from
+test arrays, assignment into and reading from
+test methods with return values
+test accessing a field of an array of objects, assigning into and reading from
Project 4 Release
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,4 +1,5 @@
#include "parser.h"
+#include "symbol_table.h"
-void generate_code(Program * program, bool debug=false, bool disable_optimization=false);
+void generate_code(Program * program, SymbolTable * symbol_table, bool debug=false, bool disable_optimization=false);
View
@@ -45,7 +45,7 @@ int main(int argc, char * argv[]) {
if (only_semantic_checking)
return 0;
- generate_code(program, output_intermediate, disable_optimization);
+ generate_code(program, symbol_table, output_intermediate, disable_optimization);
return 0;
}
View
@@ -313,7 +313,7 @@ struct NegatableExpression {
struct PrimaryExpression {
// don't change this enum without changing the corresponding one in CodeGenerator
enum Type {INTEGER, REAL, STRING, BOOLEAN, VARIABLE, METHOD, OBJECT_INSTANTIATION, PARENS, NOT};
- int type;
+ Type type;
union {
LiteralInteger * literal_integer;
LiteralReal * literal_real;
View
@@ -634,13 +634,13 @@ LiteralInteger * SemanticChecker::constant_integer(Expression * expression)
return negatable_expression->primary_expression->literal_integer;
}
-Identifier * SemanticChecker::variable_access_identifier(VariableAccess * variable_access)
+Identifier * SemanticChecker::find_identifier(VariableAccess * variable_access)
{
switch (variable_access->type) {
case VariableAccess::IDENTIFIER:
return variable_access->identifier;
case VariableAccess::INDEXED_VARIABLE:
- return variable_access_identifier(variable_access->indexed_variable->variable);
+ return find_identifier(variable_access->indexed_variable->variable);
case VariableAccess::ATTRIBUTE:
return variable_access->attribute->identifier;
default:
@@ -652,7 +652,7 @@ TypeDenoter * SemanticChecker::check_indexed_variable(IndexedVariable * indexed_
{
TypeDenoter * array_type = check_variable_access(indexed_variable->variable);
if (array_type->type != TypeDenoter::ARRAY) {
- Identifier * id = variable_access_identifier(indexed_variable->variable);
+ Identifier * id = find_identifier(indexed_variable->variable);
std::cerr << err_header(id->line_number) << "indexed variable \"" << id->text << "\" is not an array" << std::endl;
m_success = false;
return NULL;
@@ -696,22 +696,6 @@ TypeDenoter * SemanticChecker::check_indexed_variable(IndexedVariable * indexed_
return array_type_iterator;
}
-Identifier * SemanticChecker::find_identifier(VariableAccess * variable_access)
-{
- switch (variable_access->type) {
- case VariableAccess::IDENTIFIER:
- return variable_access->identifier;
- case VariableAccess::INDEXED_VARIABLE:
- return find_identifier(variable_access->indexed_variable->variable);
- case VariableAccess::ATTRIBUTE:
- return variable_access->attribute->identifier;
- case VariableAccess::THIS:
- assert(false);
- return NULL;
- }
-}
-
-
TypeDenoter * SemanticChecker::check_attribute_designator(AttributeDesignator * attribute_designator)
{
TypeDenoter * owner_type = check_variable_access(attribute_designator->owner);
View
@@ -47,8 +47,6 @@ class SemanticChecker {
TypeDenoter * check_indexed_variable(IndexedVariable * indexed_variable);
TypeDenoter * check_attribute_designator(AttributeDesignator * attribute_designator);
- Identifier * find_identifier(VariableAccess * variable_access);
-
// get the result type of adding/multiplying/etc types
TypeDenoter * combined_type(TypeDenoter * left_type, TypeDenoter * right_type);
// determine if types are exactly equal
@@ -60,7 +58,7 @@ class SemanticChecker {
// returns whether the expression is a constant integer and its value if it is
LiteralInteger * constant_integer(Expression * expression);
bool is_ancestor(TypeDenoter * child, TypeDenoter * ancestor);
- Identifier * variable_access_identifier(VariableAccess * variable_access);
+ Identifier * find_identifier(VariableAccess * variable_access);
FunctionDeclaration * class_method(std::string class_name, FunctionDesignator * function_designator);
TypeDenoter * class_variable_type(std::string class_name, Identifier * variable);
void check_type(TypeDenoter * type);
View
@@ -50,7 +50,7 @@ SymbolTable * build_symbol_table(Program * program) {
other_variable->line_number << std::endl;
success = false;
} else {
- variables->put(id_list->item->text, new VariableData(variable_declaration->type, id_list->item->line_number));
+ variables->put(id_list->item->text, new VariableData(variable_declaration->type, id_list->item->line_number, id_list->item->text));
}
}
}
@@ -73,7 +73,7 @@ SymbolTable * build_symbol_table(Program * program) {
// add the function name to function symbol table
function_variables->put(function_declaration->identifier->text,
- new VariableData(function_declaration->type, function_declaration->identifier->line_number));
+ new VariableData(function_declaration->type, function_declaration->identifier->line_number, function_declaration->identifier->text));
// add function parameters to symbol table
for (VariableDeclarationList * parameter_list = function_declaration->parameter_list; parameter_list != NULL; parameter_list = parameter_list->next)
@@ -186,7 +186,7 @@ bool add_variables(OrderedInsensitiveMap<VariableData *> * function_variables, V
bool success = true;
for (IdentifierList * id_list = variable_declaration->id_list; id_list != NULL; id_list = id_list->next) {
if (! function_variables->has_key(id_list->item->text)) {
- function_variables->put(id_list->item->text, new VariableData(variable_declaration->type, id_list->item->line_number));
+ function_variables->put(id_list->item->text, new VariableData(variable_declaration->type, id_list->item->line_number, id_list->item->text));
} else {
if (function_name.compare(id_list->item->text) == 0) {
std::cerr << err_header(id_list->item->line_number) <<
View
@@ -10,10 +10,12 @@
struct VariableData {
TypeDenoter * type;
int line_number;
+ std::string name;
- VariableData(TypeDenoter * type, int line_number) :
+ VariableData(TypeDenoter * type, int line_number, std::string name) :
type(type),
- line_number(line_number) {}
+ line_number(line_number),
+ name(name) {}
};
typedef OrderedInsensitiveMap<VariableData *> VariableTable;

0 comments on commit 3edd6df

Please sign in to comment.