Commit 7efc4488 by Andrew Dahl

Fixes #21 - C Syntax works

parent 8c0becd9
......@@ -25,6 +25,7 @@ class Instruction {
SUB, /* Integer: SUB Dest, Src Float: FSUB Dest */
DIV, /* Integer: IDIV Dest Float: FDIV Dest */
MUL, /* Integer: IMUL Dest Float: FMUL Dest */
MOD, /* */
INPUT, /* We have a Macro for this: getint */
OUTPUT, /* We have a Macro for this: pint or pfloat */
PUSH, /* Integer: PUSH Src Float: FLD Src */
......@@ -34,8 +35,12 @@ class Instruction {
JE, /* Short Jump if equal */
JG, /* Short Jump if greater than */
JL, /* Short Jump if less than */
JGE, /* */
JLE, /* */
JA, /* */
JB, /* */
JAE, /* */
JBE, /* */
JNE, /* Short Jump if not zero TEMPORARY!!!! */
CALL, /* CALL Arg */
FUNCTION_START, /* Used to differentiate blocks from functions */
......
......@@ -56,8 +56,8 @@ class Symbol {
int getStackAddress() const;
void setScopeAddress(int address);
int getScopeAddress() const;
void setGlobal(bool global);
bool getGlobal() const;
void setConstant(bool constant);
bool getConstant() const;
string toString() const;
......@@ -69,7 +69,7 @@ private:
Symbol* f; // These will be variables for the function
int stack_address;
int scope_address;
bool is_global;
bool is_constant;
};
#endif /* SYMBOL_H_ */
......@@ -26,7 +26,6 @@ public:
int Count() const;
Symbol* Insert(Symbol *entry); // Variable or Function insertion
Symbol* InsertGlobal(Symbol *entry);
Symbol* Insert(const int value); // Constant Value insertion
Symbol* Insert(const float value); // Constant Value insertion
Symbol* FindConstant(const int value);
......
......@@ -197,13 +197,13 @@ string InstructionList::toString()
{
if(symArr[i]->getReturnType() == Symbol::FLOAT)
{
oss << "PUSH DWORD [ESI+" << varAddr + 8 << "]\n";
oss << "PUSH DWORD [ESI+" << varAddr + 4 << "]\n";
oss << "PUSH DWORD [ESI+" << varAddr << "]\n";
varAddr += 8;
}
else
{
oss << "PUSH DWORD [ESI+" << varAddr << "]\n";
oss << "PUSH DWORD [ESI+" << varAddr + 4 << "]\n";
varAddr += 4;
}
}
......@@ -293,6 +293,22 @@ string InstructionList::toString()
this->stackPush(Symbol::INTEGER);
}
break;
case Instruction::MOD:
oss << this->checkTopTwoTypesToString();
if(this->getTopTwoTypes() == Symbol::FLOAT)
{
oss << "CAN'T DO THIS!!!";
}
else
{
oss << "POP EBX\n" //Load EBX with top of stack
<< "POP EAX\n" //Load EAX with top of stack
<< "CDQ\n" //Sign extend EAX to EDX, thus making a qword
<< "IDIV EBX\n" //Divide EAX by EBX
<< "PUSH EDX\n"; //Push EAX to top of stack
this->stackPush(Symbol::INTEGER);
}
break;
case Instruction::INPUT:
oss << "getint\n";
this->stackPush(Symbol::INTEGER);
......@@ -449,28 +465,44 @@ string InstructionList::toString()
}
break;
case Instruction::JNE:
oss << "JE " << temp->getArg1().reg << "end\n";
oss << "JE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JE:
oss << "JNE " << temp->getArg1().reg << "end\n";
oss << "JNE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JG:
oss << "JLE " << temp->getArg1().reg << "end\n";
oss << "JLE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JL:
oss << "JGE " << temp->getArg1().reg << "end\n";
oss << "JGE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JGE:
oss << "JL " << temp->getArg1().reg << "end\n";
break;
case Instruction::JLE:
oss << "JG " << temp->getArg1().reg << "end\n";
break;
case Instruction::JA:
oss << "JBE " << temp->getArg1().reg << "end\n";
oss << "JBE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JB:
oss << "JAE " << temp->getArg1().reg << "end\n";
oss << "JAE " << temp->getArg1().reg << "end\n";
break;
case Instruction::JAE:
oss << "JB " << temp->getArg1().reg << "end\n";
break;
case Instruction::JBE:
oss << "JA " << temp->getArg1().reg << "end\n";
break;
case Instruction::CALL:
oss << "PUSH ESI\n"
oss << "MOV EBX, [EDI-" << temp->getArg1().sym->getScopeAddress() << "]\n"
<< "PUSH EBX\n"
<< "PUSH ESI\n"
<< "MOV ESI, ESP\n"
<< "CALL "<< temp->getArg1().sym->getLexeme() << "\n"
<< "POP ESI\n"
<< "POP EBX\n"
<< "MOV [EDI-" << temp->getArg1().sym->getScopeAddress() << "], EBX\n"
<< "ADD ESP, " << temp->getArg1().sym->getArgumentsValue(true) << endl;
this->stackPop(temp->getArg1().sym->getArguments(true));
if(temp->getArg1().sym->getReturnType() == Symbol::FLOAT)
......
......@@ -4,86 +4,52 @@
#include <string.h>
#include <stdlib.h>
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 214 of yacc.c */
#line 20 "lib/parser.y"
float fnum;
int inum;
Symbol* sym;
char* name;
%}
%option yylineno
%option noyywrap
%option caseless
/* Line 214 of yacc.c */
#line 146 "lib/lex.yy.c"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
digits [[:digit:]]+
number {digits}
float {digits}[.]{digits}
identifier [[:alpha:]]([[:alpha:]]|[[:digit:]])*
extern YYSTYPE yylval;
%x comment
%}
%%
%option noyywrap
delim [ \t]
newline [\n]+
whitesp {delim}+
whiteall ({whitesp}|{newline})?*
digit [0-9]
nonzeronumber [1-9]{digit}*
zero [0]
number {zero}|{nonzeronumber}
float {number}[.]{digit}+
lessthan [<]
greaterthan [>]
equalto [=][=]
plus [+]
minus [-]
asterisk [*]
slash [/]
equals [=][^=]
lparen [(]
rparen [)]
lcurly {whiteall}?[{]{whiteall}?
rcurly [}]{whiteall}?
letter [a-zA-Z]
identifier {letter}({letter}|{digit})*
comma [,]
\/\* { BEGIN(comment); }
<comment>\*\/ { BEGIN(INITIAL); }
<comment>([^\n\*\/<<EOF>>]|e|o|f|\<)+ { /* Ignore Comments */ }
[ \t\n\s] { /* Ignore Whitespace */ }
%%
{number} { yylval.inum = atoi(yytext); return NUMBER; }
{float} { yylval.fnum = atof(yytext); return FLOAT; }
"if" { return IF; }
"then" { return THEN; }
"while" { return WHILE; }
"do" { return DO; }
"function" { return FUNCTION; }
"while" { return WHILE; }
"readInput" { return USERINPUT; }
"output" { return OUTPUT; }
"return" { return RETURN; }
"global" { return GLOBAL; }
"const" { return CONST; }
"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; }
{asterisk} { return ASTERISK; }
{lparen} { return LPAREN; }
{rparen} { return RPAREN; }
{lcurly} { return LCURLY; }
{rcurly} { return RCURLY; }
{equals} { return EQUALS; }
{newline} { return NEWLINE; }
{whitesp} { /* No action and no return */ }
{comma} { return COMMA; }
\< { return LESSTHAN; }
\> { return GREATERTHAN; }
\<= { return LESSTHANEQUAL; }
\>= { return GREATERTHANEQUAL; }
!= { return NOTEQUAL; }
== { return EQUALTO; }
\+ { return PLUS; }
- { return MINUS; }
\/ { return SLASH; }
\* { return ASTERISK; }
\( { return LPAREN; }
\) { return RPAREN; }
\{ { return LCURLY; }
\} { return RCURLY; }
= { return EQUALS; }
, { return COMMA; }
; { return SEMICOLON; }
% { return MOD; }
......@@ -247,14 +247,14 @@ int Symbol::getScopeAddress() const
return this->scope_address;
}
void Symbol::setGlobal(bool global)
void Symbol::setConstant(bool constant)
{
this->is_global = global;
this->is_constant = constant;
}
bool Symbol::getGlobal() const
bool Symbol::getConstant() const
{
return this->is_global;
return this->is_constant;
}
string Symbol::toString() const
......
......@@ -32,8 +32,6 @@ Symbol* SymbolTable::Insert(Symbol *entry)
if(it == this->table.end())
{
if(this->parent_table_num == -1)
entry->setGlobal(true);
this->table.insert( std::pair<string,Symbol*>(entry->getLexeme(),entry));
if(entry->getType() == Symbol::VARIABLE)
{
......@@ -50,12 +48,6 @@ Symbol* SymbolTable::Insert(Symbol *entry)
return entry;
}
Symbol* SymbolTable::InsertGlobal(Symbol *entry)
{
this->table.insert( std::pair<string,Symbol*>(entry->getLexeme(),entry));
return entry;
}
Symbol* SymbolTable::Insert(const int value)
{
return this->Insert(new Symbol(this->getLexeme(value), value));
......
const int a = 0;
const int b = 10;
int func(int arg8, float farg8) {
int c = 40;
output a;
output b;
output c;
output arg8;
output farg8;
return 200;
}
int func1(int arg12, float farg12) {
int a = 70;
int b = 80;
output a;
output b;
output arg12;
output farg12;
return 400;
}
int count(int num) {
while (num > 0) {
output num;
num = num - 1;
}
return num;
}
float fcount(float fnum) {
while (fnum > 0) {
output fnum;
fnum = fnum - 0.4;
}
return fnum;
}
int fact(int facnum) {
if (facnum > 1) {
facnum = facnum * fact(facnum-1);
}
return facnum;
}
int compare(int a, int b) {
if(a == b) {
output 10;
}
if(a != b) {
output 20;
}
if(a > b) {
output 30;
}
if(a >= b) {
output 40;
}
if(a < b) {
output 50;
}
if(a <= b) {
output 60;
}
if(a % b == 1) {
output 70;
}
return 90;
}
float fcompare(float a, float b) {
if(a == b) {
output 10.5;
}
if(a != b) {
output 20.5;
}
if(a > b) {
output 30.5;
}
if(a >= b) {
output 40.5;
}
if(a < b) {
output 50.5;
}
if(a <= b) {
output 60.5;
}
return 90.0;
}
int main() {
int c = 600;
output func(50, 60.0);
output func1(90, 100.0);
output c;
output count(5);
output fcount(5.0);
output fact(5);
output compare(1,2);
output compare(3,2);
output compare(2,2);
output fcompare(1.0,1.1);
output fcompare(1.1,1.0);
output fcompare(1.1,1.1);
return 20;
}
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