Added function arguments

This commit is contained in:
Morten Delenk 2016-07-02 17:11:21 +02:00
parent 6ed5d05993
commit ffce78b24d
3 changed files with 34 additions and 17 deletions

View file

@ -1,8 +1,8 @@
CC=gcc CC=gcc
CXX=g++ CXX=g++
LD=g++ LD=g++
CFLAGS=-O3 -march=native -fstack-protector=strong CFLAGS=-O3 -march=native -fstack-protector-strong -gdwarf-2
CXXFLAGS=-O3 -march=native-fstack-protector=strong CXXFLAGS=-O3 -march=native -fstack-protector-strong -gdwarf-2
LEX=flex LEX=flex
YACC=bison YACC=bison
all: mumei all: mumei
@ -11,10 +11,10 @@ mumei: mumei.tab.o lex.yy.o
$(LD) -o $@ $^ -lfl $(LD) -o $@ $^ -lfl
lex.yy.o: lex.yy.c lex.yy.o: lex.yy.c
$(CXX) $(CPPFLAGS) -c -o $@ $^ $(CXX) $(CXXFLAGS) -c -o $@ $^
mumei.tab.o: mumei.tab.c mumei.tab.o: mumei.tab.c
$(CXX) $(CPPFLAGS) -c -o $@ $^ $(CXX) $(CXXFLAGS) -c -o $@ $^
lex.yy.c: mumei.tab.c lex.yy.c: mumei.tab.c
$(LEX) mumei.l $(LEX) mumei.l

34
mumei.y
View file

@ -23,6 +23,19 @@ int getVarID(string name) {
nameTable.push_back(name); nameTable.push_back(name);
return nameTable.size()-1; return nameTable.size()-1;
} }
vector<string> argTable;
int getArgID(string name) {
auto it=argTable.end();
for(auto it=argTable.begin(); it!=argTable.end(); it++) {
if(argTable[it-argTable.begin()]==name) {
return (int)(it-argTable.begin())-(int)(argTable.size())-1;
}
}
return 0;
}
void createArg(string arg) {
argTable.push_back(arg);
}
ofstream out; ofstream out;
%} %}
%union { %union {
@ -48,13 +61,17 @@ program:
fh function ef program fh function ef program
| fh function ef | fh function ef
fh: fh:
FUNCTION VARNAME {out << $2 << ":\n\tbpget\n\tspget\n\tbpset" << endl;} FUNCTION VARNAME LPARENS args RPARENS {out << $2 << ":\n\tbpget\n\tspget\n\tbpset" << endl;}
| FUNCTION VARNAME LPARENS RPARENS {out << $2 << ":\n\tbpget\n\tspget\n\tbpset" << endl;}
args:
VARNAME COMMA args {createArg($1);}
| VARNAME {createArg($1);}
ef: ef:
END {out << "\tbpget\n\tspset\n\tbpset\n\tret"<<endl;} END {out << "\tbpget\n\tspset\n\tbpset\n\tret"<<endl;argTable.clear();}
function: function:
VARNAME EQ exp program {out << "\t[ci:2] store "<<getVarID($1)*4 << endl;} VARNAME EQ exp function {out << "\t[ci:2] store "<<getVarID($1)*4 << endl;}
| VARNAME EQ exp {out << "\t[ci:2] store "<<getVarID($1)*4 << endl;} | VARNAME EQ exp {out << "\t[ci:2] store "<<getVarID($1)*4 << endl;}
| exp program {out << "\tdrop" << endl;} | exp function {out << "\tdrop" << endl;}
| exp {out << "\tdrop" << endl;} | exp {out << "\tdrop" << endl;}
exp: exp:
factor factor
@ -66,19 +83,18 @@ factor:
| factor DIV factor {out << "\tdiv" << endl;} | factor DIV factor {out << "\tdiv" << endl;}
num: num:
INT { out << "\tpush "<< $1 << endl;} INT { out << "\tpush "<< $1 << endl;}
| VARNAME LPARENS function RPARENS {out << "\tcpget\n\tjmp @" << $1 << "\n\t[ci:2] load " << getVarID($1) << endl;} | VARNAME LPARENS fargs RPARENS {out << "\tcpget\n\tjmp @" << $1 << "\n\t[ci:2] load " << getVarID($1) << endl;}
| VARNAME LPARENS RPARENS {out << "\tcpget\n\tjmp @" << $1 << "\n\t[ci:2] load " << getVarID($1) << endl;} | VARNAME LPARENS RPARENS {out << "\tcpget\n\tjmp @" << $1 << "\n\t[ci:2] load " << getVarID($1) << endl;}
| VARNAME { out << "\t[ci:2]load "<<getVarID($1)*4<<endl;} | VARNAME { char* arg=$1; int id; if((id=getArgID(arg))==0) {out << "\t[ci:2]load "<<getVarID(arg)*4<<endl;} else {out<< "\tget "<< id << endl;}}
| LPARENS exp RPARENS | LPARENS exp RPARENS
function: fargs:
exp COMMA function exp COMMA fargs
| exp | exp
%% %%
int main(int argc, char** argv) { int main(int argc, char** argv) {
int pid; int pid;
mkfifo("tmp.a", 0660); mkfifo("tmp.a", 0660);
if(!(pid=fork())) { if(!(pid=fork())) {
//sleep(1);
system("as -o tmp.o crt0.asm tmp.a libmtg.asm"); system("as -o tmp.o crt0.asm tmp.a libmtg.asm");
return 0; return 0;
} }

View file

@ -1,6 +1,7 @@
FUNCTION main FUNCTION main()
this=is-a(new,and,sh+o+rt)*test a(5)
END END
FUNCTION a FUNCTION a(i)
puti(5) puti(i)
a(i+1)
END END