Commit 2f4e0550 by Andrew Dahl

Fixes #13 - Stage 10 - Procedural Functions is working

Note: You're required to put an left curly brace after the paramters on the same line and the right curly brace right after the return statement with no newlines seperating it... I'll try to fix this, but for now, it works this way.
parent 3045417b
Showing with 35 additions and 11 deletions
......@@ -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,21 +51,30 @@ external_decl: NEWLINE
| error { yyerrok;}
;
function_define: FUNCTION ID LPAREN {
function_decl: FUNCTION ID LPAREN {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($2, Symbol::FUNCTION, Symbol::INTEGER));
symbol_table->push_back(new SymbolTable(level, symbol_table->at(level)));
level = symbol_table->size()-1;
symbol_table->at(level)->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
} argument_exp_list RPAREN EQUALS additive_exp NEWLINE {
Symbol* temp = symbol_table->at(level)->Find($2);
temp->setFunctionValue($5);
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
$$ = temp;
}
;
function_define: function_decl argument_exp_list RPAREN EQUALS additive_exp NEWLINE {
$1->setFunctionValue($2);
level = symbol_table->at(level)->getParentTable();
temp->setReturnType($8->getReturnType());
$1->setReturnType($5->getReturnType());
instructions->levelPop();
$$ = temp;
}
$$ = $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;}
......@@ -87,6 +96,10 @@ assignment_exp: additive_exp
}
;
statlist: stat { }
| statlist stat { }
;
stat: OUTPUT additive_exp NEWLINE {
instructions->addInstruction(new Instruction(Instruction::OUTPUT));
}
......
......@@ -46,6 +46,8 @@ slash [/]
equals [=]
lparen [(]
rparen [)]
lcurly [{]
rcurly [}]
letter [a-zA-Z]
identifier {letter}({letter}|{digit})*
comma [,]
......@@ -53,9 +55,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 +66,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 */ }
......
......@@ -4,7 +4,13 @@ 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