Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions CodeGenerator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -65,8 +65,8 @@
271771811CBC1C90003BF13C /* Square.jack */ = {isa = PBXFileReference; lastKnownFileType = text; path = Square.jack; sourceTree = "<group>"; };
271771821CBC1C90003BF13C /* SquareGame.jack */ = {isa = PBXFileReference; lastKnownFileType = text; path = SquareGame.jack; sourceTree = "<group>"; };
271771831CBC1C90003BF13C /* SquareMain.jack */ = {isa = PBXFileReference; lastKnownFileType = text; path = SquareMain.jack; sourceTree = "<group>"; };
2717718D1CBD7138003BF13C /* SymbolTable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SymbolTable.cpp; sourceTree = "<group>"; };
2717718E1CBD7138003BF13C /* SymbolTable.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SymbolTable.hpp; sourceTree = "<group>"; };
2717718D1CBD7138003BF13C /* IdentifierTable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IdentifierTable.cpp; sourceTree = "<group>"; };
2717718E1CBD7138003BF13C /* IdentifierTable.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = IdentifierTable.hpp; sourceTree = "<group>"; };
271771901CBD716F003BF13C /* TableEntry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TableEntry.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
115 changes: 115 additions & 0 deletions CodeGenerator/IdentifierTable.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions CodeGenerator/IdentifierTable.hpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <cmath>
#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 */
126 changes: 0 additions & 126 deletions CodeGenerator/SymbolTable.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions CodeGenerator/SymbolTable.hpp

This file was deleted.

Loading