Skip to content

Commit

Permalink
Prevent printing function returns on function decision execution mode…
Browse files Browse the repository at this point in the history
… in interactive mode
  • Loading branch information
mertyildiran committed Apr 27, 2020
1 parent c80976e commit b14ff02
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
36 changes: 18 additions & 18 deletions chaos.y
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ function:
| T_VAR_NUMBER T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_NUMBER); }
| T_VAR_STRING T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_STRING); }
| T_VAR_ANY T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_ANY); }
| T_VAR_LIST T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_LIST); }
| T_VAR_LIST T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_LIST); }
| T_VAR_DICT T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_DICT); }
| T_VOID T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_VOID); }
| T_PRINT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($2, NULL); printFunctionReturn($2, NULL, "\n", false, true); } free($2); }
| T_ECHO T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($2, NULL); printFunctionReturn($2, NULL, "", false, true); } free($2); }
| T_PRETTY T_PRINT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($3, NULL); printFunctionReturn($3, NULL, "\n", true, true); } free($3); }
| T_PRETTY T_ECHO T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($3, NULL); printFunctionReturn($3, NULL, "", true, true); } free($3); }
| T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($1, NULL); if (is_interactive && !isFunctionType($1, NULL, K_VOID)) printFunctionReturn($1, NULL, "\n", false, false); } free($1); }
| T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($1, NULL); if (is_interactive && !isFunctionType($1, NULL, K_VOID) && !inject_mode && !decision_execution_mode) printFunctionReturn($1, NULL, "\n", false, false); } free($1); }
| T_PRINT T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($4, $2); printFunctionReturn($4, $2, "\n", false, true); } free($4); free($2); }
| T_ECHO T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($4, $2); printFunctionReturn($4, $2, "", false, true); } free($4); free($2); }
| T_PRETTY T_PRINT T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($5, $3); printFunctionReturn($5, $3, "\n", true, true); } free($5); free($3); }
| T_PRETTY T_ECHO T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($5, $3); printFunctionReturn($5, $3, "", true, true); } free($5); free($3); }
| T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($3, $1); if (is_interactive && !isFunctionType($3, $1, K_VOID)) printFunctionReturn($3, $1, "\n", false, false); } free($3); free($1); }
| T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($3, $1); if (is_interactive && !isFunctionType($3, $1, K_VOID) && !inject_mode && !decision_execution_mode) printFunctionReturn($3, $1, "\n", false, false); } free($3); free($1); }
| error T_NEWLINE { if (is_interactive) { yyerrok; yyclearin; } }
;

