Skip to content

Commit b14ff02

Browse files
committed
Prevent printing function returns on function decision execution mode in interactive mode
1 parent c80976e commit b14ff02

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

chaos.y

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ function:
117117
| T_VAR_NUMBER T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_NUMBER); }
118118
| T_VAR_STRING T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_STRING); }
119119
| T_VAR_ANY T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_ANY); }
120-
| T_VAR_LIST T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_LIST); }
120+
| T_VAR_LIST T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_LIST); }
121121
| T_VAR_DICT T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_DICT); }
122122
| T_VOID T_FUNCTION T_VAR function_parameters_start { startFunction($3, K_VOID); }
123123
| T_PRINT T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($2, NULL); printFunctionReturn($2, NULL, "\n", false, true); } free($2); }
124124
| T_ECHO T_VAR T_LEFT function_call_parameters_start { if (phase == PROGRAM) { callFunction($2, NULL); printFunctionReturn($2, NULL, "", false, true); } free($2); }
125125
| 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); }
126126
| 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); }
127-
| 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); }
127+
| 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); }
128128
| 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); }
129129
| 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); }
130130
| 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); }
131131
| 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); }
132-
| 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); }
132+
| 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); }
133133
| error T_NEWLINE { if (is_interactive) { yyerrok; yyclearin; } }
134134
;
135135

