Skip to content

Commit 45d0e0c

Browse files
committed
Add a new token T_FOREACH_VAR specifically for 'foreach as' statements inside the loop and function bodies
1 parent b8e2989 commit 45d0e0c

File tree

8 files changed

+134
-13
lines changed

8 files changed

+134
-13
lines changed

chaos.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,5 @@ switch (phase)
202202
"void" {recordToken(strdup(yytext), yyleng); if (isStreamOpen()) return T_VOID;}
203203
"def" {recordToken(strdup(yytext), yyleng); return T_FUNCTION;}
204204
"import" {recordToken(strdup(yytext), yyleng); return T_IMPORT;}
205-
[a-zA-Z_][a-zA-Z0-9_]* {bool was_foreach = isForeach(); recordToken(strdup(yytext), yyleng); if ((isStreamOpen()) || was_foreach) { yylval.sval=strdup(yytext); return T_VAR; } }
205+
[a-zA-Z_][a-zA-Z0-9_]* {recordToken(strdup(yytext), yyleng); yylval.sval=strdup(yytext); if (isStreamOpen()) { return T_VAR; } else { return T_FOREACH_VAR; }}
206206
%%

chaos.y

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ char *program_file_dir;
5959
%token<bval> T_TRUE T_FALSE
6060
%token<ival> T_INT T_TIMES_DO_INT
6161
%token<fval> T_FLOAT
62-
%token<sval> T_STRING T_VAR
62+
%token<sval> T_STRING T_VAR T_FOREACH_VAR
6363
%token<lluval> T_UNSIGNED_LONG_LONG_INT
6464
%token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LEFT T_RIGHT T_EQUAL
6565
%token T_LEFT_BRACKET T_RIGHT_BRACKET T_LEFT_CURLY_BRACKET T_RIGHT_CURLY_BRACKET T_COMMA T_DOT T_COLON
@@ -228,7 +228,7 @@ line: T_NEWLINE
228228
| function T_NEWLINE { }
229229
| T_END decisionstart { }
230230
| T_IMPORT module T_NEWLINE { if (is_interactive) handleModuleImport(NULL, false); }
231-
| T_IMPORT module T_AS T_VAR T_NEWLINE { if (is_interactive) handleModuleImport($4, false); }
231+
| T_IMPORT module T_AS T_VAR T_NEWLINE { if (is_interactive) { handleModuleImport($4, false); } else { free($4); } }
232232
| T_FROM module T_IMPORT T_MULTIPLY { if (is_interactive) handleModuleImport(NULL, true); }
233233
| T_FROM module T_IMPORT function_name { if (is_interactive) handleModuleImport(NULL, true); }
234234
| error T_NEWLINE parser { if (is_interactive) { yyerrok; yyclearin; } }
@@ -694,6 +694,9 @@ loop:
694694
| T_FOREACH T_VAR T_AS T_VAR { startForeach($2, $4); }
695695
| T_FOREACH T_VAR T_AS T_VAR T_COLON T_VAR { startForeachDict($2, $4, $6); }
696696
| T_FOREACH T_VAR T_AS T_VAR T_FOREACH_AS_COLON T_VAR { startForeachDict($2, $4, $6); }
697+
| T_FOREACH T_FOREACH_VAR T_AS T_FOREACH_VAR { startForeach($2, $4); }
698+
| T_FOREACH T_FOREACH_VAR T_AS T_FOREACH_VAR T_COLON T_FOREACH_VAR { startForeachDict($2, $4, $6); }
699+
| T_FOREACH T_FOREACH_VAR T_AS T_FOREACH_VAR T_FOREACH_AS_COLON T_FOREACH_VAR { startForeachDict($2, $4, $6); }
697700
;
698701

699702
decisionstart: { decision_mode = function_mode; handle_end_keyword(); }
@@ -753,6 +756,7 @@ preparser_line: { }
753756
function:
754757
| T_FOREACH_AS_COLON function { }
755758
| T_TIMES_DO_INT function { }
759+
| T_FOREACH_VAR function { free($1); }
756760
;
757761

758762
quit: { }

