Commit 28fdf4b3 by Andrew Dahl

Merge branch 'dev_stage10'

Conflicts:
	lib/instructionList.cpp
	lib/parser.y
parents 9fa7ae35 f29c0d30
......@@ -29,12 +29,12 @@
char* name;
}
%token NEWLINE PLUS MINUS SLASH ASTERISK LPAREN RPAREN EQUALS USERINPUT OUTPUT FUNCTION COMMA
%token NEWLINE PLUS MINUS SLASH ASTERISK LPAREN RPAREN EQUALS USERINPUT OUTPUT FUNCTION COMMA LCURLY RCURLY RETURN
%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
%type <sym> stat additive_exp mult_exp primary_exp assignment_exp function_define argument_exp_list func_call_args function_decl
%% /* Grammer Rules and Actions */
......@@ -51,7 +51,7 @@ external_decl: NEWLINE
| error { yyerrok; YYABORT;}
;
function_define: FUNCTION ID LPAREN {
function_decl: FUNCTION ID LPAREN {
if(symbol_table->at(level)->ExistsInCurrentLevel($2)) {
yyerror(symbol_table,instructions,"Redeclaration of function");
YYABORT;
......@@ -66,16 +66,25 @@ function_define: FUNCTION ID LPAREN {
symbol_table->at(level)->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
$$ = temp;
}
}
} argument_exp_list RPAREN EQUALS additive_exp NEWLINE {
Symbol* temp = symbol_table->at(level)->Find($2);
temp->setFunctionValue($5);
level = symbol_table->at(level)->getParentTable();
temp->setReturnType($8->getReturnType());
instructions->levelPop();
$$ = temp;
}
}
;
function_define: function_decl argument_exp_list RPAREN EQUALS additive_exp NEWLINE {
$1->setFunctionValue($2);
level = symbol_table->at(level)->getParentTable();
$1->setReturnType($5->getReturnType());
instructions->levelPop();
$$ = $1;
}
| function_decl argument_exp_list RPAREN LCURLY statlist RETURN additive_exp RCURLY {
$1->setFunctionValue($2);
level = symbol_table->at(level)->getParentTable();
instructions->levelPop();
$$ = $1;
}
;
argument_exp_list: { $$ = NULL;}
......@@ -102,6 +111,10 @@ assignment_exp: additive_exp
}
;
statlist: stat { }
| statlist stat { }
;
stat: OUTPUT additive_exp NEWLINE {
instructions->addInstruction(new Instruction(Instruction::OUTPUT));
}
......
......@@ -32,8 +32,9 @@ extern YYSTYPE yylval;
%option noyywrap
delim [ \t]
newline [\n]
newline [\n]+
whitesp {delim}+
whiteall ({whitesp}|{newline})?*
digit [0-9]
nonzeronumber [1-9]{digit}*
zero [0]
......@@ -46,6 +47,8 @@ slash [/]
equals [=]
lparen [(]
rparen [)]
lcurly {whiteall}?[{]{whiteall}?
rcurly {whiteall}?[}]{whiteall}?
letter [a-zA-Z]
identifier {letter}({letter}|{digit})*
comma [,]
......@@ -53,9 +56,10 @@ comma [,]
%%
{number} { yylval.inum = atoi(yytext); return NUMBER; }
{float} { yylval.fnum = atof(yytext); return FLOAT; }
"function" { return FUNCTION;}
"function" { return FUNCTION; }
"readInput" { return USERINPUT; }
"output" { return OUTPUT; }
"return" { return RETURN; }
{identifier} { yylval.name = strdup(yytext); return ID; }
{plus} { return PLUS; }
{minus} { return MINUS; }
......@@ -63,6 +67,8 @@ comma [,]
{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 */ }
......
......@@ -11,7 +11,7 @@
#include <vector>
#include "../include/symbolTable.h"
#include "../include/instructionList.h"
int yyparse (vector<SymbolTable*> symbol_table, InstructionList* instructions);
int yyparse (vector<SymbolTable*>* symbol_table, InstructionList* instructions);
#endif
using namespace std;
......@@ -27,10 +27,10 @@ int main(int argc, char *argv[]) {
}
#else
InstructionList* instructions = new InstructionList();
vector<SymbolTable*> symbol_table;
symbol_table.push_back(new SymbolTable());
symbol_table[0]->Insert(new Symbol("readInput", Symbol::INPUT, Symbol::INTEGER));
symbol_table.push_back(new SymbolTable(0, symbol_table[0]));
vector<SymbolTable*>* symbol_table = new vector<SymbolTable*>();
symbol_table->push_back(new SymbolTable());
symbol_table->at(0)->Insert(new Symbol("readInput", Symbol::INPUT, Symbol::INTEGER));
symbol_table->push_back(new SymbolTable(0, symbol_table->at(0)));
yyparse(symbol_table, instructions);
#endif
return 0;
......
a=readInput
b=readInput + 1.0
c=readInput + 2.5
function func(a,b,c,d)=a+b+c
function func2()=10
function func1(a,b)=func(a,b,1,readInput) - func2()
function outFunc(a,b,c)
{
output a
output b
output c
return a+b+c
}
output readInput + func(func1(readInput,a),2,func2(),0)
d=outFunc(1,2,3)
output a
output b
output c
output d
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