From 2ab912139deab6050cb70cbe25bcdb47727f56e5 Mon Sep 17 00:00:00 2001 From: Rodrigo Steinmuller Wanderley Date: Wed, 21 Nov 2007 17:04:10 -0300 Subject: [PATCH] Comecando a ficar mais com a cara de Yacc. --- exps/simples_fun.pl | 8 ++++ parser.y | 114 +++++++++++++++++++++++++++++----------------------- scanner.lex | 4 +- vm.c | 4 +- 4 files changed, 75 insertions(+), 55 deletions(-) create mode 100644 exps/simples_fun.pl diff --git a/exps/simples_fun.pl b/exps/simples_fun.pl new file mode 100644 index 0000000..3818099 --- /dev/null +++ b/exps/simples_fun.pl @@ -0,0 +1,8 @@ +fun1 { + my $a; + $a = 2; +} + +my $a; +fun1(0, 1, $a); + diff --git a/parser.y b/parser.y index cb7b01e..440869b 100644 --- a/parser.y +++ b/parser.y @@ -11,38 +11,37 @@ #define YYDEBUG 1 -struct label { - char *id; - int for_goto; - int for_jmp_false; -}; - void yyerror(char *); int yylex(void); int scope = 0; -int nargs = 0; -int fun_begin = 0; extern FILE *yyin; - -struct label * -alloc_label(void) + +void +install(char *name) { - return calloc(1, sizeof(struct label)); + symtab_t *node; + + node = getsym(name, scope); + if (! node || node->scope < scope) { + node = putsym(name, scope, 0); + } else { + printf("%s jah estah definido neste escopo!\n", name); + } } void -install(char *name) +install_fun(char *name, int addr) { symtab_t *node; node = getsym(name, scope); if (! node || node->scope < scope) { - node = putsym(name, scope, gen_label()); + node = putsym(name, scope, addr); } else { printf("%s jah estah definido neste escopo!\n", name); } } - + void context_check(int op, char *name) { @@ -65,26 +64,38 @@ call_fun(char *name) if (! id) { printf("Fuancao nao declarado: %s\n", name); } else { - /* id->addr points to the GOTO instruction */ - gen_code(CALL, id->addr+1); + gen_code(CALL, id->addr); } } %} %union { - char *id; int ival; - struct label *label; + struct label { + int for_goto; + int for_jmp_false; + } label; + struct var { + char *id; + } var; + struct fun { + char *id; + int nargs; + int addr; + } fun; } %start program %token INTEGER -%token VARIABLE IDENTIFIER +%token IDENTIFIER +%token VARIABLE %token