Skip to content

Commit

Permalink
Cleanups, new comment and inequality style, multiple comments bugfix,…
Browse files Browse the repository at this point in the history
… 'AND' instead of 'ADD' in EXP, ...
  • Loading branch information
psde committed Jan 16, 2013
1 parent 11bc42d commit 324a67d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
13 changes: 9 additions & 4 deletions src/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void Lexer::ungetChar(unsigned int count)

bool Lexer::isComment()
{
if(currentChar == '(' && this->buffer->peekChar() == '*')
if(currentChar == '/' && this->buffer->peekChar() == '*')
{
return true;
}
Expand All @@ -165,16 +165,21 @@ Token Lexer::nextToken()
return token;
}

if(this->isComment())
while(this->isComment())
{
Position commentStart = this->pos;
while(true)
{
if(currentChar == '*' && this->buffer->peekChar() == ')')
if(currentChar == '*' && this->buffer->peekChar() == '/')
{
// do this twice, so that ')' is not the current char anymore
this->getChar();
this->getChar();
while(isspace(currentChar))
{
this->getChar();

}
break;
}

Expand Down Expand Up @@ -246,7 +251,7 @@ Token Lexer::nextToken()
if(this->finalState[state])
{
#ifdef LEXER_DEBUG
std::cout << "We got a final state: " << LexerStateStrings[state] << std::endl;
std::cout << "We got a final state: " << LexerStateStrings[state] << " lexem: " << lexem << std::endl;
#endif
token.setType(this->finalState[state]);
token.setLexem(lexem);
Expand Down
49 changes: 37 additions & 12 deletions src/ParseTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class Decl : public IdentifierParseTree

public:
Decl(String identifier = "")
: IdentifierParseTree(identifier) {}
: IdentifierParseTree(identifier)
, arraySize(0)
, array(false)
{}

bool getArray() { return this->array; }
int getArraySize() { return this->arraySize; }
Expand Down Expand Up @@ -141,6 +144,9 @@ class Exp2_1 : public Exp2
protected:
Exp *exp;
public:
Exp2_1()
: exp(NULL) {}

~Exp2_1();

void setExp(Exp *exp) { this->exp = exp; }
Expand All @@ -158,6 +164,9 @@ class Exp2_2 : public Exp2, public IdentifierParseTree
Index *index;

public:
Exp2_2()
: IdentifierParseTree(""), index(NULL) {}

~Exp2_2();

void setIndex(Index *index) { this->index = index; }
Expand All @@ -174,6 +183,9 @@ class Exp2_3 : public Exp2
protected:
long integer;
public:
Exp2_3()
: integer(0) {}

void setInteger(long integer) { this->integer = integer; }
long getInteger() { return this->integer; }

Expand All @@ -188,6 +200,9 @@ class Exp2_4 : public Exp2
protected:
Exp2 *exp2;
public:
Exp2_4()
: exp2(NULL) {}

~Exp2_4() { delete this->exp2; }

void setExp2(Exp2 *exp2) { this->exp2 = exp2; }
Expand All @@ -204,6 +219,9 @@ class Exp2_5 : public Exp2
protected:
Exp2 *exp2;
public:
Exp2_5()
: exp2(NULL) {}

~Exp2_5() { delete this->exp2; }

void setExp2(Exp2 *exp2) { this->exp2 = exp2; }
Expand All @@ -221,7 +239,10 @@ class Exp : public ParseTree
OpExp* opexp;

public:
Exp() {}
Exp()
: exp2(NULL)
, opexp(NULL) {}

~Exp() { delete this->exp2; delete this->opexp; }

void addExp2(Exp2 *exp2) { this->exp2 = exp2; }
Expand All @@ -248,7 +269,7 @@ class Statement_1 : public Statement, public IdentifierParseTree
Index *index;
Exp *exp;
public:
Statement_1() : index(NULL), exp(NULL) {}
Statement_1() : IdentifierParseTree(""), index(NULL), exp(NULL) {}
~Statement_1() { if(this->index != NULL) delete this->index; delete this->exp; }

void setIndex(Index* index) { this->index = index; }
Expand All @@ -268,6 +289,9 @@ class Statement_2 : public Statement
protected:
Exp *exp;
public:
Statement_2()
: exp(NULL) {}

~Statement_2() { delete this->exp; }

void setExp(Exp *exp) { this->exp = exp; }
Expand All @@ -278,16 +302,13 @@ class Statement_2 : public Statement
String makeCode();
};

/* read ( EXP )*/
class Statement_3 : public Statement
/* read ( identifier INDEX )*/
class Statement_3 : public Statement, public IdentifierParseTree
{
protected:
Exp *exp;
public:
~Statement_3() { delete this->exp; }

void setExp(Exp *exp) { this->exp = exp; }
Exp* getExp() { return this->exp; }
Statement_3()
: IdentifierParseTree("") {}
~Statement_3() { }

String dump();
String typeCheck();
Expand All @@ -301,6 +322,8 @@ class Statement_4 : public Statement
protected:
Statements *statements;
public:
Statement_4()
: statements(NULL) {}
~Statement_4();

void setStatements(Statements* statements) { this->statements = statements; }
Expand Down Expand Up @@ -343,6 +366,8 @@ class Statement_6 : public Statement
Exp *exp;
Statement *statement;
public:
Statement_6()
: statement(NULL), exp(NULL) {}
~Statement_6() { delete this->statement; delete this->exp; }

void setExp(Exp *exp) { this->exp = exp; }
Expand Down Expand Up @@ -380,7 +405,7 @@ class Prog : public ParseTree
Statements *statements;

public:
Prog() {}
Prog() : decls(NULL), statements(NULL) {}
~Prog() { delete this->decls; delete this->statements; }

void addDecls(Decls *decls) { this->decls = decls; }
Expand Down
2 changes: 1 addition & 1 deletion src/ParseTreeDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ String Statement_2::dump()
String Statement_3::dump()
{
String str = "read( ";
str += exp->dump();
str += identifier;
str += " )";
return str;
}
Expand Down
14 changes: 10 additions & 4 deletions src/ParseTreeMakeCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ static long& uniqueId()

static long getUniqueId()
{
return uniqueId()++;
static long id = 1;
id++;
return id;
}

String Decl::makeCode()
Expand Down Expand Up @@ -37,7 +39,7 @@ String Decls::makeCode()

String Op::makeCode()
{
String items[] = { "ADD", "SUB", "MUL", "DIV", "LES", "EQU", ">", "<=>", "ADD", "!" };
String items[] = { "ADD", "SUB", "MUL", "DIV", "LES", "EQU", ">", "<!>", "AND", "!" };

String str = items[this->type];
str += "\n";
Expand Down Expand Up @@ -150,8 +152,11 @@ String Statement_2::makeCode()

String Statement_3::makeCode()
{
String str = exp->makeCode();
str += "REA\n";
String str = "REA\n";
str += "LA $";
str += identifier;
str += "\n";
str += "STR\n";
return str;
}

Expand Down Expand Up @@ -238,6 +243,7 @@ String Prog::makeCode()
{
String str = this->decls->makeCode();
str += this->statements->makeCode();
str += "STP\n";
return str;
}

6 changes: 4 additions & 2 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Statements* Parser::parseStatements()
/*
STATEMENT ::= identifier INDEX = EXP |
print ( EXP ) |
read ( EXP ) |
read ( identifier INDEX ) |
{ STATEMENTS } |
if ( EXP ) STATEMENT else STATEMENT |
while ( EXP ) STATEMENT
Expand Down Expand Up @@ -224,7 +224,9 @@ Statement* Parser::parseStatement()

Statement_3 *statement = new Statement_3();
this->requireToken(RULE_STATEMENT, Token::TOKEN_PAREN_L, true, true);
statement->setExp(this->parseExp());
this->requireToken(RULE_STATEMENT, Token::TOKEN_IDENTIFIER, true);
statement->setIdentifier(currentToken.getLexem());
this->nextToken();
this->requireToken(RULE_STATEMENT, Token::TOKEN_PAREN_R, true, true);
return statement;
}
Expand Down

0 comments on commit 324a67d

Please sign in to comment.