--- a/c2iec.y +++ b/c2iec.y @@ -82,6 +82,12 @@ char s_szSymTab[SYM_COUNT][SYM_SIZE]; void addSym(char *szSym); int isSym(char *szSym); + +// define function name of current context +static char s_szFuncName[SYM_SIZE]; +void setFunctionName(char *szName); +char *getFunctionName(void); + // lex/yacc functions extern int yylex (void); @@ -532,6 +538,7 @@ { sprintf($<str>$, "FUNCTION %s:%%s;\n",$<str>1); fifo_push(decl_func, $<str>$); + setFunctionName($<str>1); strcpy($<str>$, $<str>1); // strcpy($<str>$, ""); } @@ -539,6 +546,7 @@ { sprintf($<str>$, "FUNCTION %s:%%s;\n",$<str>1); fifo_push(decl_func, $<str>$); + setFunctionName($<str>1); strcpy($<str>$, $<str>1); // strcpy($<str>$, ""); } @@ -546,6 +554,7 @@ { sprintf($<str>$, "FUNCTION %s:%%s;\n",$<str>1); fifo_push(decl_func, $<str>$); + setFunctionName($<str>1); strcpy($<str>$, ""); // strcpy($<str>$, ""); } @@ -680,7 +689,9 @@ selection_statement : IF '(' expression ')' statement + { sprintf($<str>$, "\nIF %s\n%s\nEND_IF\n",$<str>3, $<str>5); } | IF '(' expression ')' statement ELSE statement + { sprintf($<str>$, "\nIF %s\n%s\nELSE\n%s\nEND_IF\n",$<str>3, $<str>5, $<str>7); } | SWITCH '(' expression ')' statement { sprintf($<str>$, "(*switch*)WHILE TRUE DO\nCASE %s OF\n %s\nEND_CASE\n\n(*switch*)EXIT;\n(*switch*)END_WHILE",$<str>3, $<str>5); } ; @@ -704,9 +715,14 @@ | BREAK ';' { sprintf($<str>$, "EXIT;\n"); } | RETURN ';' - { sprintf($<str>$, " %s;\n",$<str>1); } + { + sprintf($<str>$, " RETURN;\n"); + } | RETURN expression ';' - { sprintf($<str>$, " %s %s;\n",$<str>1, $<str>2); } + { + sprintf($<str>$, " %s := (%s);\n", getFunctionName(), $<str>2); + sprintf(TAIL($<str>$), " RETURN;\n"); + } ; translation_unit @@ -953,3 +969,13 @@ return 0; } + +void setFunctionName(char *szName) +{ + strcpy(s_szFuncName, szName); +} + +char *getFunctionName(void) +{ + return s_szFuncName; +}