diff --git a/CodeGenerator.xcodeproj/project.pbxproj b/CodeGenerator.xcodeproj/project.pbxproj index 059f064..8355608 100644 --- a/CodeGenerator.xcodeproj/project.pbxproj +++ b/CodeGenerator.xcodeproj/project.pbxproj @@ -21,7 +21,7 @@ 271771841CBC1C90003BF13C /* Square.jack in CopyFiles */ = {isa = PBXBuildFile; fileRef = 271771811CBC1C90003BF13C /* Square.jack */; }; 271771851CBC1C90003BF13C /* SquareGame.jack in CopyFiles */ = {isa = PBXBuildFile; fileRef = 271771821CBC1C90003BF13C /* SquareGame.jack */; }; 271771861CBC1C90003BF13C /* SquareMain.jack in CopyFiles */ = {isa = PBXBuildFile; fileRef = 271771831CBC1C90003BF13C /* SquareMain.jack */; }; - 2717718F1CBD7138003BF13C /* SymbolTable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2717718D1CBD7138003BF13C /* SymbolTable.cpp */; }; + 2717718F1CBD7138003BF13C /* IdentifierTable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2717718D1CBD7138003BF13C /* IdentifierTable.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -65,8 +65,8 @@ 271771811CBC1C90003BF13C /* Square.jack */ = {isa = PBXFileReference; lastKnownFileType = text; path = Square.jack; sourceTree = ""; }; 271771821CBC1C90003BF13C /* SquareGame.jack */ = {isa = PBXFileReference; lastKnownFileType = text; path = SquareGame.jack; sourceTree = ""; }; 271771831CBC1C90003BF13C /* SquareMain.jack */ = {isa = PBXFileReference; lastKnownFileType = text; path = SquareMain.jack; sourceTree = ""; }; - 2717718D1CBD7138003BF13C /* SymbolTable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SymbolTable.cpp; sourceTree = ""; }; - 2717718E1CBD7138003BF13C /* SymbolTable.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SymbolTable.hpp; sourceTree = ""; }; + 2717718D1CBD7138003BF13C /* IdentifierTable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IdentifierTable.cpp; sourceTree = ""; }; + 2717718E1CBD7138003BF13C /* IdentifierTable.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = IdentifierTable.hpp; sourceTree = ""; }; 271771901CBD716F003BF13C /* TableEntry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TableEntry.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -111,8 +111,8 @@ 2717716C1CBC1986003BF13C /* CompilationEngine.hpp */, 2717716D1CBC1986003BF13C /* JackTokenizer.cpp */, 2717716E1CBC1986003BF13C /* JackTokenizer.hpp */, - 2717718D1CBD7138003BF13C /* SymbolTable.cpp */, - 2717718E1CBD7138003BF13C /* SymbolTable.hpp */, + 2717718D1CBD7138003BF13C /* IdentifierTable.cpp */, + 2717718E1CBD7138003BF13C /* IdentifierTable.hpp */, 271771901CBD716F003BF13C /* TableEntry.h */, ); path = CodeGenerator; @@ -229,7 +229,7 @@ files = ( 271771651CBC1087003BF13C /* main.cpp in Sources */, 2717716F1CBC1986003BF13C /* CompilationEngine.cpp in Sources */, - 2717718F1CBD7138003BF13C /* SymbolTable.cpp in Sources */, + 2717718F1CBD7138003BF13C /* IdentifierTable.cpp in Sources */, 271771701CBC1986003BF13C /* JackTokenizer.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/CodeGenerator.xcodeproj/xcuserdata/Kyle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/CodeGenerator.xcodeproj/xcuserdata/Kyle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/CodeGenerator.xcodeproj/xcuserdata/Kyle.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/CodeGenerator/IdentifierTable.cpp b/CodeGenerator/IdentifierTable.cpp new file mode 100644 index 0000000..61d27d0 --- /dev/null +++ b/CodeGenerator/IdentifierTable.cpp @@ -0,0 +1,115 @@ +// +// IdentifierTable.cpp +// CodeGenerator +// +// Created by Kyle Bludworth on 4/12/16. +// Copyright © 2016 Kyle Bludworth. All rights reserved. +// + +#include "IdentifierTable.hpp" + +/* + This data structure was strongly influenced by the example given below: + http://www.algolist.net/Data_structures/Hash_table/Simple_example + */ +/* + The function hashIdentifier() was strongly influenced by the user jonathanasdf + on stackoverflow: + http://stackoverflow.com/questions/2624192/good-hash-function-for-strings + */ + +#include "IdentifierTable.hpp" + +IdentifierTable::IdentifierTable() +{ + table = new TableEntry*[TABLE_SIZE]; + + for (int i = 0; i < TABLE_SIZE; i++) + table[i] = NULL; +} + +IdentifierTable::~IdentifierTable() +{ + for (int i = 0; i < TABLE_SIZE; i++) + { + if (table[i] != NULL) + delete table[i]; + } + + delete[] table; +} + +int IdentifierTable::hashIdentifier(string identifier) +{ + int hash = 7; + + for (int i = 0; i < identifier.length(); i++) + { + hash = (hash * 31) + identifier[i]; + } + + return std::abs(hash); +} + +void IdentifierTable::addEntry(string identifier, string type, int kind, + int number) +{ + int hash = probe(identifier); + + if (table[hash] != NULL) + delete table[hash]; + + table[hash] = new TableEntry(identifier, type, kind, number); +} + +int IdentifierTable::probe(string identifier) +{ + int hash = hashIdentifier(identifier) % TABLE_SIZE; + + while (table[hash] != NULL && table[hash]->getKey() != identifier) + { + hash = (hash + 1) % TABLE_SIZE; // Probe, linear like. + } + + return hash; +} + +string IdentifierTable::getType(string identifier) +{ + int hash = probe(identifier); + + if (table[hash] == NULL) + return ""; + else + return table[hash]->getType(); +} + +int IdentifierTable::getKindOf(string identifier) +{ + int hash = probe(identifier); + + if (table[hash] == NULL) + return -1; + else + return table[hash]->getKindOf(); +} + +int IdentifierTable::getNumber(string identifier) +{ + int hash = probe(identifier); + + if (table[hash] == NULL) + return -1; + else + return table[hash]->getNumber(); +} + +bool IdentifierTable::contains(string identifier) +{ + int hash = probe(identifier); + + if (table[hash] == NULL) + return false; + else + return true; +} \ No newline at end of file diff --git a/CodeGenerator/IdentifierTable.hpp b/CodeGenerator/IdentifierTable.hpp new file mode 100644 index 0000000..73b27f2 --- /dev/null +++ b/CodeGenerator/IdentifierTable.hpp @@ -0,0 +1,35 @@ +// +// IdentifierTable.hpp +// CodeGenerator +// +// Created by Kyle Bludworth on 4/12/16. +// Copyright © 2016 Kyle Bludworth. All rights reserved. +// + +#ifndef IdentifierTable_hpp +#define IdentifierTable_hpp + +#include +#include +#include "TableEntry.h" + +using std::string; + +class IdentifierTable +{ +private: + TableEntry **table; + const int TABLE_SIZE = 523; + int hashIdentifier(string identifier); +public: + IdentifierTable(); + ~IdentifierTable(); + void addEntry(string identifier, string type, int kind, int number); + int probe(string identifier); + string getType(string identifier); + int getKindOf(string identifier); + int getNumber(string identifier); + bool contains(string identifier); +}; + +#endif /* IdentifierTable_hpp */ diff --git a/CodeGenerator/SymbolTable.cpp b/CodeGenerator/SymbolTable.cpp deleted file mode 100644 index ca8fc7e..0000000 --- a/CodeGenerator/SymbolTable.cpp +++ /dev/null @@ -1,126 +0,0 @@ -// -// SymbolTable.cpp -// CodeGenerator -// -// Created by Kyle Bludworth on 4/12/16. -// Copyright © 2016 Kyle Bludworth. All rights reserved. -// - -#include "SymbolTable.hpp" - -/* - This data structure was strongly influenced by the example given below: - http://www.algolist.net/Data_structures/Hash_table/Simple_example - */ -/* - The function hashSymbol() was strongly influenced by the user jonathanasdf - on stackoverflow: - http://stackoverflow.com/questions/2624192/good-hash-function-for-strings - */ - -#include "SymbolTable.hpp" - -SymbolTable::SymbolTable() -{ - table = new TableEntry*[TABLE_SIZE]; - - for (int i = 0; i < TABLE_SIZE; i++) - table[i] = NULL; - - addPredefinedSymbols(); -} - -SymbolTable::~SymbolTable() -{ - for (int i = 0; i < TABLE_SIZE; i++) - { - if (table[i] != NULL) - delete table[i]; - } - - delete[] table; -} - -void SymbolTable::addPredefinedSymbols() -{ - addEntry("SP", 0); - addEntry("LCL", 1); - addEntry("ARG", 2); - addEntry("THIS", 3); - addEntry("THAT", 4); - addEntry("R0", 0); - addEntry("R1", 1); - addEntry("R2", 2); - addEntry("R3", 3); - addEntry("R4", 4); - addEntry("R5", 5); - addEntry("R6", 6); - addEntry("R7", 7); - addEntry("R8", 8); - addEntry("R9", 9); - addEntry("R10", 10); - addEntry("R11", 11); - addEntry("R12", 12); - addEntry("R13", 13); - addEntry("R14", 14); - addEntry("R15", 15); - addEntry("SCREEN", 16384); - addEntry("KBD", 24576); -} - -int SymbolTable::hashSymbol(string symbol) -{ - int hash = 7; - - for (int i = 0; i < symbol.length(); i++) - { - hash = (hash * 31) + symbol[i]; - } - - return std::abs(hash); -} - -void SymbolTable::addEntry(string symbol, int address) -{ - int hash = hashSymbol(symbol) % TABLE_SIZE; - - while (table[hash] != NULL && table[hash]->getKey() != symbol) - { - hash = (hash + 1) % TABLE_SIZE; - } - - if (table[hash] != NULL) - delete table[hash]; - - table[hash] = new TableEntry(symbol, address); -} - -int SymbolTable::getAddress(string symbol) -{ - int hash = hashSymbol(symbol) % TABLE_SIZE; - - while (table[hash] != NULL && table[hash]->getKey() != symbol) - { - hash = (hash + 1) % TABLE_SIZE; // Probe, linear like. - } - - if (table[hash] == NULL) - return -1; - else - return table[hash]->getValue(); -} - -bool SymbolTable::contains(string symbol) -{ - int hash = hashSymbol(symbol) % TABLE_SIZE; - - while (table[hash] != NULL && table[hash]->getKey() != symbol) - { - hash = (hash + 1) % TABLE_SIZE; - } - - if (table[hash] == NULL) - return false; - else - return true; -} \ No newline at end of file diff --git a/CodeGenerator/SymbolTable.hpp b/CodeGenerator/SymbolTable.hpp deleted file mode 100644 index 32c192f..0000000 --- a/CodeGenerator/SymbolTable.hpp +++ /dev/null @@ -1,33 +0,0 @@ -// -// SymbolTable.hpp -// CodeGenerator -// -// Created by Kyle Bludworth on 4/12/16. -// Copyright © 2016 Kyle Bludworth. All rights reserved. -// - -#ifndef SymbolTable_hpp -#define SymbolTable_hpp - -#include -#include -#include "TableEntry.h" - -using std::string; - -class SymbolTable -{ -private: - TableEntry **table; - const int TABLE_SIZE = 523; - int hashSymbol(string symbol); - void addPredefinedSymbols(); -public: - SymbolTable(); - ~SymbolTable(); - void addEntry(string symbol, int address); - int getAddress(string symbol); - bool contains(string symbol); -}; - -#endif /* SymbolTable_hpp */ diff --git a/CodeGenerator/TableEntry.h b/CodeGenerator/TableEntry.h index 0ddb178..34b3661 100644 --- a/CodeGenerator/TableEntry.h +++ b/CodeGenerator/TableEntry.h @@ -17,26 +17,45 @@ using std::string; +struct Identifier +{ + string type; + int kind; + int number; +}; + class TableEntry { private: - string symbol; // key - int address; // value + string key; // key + Identifier values; // values public: - TableEntry(string symbol, int address) + TableEntry(string key, string type, int kind, int number) { - this->symbol = symbol; - this->address = address; + this->key = key; + values.type = type; + values.kind = kind; + values.number = number; } - + string getKey() { - return symbol; + return key; + } + + string getType() + { + return values.type; + } + + int getKindOf() + { + return values.kind; } - int getValue() + int getNumber() { - return address; + return values.number; } }; diff --git a/CodeGenerator/main.cpp b/CodeGenerator/main.cpp index 7f827c2..ae2ebfe 100644 --- a/CodeGenerator/main.cpp +++ b/CodeGenerator/main.cpp @@ -8,10 +8,11 @@ #include #include "CompilationEngine.hpp" +#include "IdentifierTable.hpp" int main(int argc, const char * argv[]) { - CompilationEngine ce(""); + //CompilationEngine ce(""); return 0; }