tests/everything.kaos

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
This is a test that almost every feature of the language
3+
used in a program altogether to see if anything is wrong.
4+
*/
5+
6+
import modules.lib
7+
from modules.lib import everything_f2
8+
import modules.lib as lib2
9+
10+
/*
11+
This is a
12+
multiline comment
13+
for the function below
14+
@description Adds to numbers together
15+
*/
16+
num def add(num x, str y)
17+
num z = x + y
18+
19+
bool a = true
20+
num b = 576.34
21+
// This is a comment
22+
list c = ['foo', "bar", 17, 3.14, a, false] // End of Line(EOL) comment
23+
pretty echo c
24+
# This is a comment too
25+
dict d = {'a': 1, 'b': b, 'c': [2, 3, 4]} # End of Line(EOL) comment
26+
pretty print d
27+
28+
del c
29+
end {
30+
z == 8 : f1(),
31+
z > 10 : f2(),
32+
default : f3()
33+
}
34+
35+
print add(3, 5)
36+
37+
echo lib.everything_f1(4)
38+
print everything_f2(32)
39+
print lib2.everything_f3(121, 34)
40+
41+
num def f1()
42+
num a = 101
43+
return a
44+
end
45+
46+
num def f2()
47+
num b = 102
48+
return b
49+
end
50+
51+
num def f3()
52+
num c = 103
53+
return c
54+
end
55+
56+
list a = [
57+
1,
58+
2,
59+
3
60+
]
61+
62+
foreach a as el
63+
print el
64+
el = 5
65+
2 times do
66+
print el
67+
end
68+
end
69+
70+
dict n = {'a': 'foo', 'b': 'bar', 'c': 'baz'}
71+
72+
foreach n as key : val
73+
print key
74+
print val
75+
end

tests/everything.out

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
'foo',
3+
'bar',
4+
17,
5+
3.14,
6+
true,
7+
false
8+
]{
9+
'a': 1,
10+
'b': 576.34,
11+
'c': [
12+
2,
13+
3,
14+
4
15+
]
16+
}
17+
101
18+
264
19+
465
20+
1
21+
5
22+
5
23+
2
24+
5
25+
5
26+
3
27+
5
28+
5
29+
a
30+
foo
31+
b
32+
bar
33+
c
34+
baz

tests/module.kaos

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import modules.hello
22
from modules.module5 import *
33
from modules.module6 import module6Function2, module6Function3, module6Function5
44
import modules/module7
5-
import modules\module8
5+
import modules\module8 as module_x
66
from modules.level1.level2.module9 import module9
77

88
void def mainContext()
@@ -15,6 +15,6 @@ module6Function2()
1515
module6Function3()
1616
module6Function5()
1717
module7.module7()
18-
module8.module8()
18+
module_x.module8()
1919
module9()
2020
mainContext()

tests/modules/lib.kaos

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,19 @@ dict def dict_f1()
2727
dict a = {'d': 4, 'e': 5, 'f': 6}
2828
return a
2929
end
30+
31+
num def everything_f1(num x)
32+
x = x / 2
33+
return x
34+
end
35+
36+
num def everything_f2(num x)
37+
x = x * 2
38+
return x
39+
end
40+
41+
num def everything_f3(num x, num y)
42+
x = (x + y) * 3
43+
return x
44+
end
45+

utilities/injector.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ void recordToken(char *token, int length) {
4646
free(token);
4747
}
4848

49-
bool isForeach() {
50-
if (last_token == NULL) return false;
51-
if (strcmp(last_token, "foreach") == 0 || strcmp(last_token, "as") == 0 || strcmp(last_token, ":") == 0) return true;
52-
return false;
53-
}
54-
5549
bool isStreamOpen() {
5650
return loop_mode == NULL && function_mode == NULL;
5751
}

utilities/injector.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ void parseTheModuleContent(char *module_path);
1919
char *last_token;
2020

2121
void recordToken(char *token, int length);
22-
23-
bool isForeach();
2422
bool isStreamOpen();
2523

2624
#endif

0 commit comments

Comments
 (0)