Commit 5d15795e by Andrew Dahl

Fixes #16 - Comparisons work

parent 5cdc77a3
......@@ -442,7 +442,7 @@ string InstructionList::toString()
<< "MOV EBX, 0\n"
<< "CMP EAX, EBX\n";
}
this->stackPop(1);
}
else
{
......@@ -451,26 +451,36 @@ string InstructionList::toString()
if(this->getTopType() == Symbol::FLOAT)
{
// Compare floats
oss << "FLD QWORD [ESP]\n"
<< "POP EBX\n"
<< "POP EBX\n"
<< "FLD QWORD [ESP]\n"
<< "FCOMPP\n"
<< "FSTSW AX\n"
<< "FWAIT\n"
<< "SAHF\n";
}
else
{
// Compare ints
oss << "POP EBX\n"
<< "POP EAX\n"
<< "CMP EAX, EBX\n";
}
this->stackPop(2);
}
break;
case Instruction::JNE:
oss << "JE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JE:
oss << "JNE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JG:
oss << "JLE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JL:
oss << "JGE " << temp->getArg1().reg << "end\n";
break;
case Instruction::CALL:
oss << "PUSH ESI\n"
......
......@@ -30,13 +30,13 @@
char* name;
}
%token NEWLINE PLUS MINUS SLASH ASTERISK LPAREN RPAREN EQUALS USERINPUT OUTPUT FUNCTION COMMA LCURLY RCURLY RETURN TYPEINT TYPEFLOAT IF THEN
%token NEWLINE PLUS MINUS SLASH ASTERISK LPAREN RPAREN EQUALS USERINPUT OUTPUT FUNCTION COMMA LCURLY RCURLY RETURN TYPEINT TYPEFLOAT IF THEN EQUALTO GREATERTHAN LESSTHAN
%token <inum> NUMBER
%token <fnum> FLOAT
%token <name> ID
%type <sym> stat additive_exp mult_exp primary_exp assignment_exp function_define argument_exp_list func_call_args function_decl type_spec
%type <inum> selection_stat
%type <inum> selection_stat comparison_operator
%% /* Grammer Rules and Actions */
......@@ -99,9 +99,28 @@ argument_exp_list: { $$ = NULL;}
}
;
selection_stat: IF additive_exp THEN LCURLY {
instructions->addInstruction(new Instruction(Instruction::CMP, $2));
instructions->addInstruction(new Instruction(Instruction::JNE, conditional));
comparison_operator: additive_exp { instructions->addInstruction(new Instruction(Instruction::CMP, $1)); $$ = 0; }
| additive_exp EQUALTO additive_exp { instructions->addInstruction(new Instruction(Instruction::CMP, $1, $3)); $$ = 1; }
| additive_exp GREATERTHAN additive_exp { instructions->addInstruction(new Instruction(Instruction::CMP, $1, $3)); $$ = 2; }
| additive_exp LESSTHAN additive_exp { instructions->addInstruction(new Instruction(Instruction::CMP, $1, $3)); $$ = 3; }
;
selection_stat: IF comparison_operator THEN LCURLY {
switch($2)
{
case 0:
instructions->addInstruction(new Instruction(Instruction::JNE, conditional));
break;
case 1: //EQUALSTO
instructions->addInstruction(new Instruction(Instruction::JE, conditional));
break;
case 2: //GREATERTHAN
instructions->addInstruction(new Instruction(Instruction::JG, conditional));
break;
case 3: //LESSTHAN
instructions->addInstruction(new Instruction(Instruction::JL, conditional));
break;
}
int size = symbol_table->at(level)->getStackAddress();
symbol_table->push_back(new SymbolTable(level, symbol_table->at(level)));
level = symbol_table->size()-1;
......@@ -112,8 +131,8 @@ selection_stat: IF additive_exp THEN LCURLY {
int size = symbol_table->at(level)->getStackAddress();
level = symbol_table->at(level)->getParentTable();
symbol_table->at(level)->setStackAddress(size);
instructions->addInstruction(new Instruction(Instruction::CONDITION_IF, $<inum>5));
}
instructions->addInstruction(new Instruction(Instruction::CONDITION_IF, $<inum>5));
}
;
assignment_exp: additive_exp
......
......@@ -40,11 +40,14 @@ nonzeronumber [1-9]{digit}*
zero [0]
number {zero}|{nonzeronumber}
float {number}[.]{digit}+
lessthan [<]
greaterthan [>]
equalto [=][=]
plus [+]
minus [-]
asterisk [*]
slash [/]
equals [=]
equals [=][^=]
lparen [(]
rparen [)]
lcurly {whiteall}?[{]{whiteall}?
......@@ -65,6 +68,9 @@ comma [,]
"int" { return TYPEINT; }
"float" { return TYPEFLOAT; }
{identifier} { yylval.name = strdup(yytext); return ID; }
{lessthan} { return LESSTHAN; }
{greaterthan} { return GREATERTHAN; }
{equalto} { return EQUALTO; }
{plus} { return PLUS; }
{minus} { return MINUS; }
{slash} { return SLASH; }
......
function int count(int num) {
if num then {
output count(num - 1)
}
return num
}
function float fcount(float fnum) {
if fnum then {
output fcount(fnum - 1)
}
return fnum
}
function int fact(int facnum,int flag) {
if facnum-1 then {
if flag then {
output facnum
if 0 then {
output 0
}
if 0 then {
if readInput then {
output 100
}
}
if count(facnum) then {
output count(facnum)
}
}
facnum = facnum * fact(facnum-1,flag)
}
return facnum
}
output count(5)
output fcount(5.0)
output fact(readInput,readInput)
int a = 1
int b = 1
if a == b then {
output 100
a = a + 1
if a == b then {
output 110
}
if a > b then {
output 120
b = b + 2
if a > b then {
output 130
}
if a < b then {
output 140
a = a + 2
if a < b then {
output 150
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment