Commit e377a9df by Andrew Dahl

Fixes #17 Loops Work

parent 5d15795e
......@@ -34,12 +34,16 @@ class Instruction {
JE, /* Short Jump if equal */
JG, /* Short Jump if greater than */
JL, /* Short Jump if less than */
JA, /* */
JB, /* */
JNE, /* Short Jump if not zero TEMPORARY!!!! */
CALL, /* CALL Arg */
FUNCTION_START, /* Used to differentiate blocks from functions */
VARDECL,/* Used to determine variable declarations in blocks */
FUNCTION_RETURN, /* */
CONDITION_IF /* */
CONDITION_IF, /* */
LOOP_START, /* */
LOOP_END /* */
};
enum arg_type {
......
......@@ -451,11 +451,7 @@ 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"
oss << "FCOMPP\n"
<< "FSTSW AX\n"
<< "FWAIT\n"
<< "SAHF\n";
......@@ -482,6 +478,12 @@ string InstructionList::toString()
case Instruction::JL:
oss << "JGE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JA:
oss << "JBE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JB:
oss << "JAE " << temp->getArg1().reg << "end\n";
break;
case Instruction::CALL:
oss << "PUSH ESI\n"
<< "MOV ESI, ESP\n"
......@@ -501,6 +503,14 @@ string InstructionList::toString()
oss << "PUSH DWORD [tempi]\n";
}
break;
case Instruction::LOOP_START:
oss << temp->getArg1().reg << "start:\n";
break;
case Instruction::LOOP_END:
oss << "JMP " << temp->getArg1().reg << "start\n"
<< temp->getArg1().reg << "end:\n";
break;
case Instruction::FUNCTION_START:
// Do we care? Yes.
//oss << FunctionToString(temp);
......
......@@ -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 EQUALTO GREATERTHAN LESSTHAN
%token NEWLINE PLUS MINUS SLASH ASTERISK LPAREN RPAREN EQUALS USERINPUT OUTPUT FUNCTION COMMA LCURLY RCURLY RETURN TYPEINT TYPEFLOAT IF THEN EQUALTO GREATERTHAN LESSTHAN WHILE DO
%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 comparison_operator
%type <inum> selection_stat iteration_stat
%% /* Grammer Rules and Actions */
......@@ -99,28 +99,31 @@ argument_exp_list: { $$ = NULL;}
}
;
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; }
comparison_operator: additive_exp {
instructions->addInstruction(new Instruction(Instruction::CMP, $1));
instructions->addInstruction(new Instruction(Instruction::JNE, conditional));
}
| additive_exp EQUALTO additive_exp {
instructions->addInstruction(new Instruction(Instruction::CMP, $1, $3));
instructions->addInstruction(new Instruction(Instruction::JE, conditional));
}
| additive_exp GREATERTHAN additive_exp {
instructions->addInstruction(new Instruction(Instruction::CMP, $1, $3));
if($1->getReturnType() == Symbol::FLOAT || $3->getReturnType() == Symbol::FLOAT)
instructions->addInstruction(new Instruction(Instruction::JA, conditional));
else
instructions->addInstruction(new Instruction(Instruction::JG, conditional));
}
| additive_exp LESSTHAN additive_exp {
instructions->addInstruction(new Instruction(Instruction::CMP, $1, $3));
if($1->getReturnType() == Symbol::FLOAT || $3->getReturnType() == Symbol::FLOAT)
instructions->addInstruction(new Instruction(Instruction::JB, conditional));
else
instructions->addInstruction(new Instruction(Instruction::JL, conditional));
}
;
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;
......@@ -135,6 +138,22 @@ selection_stat: IF comparison_operator THEN LCURLY {
}
;
iteration_stat: WHILE {
instructions->addInstruction(new Instruction(Instruction::LOOP_START, conditional));
} comparison_operator DO LCURLY {
int size = symbol_table->at(level)->getStackAddress();
symbol_table->push_back(new SymbolTable(level, symbol_table->at(level)));
level = symbol_table->size()-1;
symbol_table->at(level)->setStackAddress(size);
$<inum>$ = conditional;
conditional++;
} statlist RCURLY {
int size = symbol_table->at(level)->getStackAddress();
level = symbol_table->at(level)->getParentTable();
symbol_table->at(level)->setStackAddress(size);
instructions->addInstruction(new Instruction(Instruction::LOOP_END, $<inum>6));
}
assignment_exp: additive_exp
| ID EQUALS assignment_exp {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($1, Symbol::VARIABLE, $3->getReturnType()));
......@@ -194,6 +213,7 @@ stat: OUTPUT additive_exp NEWLINE {
}
}
| selection_stat { }
| iteration_stat { }
;
additive_exp: additive_exp PLUS mult_exp {
......
......@@ -61,6 +61,8 @@ comma [,]
{float} { yylval.fnum = atof(yytext); return FLOAT; }
"if" { return IF; }
"then" { return THEN; }
"while" { return WHILE; }
"do" { return DO; }
"function" { return FUNCTION; }
"readInput" { return USERINPUT; }
"output" { return OUTPUT; }
......
function int count(int num) {
while num > 0 do {
output num
num = num - 1
}
return num
}
function float fcount(float fnum) {
while fnum > 0 do {
output fnum
fnum = fnum - 0.4
}
return fnum
}
output count(5)
output fcount(5.0)
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