Skip to content

Commit f61fc66

Browse files
committed
Implement variable update from function returns
1 parent 4c9c882 commit f61fc66

File tree

9 files changed

+183
-20
lines changed

9 files changed

+183
-20
lines changed

chaos.y

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ variable: T_VAR { $$ = $1; }
501501
| variable T_EQUAL boolean_expression { updateSymbolBool($1, $3); $$ = ""; }
502502
| variable T_EQUAL arraystart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
503503
| variable T_EQUAL dictionarystart { finishComplexModeWithUpdate($1); $$ = ""; free($1); }
504+
| 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); } $$ = ""; }
505+
| 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); } $$ = ""; }
504506
| T_RETURN variable { returnSymbol($2); $$ = ""; }
505507
| variable_complex_element { if (is_interactive) { printSymbolValueEndWithNewLine(getComplexElementBySymbolId(variable_complex_element, variable_complex_element_symbol_id), false, false); $$ = ""; } else { yyerror("Syntax error"); } }
506508
| variable_complex_element T_EQUAL T_TRUE { updateComplexElementBool($3); $$ = ""; }
@@ -513,6 +515,8 @@ variable: T_VAR { $$ = $1; }
513515
| variable_complex_element T_EQUAL boolean_expression { updateComplexElementBool($3); $$ = ""; }
514516
| variable_complex_element T_EQUAL arraystart { updateComplexElementComplex(); $$ = ""; }
515517
| variable_complex_element T_EQUAL dictionarystart { updateComplexElementComplex(); $$ = ""; }
518+
| 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); } $$ = ""; }
519+
| 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); } $$ = ""; }
516520
;
517521

518522
variable_complex_element: { }