Expand Down Expand Up @@ -160,7 +160,7 @@ function_parameters: T_VAR_STRING T_VAR { addFunctio
| function_parameters T_NEWLINE { }
;

function_parameters: T_VAR_LIST T_VAR { addFunctionParameter($2, K_LIST); }
function_parameters: T_VAR_LIST T_VAR { addFunctionParameter($2, K_LIST); }
| function_parameters T_COMMA function_parameters { }
| function_parameters T_NEWLINE { }
;
Expand Down Expand Up @@ -213,9 +213,9 @@ parser:
;

line: T_NEWLINE
| mixed_expression T_NEWLINE { if (is_interactive && isStreamOpen()) printf("%Lg\n", $1); }
| expression T_NEWLINE { if (is_interactive && isStreamOpen()) printf("%lld\n", $1); }
| variable T_NEWLINE { if ($1[0] != '\0' && is_interactive) { printSymbolValueEndWithNewLine(getSymbol($1), false, false); free($1); } }
| mixed_expression T_NEWLINE { if (is_interactive && isStreamOpen() && !inject_mode) printf("%Lg\n", $1); }
| expression T_NEWLINE { if (is_interactive && isStreamOpen() && !inject_mode) printf("%lld\n", $1); }
| variable T_NEWLINE { if ($1[0] != '\0' && is_interactive && !inject_mode) { printSymbolValueEndWithNewLine(getSymbol($1), false, false); free($1); } }
| loop T_NEWLINE { }
| quit T_NEWLINE { }
| T_PRINT print T_NEWLINE { }
Expand Down Expand Up @@ -500,12 +500,12 @@ variable: T_VAR { $$ = $1; }
| variable T_EQUAL mixed_expression { updateSymbolFloat($1, $3); $$ = ""; }
| variable T_EQUAL expression { updateSymbolFloat($1, $3); $$ = ""; }
| variable T_EQUAL boolean_expression { updateSymbolBool($1, $3); $$ = ""; }
| variable T_EQUAL liststart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
| variable T_EQUAL liststart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
| variable T_EQUAL dictionarystart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
| variable T_EQUAL T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($3, NULL); updateSymbolByClonningFunctionReturn($1, $3, NULL); } else { free($1); free($3); } $$ = ""; }
| variable T_EQUAL T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($5, $3); updateSymbolByClonningFunctionReturn($1, $5, $3); } else { free($1); free($3); free($5); } $$ = ""; }
| T_RETURN variable { returnSymbol($2); $$ = ""; }
| variable_complex_element { if (is_interactive) { printSymbolValueEndWithNewLine(getComplexElementBySymbolId(variable_complex_element, variable_complex_element_symbol_id), false, false); $$ = ""; } else { yyerror("Syntax error"); } }
| variable_complex_element { if (is_interactive && !inject_mode) { printSymbolValueEndWithNewLine(getComplexElementBySymbolId(variable_complex_element, variable_complex_element_symbol_id), false, false); $$ = ""; } else { yyerror("Syntax error"); } }
| variable_complex_element T_EQUAL T_TRUE { updateComplexElementBool($3); $$ = ""; }
| variable_complex_element T_EQUAL T_FALSE { updateComplexElementBool($3); $$ = ""; }
| variable_complex_element T_EQUAL T_STRING { updateComplexElementString($3); $$ = ""; }
Expand All @@ -514,7 +514,7 @@ variable: T_VAR { $$ = $1; }
| variable_complex_element T_EQUAL mixed_expression { updateComplexElementFloat($3); $$ = ""; }
| variable_complex_element T_EQUAL expression { updateComplexElementFloat($3); $$ = ""; }
| variable_complex_element T_EQUAL boolean_expression { updateComplexElementBool($3); $$ = ""; }
| variable_complex_element T_EQUAL liststart { updateComplexElementComplex(); $$ = ""; }
| variable_complex_element T_EQUAL liststart { updateComplexElementComplex(); $$ = ""; }
| variable_complex_element T_EQUAL dictionarystart { updateComplexElementComplex(); $$ = ""; }
| variable_complex_element T_EQUAL T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($3, NULL); updateComplexSymbolByClonningFunctionReturn($3, NULL); } else { free($3); } $$ = ""; }
| variable_complex_element T_EQUAL T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($5, $3); updateComplexSymbolByClonningFunctionReturn($5, $3); } else { free($3); free($5); } $$ = ""; }
Expand All @@ -530,9 +530,9 @@ variable: { }
| T_VAR_BOOL T_VAR T_EQUAL boolean_expression { addSymbolBool($2, $4); $$ = ""; }
| T_VAR_BOOL T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($2, K_BOOL, $4, K_ANY); $$ = ""; }
| T_VAR_BOOL T_VAR T_EQUAL T_VAR left_right_bracket { createCloneFromComplexElement($2, K_BOOL, $4, K_ANY); $$ = ""; }
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_BOOL); $$ = ""; }
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_BOOL); $$ = ""; }
| T_VAR_BOOL T_VAR_DICT T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_DICT, $5, K_BOOL); $$ = ""; }
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_BOOL); $$ = ""; free($3); }
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_BOOL); $$ = ""; free($3); }
| T_VAR_BOOL T_VAR_DICT T_VAR T_EQUAL dictionarystart { finishComplexMode($3, K_BOOL); $$ = ""; free($3); }
| T_VAR_BOOL T_VAR T_EQUAL T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($4, NULL); createCloneFromFunctionReturn($2, K_BOOL, $4, NULL, K_ANY); } else { free($2); free($4); } $$ = ""; }
| T_VAR_BOOL T_VAR T_EQUAL T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($6, $4); createCloneFromFunctionReturn($2, K_BOOL, $6, $4, K_ANY); } else { free($2); free($4); free($6); } $$ = ""; }
Expand All @@ -541,9 +541,9 @@ variable: { }
variable: { }
| T_VAR_NUMBER T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($2, K_NUMBER, $4, K_ANY); $$ = ""; }
| T_VAR_NUMBER T_VAR T_EQUAL T_VAR left_right_bracket { createCloneFromComplexElement($2, K_NUMBER, $4, K_ANY); $$ = ""; }
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_NUMBER); $$ = ""; }
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_NUMBER); $$ = ""; }
| T_VAR_NUMBER T_VAR_DICT T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_DICT, $5, K_NUMBER); $$ = ""; }
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_NUMBER); $$ = ""; free($3); }
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_NUMBER); $$ = ""; free($3); }
| T_VAR_NUMBER T_VAR_DICT T_VAR T_EQUAL dictionarystart { finishComplexMode($3, K_NUMBER); $$ = ""; free($3); }
| T_VAR_NUMBER T_VAR T_EQUAL mixed_expression { addSymbolFloat($2, $4); $$ = ""; }
| T_VAR_NUMBER T_VAR T_EQUAL expression { addSymbolFloat($2, $4); $$ = ""; }
Expand All @@ -555,9 +555,9 @@ variable: { }
| T_VAR_STRING T_VAR T_EQUAL T_STRING { addSymbolString($2, $4); $$ = ""; }
| T_VAR_STRING T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($2, K_STRING, $4, K_ANY); $$ = ""; }
| T_VAR_STRING T_VAR T_EQUAL T_VAR left_right_bracket { createCloneFromComplexElement($2, K_STRING, $4, K_ANY); $$ = ""; }
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_STRING); $$ = ""; }
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_STRING); $$ = ""; }
| T_VAR_STRING T_VAR_DICT T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_DICT, $5, K_STRING); $$ = ""; }
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_STRING); $$ = ""; free($3); }
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_STRING); $$ = ""; free($3); }
| T_VAR_STRING T_VAR_DICT T_VAR T_EQUAL dictionarystart { finishComplexMode($3, K_STRING); $$ = ""; free($3); }
| T_VAR_STRING T_VAR T_EQUAL T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($4, NULL); createCloneFromFunctionReturn($2, K_STRING, $4, NULL, K_ANY); } else { free($2); free($4); } $$ = ""; }
| T_VAR_STRING T_VAR T_EQUAL T_VAR T_DOT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($6, $4); createCloneFromFunctionReturn($2, K_STRING, $6, $4, K_ANY); } else { free($2); free($4); free($6); } $$ = ""; }
Expand Down Expand Up @@ -770,7 +770,7 @@ quit: { }
freeEverything();
exit(E_SUCCESS);
}
| T_QUIT expression T_NEWLINE {
| T_QUIT expression T_NEWLINE {
if (is_interactive) {
print_bye_bye();
} else {
Expand All @@ -779,7 +779,7 @@ quit: { }
freeEverything();
exit($2);
}
| T_QUIT T_VAR T_NEWLINE {
| T_QUIT T_VAR T_NEWLINE {
long long code = getSymbolValueInt($2);
if (is_interactive) {
print_bye_bye();
Expand Down
3 changes: 3 additions & 0 deletions functions/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bool interactive_shell_function_error_absorbed = false;
extern int yyparse();

int reset_line_no_to = 0;
bool decision_execution_mode = false;

void startFunction(char *name, enum Type type) {
if (is_interactive) {
Expand Down Expand Up @@ -525,7 +526,9 @@ void executeDecision(_Function* function) {
if (symbol->value.b) {
function_buffer = strcat_ext(function_buffer, function->decision_functions.arr[i]);
function_buffer = strcat_ext(function_buffer, "\n");
decision_execution_mode = true;
injectCode(function_buffer, INIT_PROGRAM);
decision_execution_mode = false;
is_decision_made = true;
break;
}
Expand Down
1 change: 1 addition & 0 deletions functions/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ _Function* scope_override;
_Function* decision_mode;
_Function* decision_expression_mode;
_Function* decision_function_mode;
bool decision_execution_mode;
Symbol* decision_symbol_chain;
char *decision_buffer;

Expand Down
35 changes: 31 additions & 4 deletions tests/shell/function.kaos
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import tests.shell.modules.module1

void def f1()
void def fa()
print "hey"
end

asdasd1()

f1()
fa()

asdasd2()

num def f2()
num def fb()
num a = 5
return a
end
f2()
fb()

// Print the function return on function calls from modules
module1.sub(7, 3)

// Test for not printing function returns on function decision execution mode
num def f1()
num a = 101
return a
end

num def f2()
num b = 102
return b
end

num def f3()
num c = 103
return c
end

num def add(num x, num y)
num z = x + y
end {
z == 8 : f1(),
z > 10 : f2(),
default : f3()
}

add(3, 5)
print add(3, 5)
2 changes: 2 additions & 0 deletions tests/shell/function.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Undefined function: asdasd2
Absorbed by Interactive Shell
5
4
101
101

0 comments on commit b14ff02

Please sign in to comment.