@@ -160,7 +160,7 @@ function_parameters: T_VAR_STRING T_VAR { addFunctio
160160
| function_parameters T_NEWLINE { }
161161
;
162162

163-
function_parameters: T_VAR_LIST T_VAR { addFunctionParameter($2, K_LIST); }
163+
function_parameters: T_VAR_LIST T_VAR { addFunctionParameter($2, K_LIST); }
164164
| function_parameters T_COMMA function_parameters { }
165165
| function_parameters T_NEWLINE { }
166166
;
@@ -213,9 +213,9 @@ parser:
213213
;
214214

215215
line: T_NEWLINE
216-
| mixed_expression T_NEWLINE { if (is_interactive && isStreamOpen()) printf("%Lg\n", $1); }
217-
| expression T_NEWLINE { if (is_interactive && isStreamOpen()) printf("%lld\n", $1); }
218-
| variable T_NEWLINE { if ($1[0] != '\0' && is_interactive) { printSymbolValueEndWithNewLine(getSymbol($1), false, false); free($1); } }
216+
| mixed_expression T_NEWLINE { if (is_interactive && isStreamOpen() && !inject_mode) printf("%Lg\n", $1); }
217+
| expression T_NEWLINE { if (is_interactive && isStreamOpen() && !inject_mode) printf("%lld\n", $1); }
218+
| variable T_NEWLINE { if ($1[0] != '\0' && is_interactive && !inject_mode) { printSymbolValueEndWithNewLine(getSymbol($1), false, false); free($1); } }
219219
| loop T_NEWLINE { }
220220
| quit T_NEWLINE { }
221221
| T_PRINT print T_NEWLINE { }
@@ -500,12 +500,12 @@ variable: T_VAR { $$ = $1; }
500500
| variable T_EQUAL mixed_expression { updateSymbolFloat($1, $3); $$ = ""; }
501501
| variable T_EQUAL expression { updateSymbolFloat($1, $3); $$ = ""; }
502502
| variable T_EQUAL boolean_expression { updateSymbolBool($1, $3); $$ = ""; }
503-
| variable T_EQUAL liststart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
503+
| variable T_EQUAL liststart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
504504
| variable T_EQUAL dictionarystart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
505505
| 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); } $$ = ""; }
506506
| 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); } $$ = ""; }
507507
| T_RETURN variable { returnSymbol($2); $$ = ""; }
508-
| variable_complex_element { if (is_interactive) { printSymbolValueEndWithNewLine(getComplexElementBySymbolId(variable_complex_element, variable_complex_element_symbol_id), false, false); $$ = ""; } else { yyerror("Syntax error"); } }
508+
| variable_complex_element { if (is_interactive && !inject_mode) { printSymbolValueEndWithNewLine(getComplexElementBySymbolId(variable_complex_element, variable_complex_element_symbol_id), false, false); $$ = ""; } else { yyerror("Syntax error"); } }
509509
| variable_complex_element T_EQUAL T_TRUE { updateComplexElementBool($3); $$ = ""; }
510510
| variable_complex_element T_EQUAL T_FALSE { updateComplexElementBool($3); $$ = ""; }
511511
| variable_complex_element T_EQUAL T_STRING { updateComplexElementString($3); $$ = ""; }
@@ -514,7 +514,7 @@ variable: T_VAR { $$ = $1; }
514514
| variable_complex_element T_EQUAL mixed_expression { updateComplexElementFloat($3); $$ = ""; }
515515
| variable_complex_element T_EQUAL expression { updateComplexElementFloat($3); $$ = ""; }
516516
| variable_complex_element T_EQUAL boolean_expression { updateComplexElementBool($3); $$ = ""; }
517-
| variable_complex_element T_EQUAL liststart { updateComplexElementComplex(); $$ = ""; }
517+
| variable_complex_element T_EQUAL liststart { updateComplexElementComplex(); $$ = ""; }
518518
| variable_complex_element T_EQUAL dictionarystart { updateComplexElementComplex(); $$ = ""; }
519519
| 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); } $$ = ""; }
520520
| 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); } $$ = ""; }
@@ -530,9 +530,9 @@ variable: { }
530530
| T_VAR_BOOL T_VAR T_EQUAL boolean_expression { addSymbolBool($2, $4); $$ = ""; }
531531
| T_VAR_BOOL T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($2, K_BOOL, $4, K_ANY); $$ = ""; }
532532
| T_VAR_BOOL T_VAR T_EQUAL T_VAR left_right_bracket { createCloneFromComplexElement($2, K_BOOL, $4, K_ANY); $$ = ""; }
533-
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_BOOL); $$ = ""; }
533+
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_BOOL); $$ = ""; }
534534
| T_VAR_BOOL T_VAR_DICT T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_DICT, $5, K_BOOL); $$ = ""; }
535-
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_BOOL); $$ = ""; free($3); }
535+
| T_VAR_BOOL T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_BOOL); $$ = ""; free($3); }
536536
| T_VAR_BOOL T_VAR_DICT T_VAR T_EQUAL dictionarystart { finishComplexMode($3, K_BOOL); $$ = ""; free($3); }
537537
| 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); } $$ = ""; }
538538
| 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); } $$ = ""; }
@@ -541,9 +541,9 @@ variable: { }
541541
variable: { }
542542
| T_VAR_NUMBER T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($2, K_NUMBER, $4, K_ANY); $$ = ""; }
543543
| T_VAR_NUMBER T_VAR T_EQUAL T_VAR left_right_bracket { createCloneFromComplexElement($2, K_NUMBER, $4, K_ANY); $$ = ""; }
544-
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_NUMBER); $$ = ""; }
544+
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_NUMBER); $$ = ""; }
545545
| T_VAR_NUMBER T_VAR_DICT T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_DICT, $5, K_NUMBER); $$ = ""; }
546-
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_NUMBER); $$ = ""; free($3); }
546+
| T_VAR_NUMBER T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_NUMBER); $$ = ""; free($3); }
547547
| T_VAR_NUMBER T_VAR_DICT T_VAR T_EQUAL dictionarystart { finishComplexMode($3, K_NUMBER); $$ = ""; free($3); }
548548
| T_VAR_NUMBER T_VAR T_EQUAL mixed_expression { addSymbolFloat($2, $4); $$ = ""; }
549549
| T_VAR_NUMBER T_VAR T_EQUAL expression { addSymbolFloat($2, $4); $$ = ""; }
@@ -555,9 +555,9 @@ variable: { }
555555
| T_VAR_STRING T_VAR T_EQUAL T_STRING { addSymbolString($2, $4); $$ = ""; }
556556
| T_VAR_STRING T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($2, K_STRING, $4, K_ANY); $$ = ""; }
557557
| T_VAR_STRING T_VAR T_EQUAL T_VAR left_right_bracket { createCloneFromComplexElement($2, K_STRING, $4, K_ANY); $$ = ""; }
558-
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_STRING); $$ = ""; }
558+
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_LIST, $5, K_STRING); $$ = ""; }
559559
| T_VAR_STRING T_VAR_DICT T_VAR T_EQUAL T_VAR { createCloneFromSymbolByName($3, K_DICT, $5, K_STRING); $$ = ""; }
560-
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_STRING); $$ = ""; free($3); }
560+
| T_VAR_STRING T_VAR_LIST T_VAR T_EQUAL liststart { finishComplexMode($3, K_STRING); $$ = ""; free($3); }
561561
| T_VAR_STRING T_VAR_DICT T_VAR T_EQUAL dictionarystart { finishComplexMode($3, K_STRING); $$ = ""; free($3); }
562562
| 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); } $$ = ""; }
563563
| 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); } $$ = ""; }
@@ -770,7 +770,7 @@ quit: { }
770770
freeEverything();
771771
exit(E_SUCCESS);
772772
}
773-
| T_QUIT expression T_NEWLINE {
773+
| T_QUIT expression T_NEWLINE {
774774
if (is_interactive) {
775775
print_bye_bye();
776776
} else {
@@ -779,7 +779,7 @@ quit: { }
779779
freeEverything();
780780
exit($2);
781781
}
782-
| T_QUIT T_VAR T_NEWLINE {
782+
| T_QUIT T_VAR T_NEWLINE {
783783
long long code = getSymbolValueInt($2);
784784
if (is_interactive) {
785785
print_bye_bye();

functions/function.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ bool interactive_shell_function_error_absorbed = false;
55
extern int yyparse();
66

77
int reset_line_no_to = 0;
8+
bool decision_execution_mode = false;
89

910
void startFunction(char *name, enum Type type) {
1011
if (is_interactive) {
@@ -525,7 +526,9 @@ void executeDecision(_Function* function) {
525526
if (symbol->value.b) {
526527
function_buffer = strcat_ext(function_buffer, function->decision_functions.arr[i]);
527528
function_buffer = strcat_ext(function_buffer, "\n");
529+
decision_execution_mode = true;
528530
injectCode(function_buffer, INIT_PROGRAM);
531+
decision_execution_mode = false;
529532
is_decision_made = true;
530533
break;
531534
}

functions/function.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ _Function* scope_override;
5656
_Function* decision_mode;
5757
_Function* decision_expression_mode;
5858
_Function* decision_function_mode;
59+
bool decision_execution_mode;
5960
Symbol* decision_symbol_chain;
6061
char *decision_buffer;
6162

tests/shell/function.kaos

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import tests.shell.modules.module1
22

3-
void def f1()
3+
void def fa()
44
print "hey"
55
end
66

77
asdasd1()
88

9-
f1()
9+
fa()
1010

1111
asdasd2()
1212

13-
num def f2()
13+
num def fb()
1414
num a = 5
1515
return a
1616
end
17-
f2()
17+
fb()
1818

1919
// Print the function return on function calls from modules
2020
module1.sub(7, 3)
21+
22+
// Test for not printing function returns on function decision execution mode
23+
num def f1()
24+
num a = 101
25+
return a
26+
end
27+
28+
num def f2()
29+
num b = 102
30+
return b
31+
end
32+
33+
num def f3()
34+
num c = 103
35+
return c
36+
end
37+
38+
num def add(num x, num y)
39+
num z = x + y
40+
end {
41+
z == 8 : f1(),
42+
z > 10 : f2(),
43+
default : f3()
44+
}
45+
46+
add(3, 5)
47+
print add(3, 5)

tests/shell/function.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ Undefined function: asdasd2
1111
Absorbed by Interactive Shell
1212
5
1313
4
14+
101
15+
101

0 commit comments

Comments
 (0)