errors.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ void throw_error_base(unsigned short code, char *str1, char *str2, long long lld
5151
case E_ILLEGAL_VARIABLE_TYPE_FOR_VARIABLE:
5252
sprintf(error_msg, "Illegal variable type: %s, for variable: %s", str1, str2);
5353
break;
54-
case E_ARRAYS_ARE_NOT_MASS_ASSIGNABLE:
55-
sprintf(error_msg, "Arrays are not mass assignable! Target variable: %s", str1);
56-
break;
5754
case E_VARIABLE_IS_NOT_A_DICTIONARY:
5855
sprintf(error_msg, "Variable '%s' is not a dictionary!", str1);
5956
break;

errors.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ enum ExitCode {
2121
E_VARIABLE_IS_NOT_AN_ARRAY,
2222
E_INDEX_OUT_OF_RANGE,
2323
E_ILLEGAL_VARIABLE_TYPE_FOR_VARIABLE,
24-
E_ARRAYS_ARE_NOT_MASS_ASSIGNABLE,
2524
E_VARIABLE_IS_NOT_A_DICTIONARY,
2625
E_UNDEFINED_KEY,
2726
E_UNRECOGNIZED_COMPLEX_DATA_TYPE,

functions/function.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,31 @@ void createCloneFromFunctionReturn(char *clone_name, enum Type type, char *name,
376376
free(module);
377377
}
378378

379+
void updateSymbolByClonningFunctionReturn(char *clone_name, char *name, char*module) {
380+
_Function* function = getFunction(name, module);
381+
if (function->symbol == NULL) {
382+
append_to_array_without_malloc(&free_string_stack, name);
383+
throw_error(E_FUNCTION_DID_NOT_RETURN_ANYTHING, name);
384+
return;
385+
}
386+
updateSymbolByClonning(clone_name, function->symbol);
387+
free(clone_name);
388+
free(name);
389+
free(module);
390+
}
391+
392+
void updateComplexSymbolByClonningFunctionReturn(char *name, char*module) {
393+
_Function* function = getFunction(name, module);
394+
if (function->symbol == NULL) {
395+
append_to_array_without_malloc(&free_string_stack, name);
396+
throw_error(E_FUNCTION_DID_NOT_RETURN_ANYTHING, name);
397+
return;
398+
}
399+
updateComplexElementSymbol(function->symbol);
400+
free(name);
401+
free(module);
402+
}
403+
379404
void initMainFunction() {
380405
main_function = (struct _Function*)malloc(sizeof(_Function));
381406
main_function->name = "main";

functions/function.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ void addFunctionCallParameterSymbol(char *name);
8585
void returnSymbol(char *name);
8686
void printFunctionReturn(char *name, char *module, char *end, bool pretty, bool escaped);
8787
void createCloneFromFunctionReturn(char *clone_name, enum Type type, char *name, char *module, enum Type extra_type);
88+
void updateSymbolByClonningFunctionReturn(char *clone_name, char *name, char*module);
89+
void updateComplexSymbolByClonningFunctionReturn(char *name, char*module);
8890
void initMainFunction();
8991
void initScopeless();
9092
void freeFunction(_Function* function);

symbol.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,6 @@ Symbol* updateSymbolByClonning(char *clone_name, Symbol* symbol) {
650650
throw_error(E_ILLEGAL_VARIABLE_TYPE_FOR_VARIABLE, getTypeName(symbol->type), clone_name);
651651
}
652652

653-
if (clone_symbol->type == K_ARRAY) {
654-
append_to_array_without_malloc(&free_string_stack, clone_name);
655-
throw_error(E_ARRAYS_ARE_NOT_MASS_ASSIGNABLE, clone_name);
656-
}
657-
658653
Symbol* temp_symbol = clone_symbol;
659654

660655
if (clone_symbol->type == K_ANY) {

tests/function.kaos

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,109 @@ dict a = dict_f1()
175175
print a
176176
dict b = lib.dict_f1()
177177
print b
178+
179+
180+
// Variable update from function returns
181+
182+
del a
183+
bool a = false
184+
a = bool_f1()
185+
print a
186+
a = lib.bool_f1()
187+
print a
188+
189+
del a
190+
num a = 0
191+
a = num_f1()
192+
print a
193+
a = lib.num_f1()
194+
print a
195+
196+
del a
197+
str a = ''
198+
a = str_f1()
199+
print a
200+
a = lib.str_f1()
201+
print a
202+
203+
del a
204+
any a = 0
205+
a = any_f1()
206+
print a
207+
a = lib.any_f1()
208+
print a
209+
210+
del a
211+
list a = []
212+
a = list_f1()
213+
print a
214+
a = lib.list_f1()
215+
print a
216+
217+
del a
218+
dict a = {}
219+
a = dict_f1()
220+
print a
221+
a = lib.dict_f1()
222+
print a
223+
224+
225+
// Complex element update from function returns
226+
227+
del a
228+
del b
229+
list a = [1, 2, 3, 4, 5, 6]
230+
dict b = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
231+
232+
a[0] = bool_f1()
233+
print a
234+
a[0] = lib.bool_f1()
235+
print a
236+
b['a'] = bool_f1()
237+
print b
238+
b['a'] = lib.bool_f1()
239+
print b
240+
241+
a[1] = num_f1()
242+
print a
243+
a[1] = lib.num_f1()
244+
print a
245+
b['b'] = num_f1()
246+
print b
247+
b['b'] = lib.num_f1()
248+
print b
249+
250+
a[2] = str_f1()
251+
print a
252+
a[2] = lib.str_f1()
253+
print a
254+
b['c'] = str_f1()
255+
print b
256+
b['c'] = lib.str_f1()
257+
print b
258+
259+
a[3] = any_f1()
260+
print a
261+
a[3] = lib.any_f1()
262+
print a
263+
b['d'] = any_f1()
264+
print b
265+
b['d'] = lib.any_f1()
266+
print b
267+
268+
a[4] = list_f1()
269+
print a
270+
a[4] = lib.list_f1()
271+
print a
272+
b['e'] = list_f1()
273+
print b
274+
b['e'] = lib.list_f1()
275+
print b
276+
277+
a[5] = dict_f1()
278+
print a
279+
a[5] = lib.dict_f1()
280+
print a
281+
b['f'] = dict_f1()
282+
print b
283+
b['f'] = lib.dict_f1()

tests/function.out

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,49 @@ false
2626
false
2727
[1, 2, 3]
2828
true
29-
true
30-
5
29+
false
3130
5
31+
6
3232
foo
33-
foo
33+
bar
3434
7
35-
7
36-
[1, 2, 3]
35+
8
3736
[1, 2, 3]
37+
[4, 5, 6]
3838
{'a': 1, 'b': 2, 'c': 3}
39+
{'d': 4, 'e': 5, 'f': 6}
40+
true
41+
false
42+
5
43+
6
44+
foo
45+
bar
46+
7
47+
8
48+
[1, 2, 3]
49+
[4, 5, 6]
3950
{'a': 1, 'b': 2, 'c': 3}
51+
{'d': 4, 'e': 5, 'f': 6}
52+
[true, 2, 3, 4, 5, 6]
53+
[false, 2, 3, 4, 5, 6]
54+
{'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'a': true}
55+
{'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'a': false}
56+
[false, 5, 3, 4, 5, 6]
57+
[false, 6, 3, 4, 5, 6]
58+
{'c': 3, 'd': 4, 'e': 5, 'f': 6, 'a': false, 'b': 5}
59+
{'c': 3, 'd': 4, 'e': 5, 'f': 6, 'a': false, 'b': 6}
60+
[false, 6, 'foo', 4, 5, 6]
61+
[false, 6, 'bar', 4, 5, 6]
62+
{'d': 4, 'e': 5, 'f': 6, 'a': false, 'b': 6, 'c': 'foo'}
63+
{'d': 4, 'e': 5, 'f': 6, 'a': false, 'b': 6, 'c': 'bar'}
64+
[false, 6, 'bar', 7, 5, 6]
65+
[false, 6, 'bar', 8, 5, 6]
66+
{'e': 5, 'f': 6, 'a': false, 'b': 6, 'c': 'bar', 'd': 7}
67+
{'e': 5, 'f': 6, 'a': false, 'b': 6, 'c': 'bar', 'd': 8}
68+
[false, 6, 'bar', 8, [1, 2, 3], 6]
69+
[false, 6, 'bar', 8, [4, 5, 6], 6]
70+
{'f': 6, 'a': false, 'b': 6, 'c': 'bar', 'd': 8, 'e': [1, 2, 3]}
71+
{'f': 6, 'a': false, 'b': 6, 'c': 'bar', 'd': 8, 'e': [4, 5, 6]}
72+
[false, 6, 'bar', 8, [4, 5, 6], {'a': 1, 'b': 2, 'c': 3}]
73+
[false, 6, 'bar', 8, [4, 5, 6], {'d': 4, 'e': 5, 'f': 6}]
74+
{'a': false, 'b': 6, 'c': 'bar', 'd': 8, 'e': [4, 5, 6], 'f': {'a': 1, 'b': 2, 'c': 3}}

tests/modules/lib.kaos

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
bool def bool_f1()
2-
bool a = true
2+
bool a = false
33
return a
44
end
55

66
num def num_f1()
7-
num a = 5
7+
num a = 6
88
return a
99
end
1010

1111
str def str_f1()
12-
str a = "foo"
12+
str a = "bar"
1313
return a
1414
end
1515

1616
any def any_f1()
17-
any a = 7
17+
any a = 8
1818
return a
1919
end
2020

2121
list def list_f1()
22-
list a = [1, 2, 3]
22+
list a = [4, 5, 6]
2323
return a
2424
end
2525

2626
dict def dict_f1()
27-
dict a = {'a': 1, 'b': 2, 'c': 3}
27+
dict a = {'d': 4, 'e': 5, 'f': 6}
2828
return a
2929
end

0 commit comments

Comments
 (0)