Skip to content

Commit 26960ec

Browse files
while loop and for-each loop works
1 parent 41d1321 commit 26960ec

File tree

5 files changed

+69
-42
lines changed

5 files changed

+69
-42
lines changed

mas/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static Token* read_identifier() {
8383
else if (strcmp(buffer, "print") == 0) tok->type = KW_PRINT;
8484
else if (strcmp(buffer, "end") == 0) tok->type = TOK_END;
8585
else tok->type = TOK_ID;
86-
86+
// printf("LEXED IDENTIFIER: '%s' -> TOKEN %d\n", buffer, tok->type);
8787
return tok;
8888
}
8989

mas/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main(int argc, char* argv[]) {
5858

5959
// if (type == TOK_EOF) break;
6060
// }while(1);
61-
61+
// printf("KW_IN = %d\n", KW_IN);
6262
ASTNode* ast = parse_program();
6363
fclose(f);
6464

mas/mas.exe

0 Bytes
Binary file not shown.

mas/parser.c

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ ASTNode* parse_statement() {
7171
if (match(KW_DEF)) {
7272
// Function definition
7373
advance(); // consume 'def'
74-
consume(TOK_ID, "Expected function name");
74+
if (!match(TOK_ID)) {
75+
fprintf(stderr, "Parse error at line %d: Expected function name\n", current_token->line);
76+
exit(1);
77+
}
7578
char* func_name = strdup(current_token->value);
7679
advance();
7780
consume(TOK_LPAREN, "Expected '('");
@@ -81,10 +84,13 @@ ASTNode* parse_statement() {
8184

8285
if (!match(TOK_RPAREN)) {
8386
do {
84-
consume(TOK_ID, "Expected parameter name");
85-
params[param_count++] = strdup(current_token->value);
86-
advance();
87-
} while (match(TOK_COMMA));
87+
if (!match(TOK_ID)) {
88+
fprintf(stderr, "Parse error at line %d: Expected parameter name\n", current_token->line);
89+
exit(1);
90+
}
91+
params[param_count++] = strdup(current_token->value);
92+
advance();
93+
} while (match(TOK_COMMA));
8894
consume(TOK_RPAREN, "Expected ')'");
8995
}
9096

@@ -139,36 +145,41 @@ ASTNode* parse_statement() {
139145
return loop;
140146
}
141147
else if (match(KW_EACH)) {
142-
advance(); // consume 'each'
143-
consume(TOK_ID, "Expected variable name");
144-
char* target = strdup(current_token->value);
145-
advance();
146-
consume(KW_IN, "Expected 'in'");
147-
ASTNode* iterable = parse_expression();
148-
consume(TOK_COLON, "Expected ':'");
149-
consume(TOK_NEWLINE, "Expected newline after each header");
150-
151-
int body_count = 0;
152-
ASTNode** body = malloc(sizeof(ASTNode*) * 100);
153-
while (current_token && current_token->type != TOK_END) {
154-
if (current_token->type == TOK_NEWLINE) {
155-
advance();
156-
continue;
157-
}
158-
body[body_count++] = parse_statement();
148+
advance(); // consume 'each'
149+
150+
if (!match(TOK_ID)) {
151+
fprintf(stderr, "Parse error at line %d: Expected variable name\n", current_token->line);
152+
exit(1);
153+
}
154+
char* target = strdup(current_token->value);
155+
advance(); // consume the identifier
156+
157+
consume(KW_IN, "Expected 'in'");
158+
ASTNode* iterable = parse_expression();
159+
consume(TOK_COLON, "Expected ':'");
160+
consume(TOK_NEWLINE, "Expected newline after each header");
161+
162+
// Parse body
163+
int body_count = 0;
164+
ASTNode** body = malloc(sizeof(ASTNode*) * 100);
165+
while (current_token && current_token->type != TOK_END) {
166+
if (current_token->type == TOK_NEWLINE) {
167+
advance();
168+
continue;
159169
}
160-
consume(TOK_END, "Expected 'end' to close each");
161-
162-
ASTNode* each = malloc(sizeof(ASTNode));
163-
each->type = AST_EACH;
164-
each->line = current_token->line;
165-
each->data.each.target = target;
166-
each->data.each.iterable = iterable;
167-
each->data.each.body = body;
168-
each->data.each.body_count = body_count;
169-
return each;
170+
body[body_count++] = parse_statement();
170171
}
171-
else if (match(KW_IF)) {
172+
consume(TOK_END, "Expected 'end' to close each");
173+
174+
ASTNode* each = malloc(sizeof(ASTNode));
175+
each->type = AST_EACH;
176+
each->line = current_token->line;
177+
each->data.each.target = target;
178+
each->data.each.iterable = iterable;
179+
each->data.each.body = body;
180+
each->data.each.body_count = body_count;
181+
return each;
182+
}else if (match(KW_IF)) {
172183
advance(); // consume 'if'
173184
ASTNode* condition = parse_expression();
174185
consume(TOK_COLON, "Expected ':'");

mas/test.mas

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
x = 2
2-
if x > 5:
3-
print "hello"
4-
print "hello"
5-
else:
6-
print "world"
7-
end
1+
# Hello World
2+
print "Hello, MAS!"
3+
4+
# Variables and math
5+
x = 10
6+
y = x * 2 + 5
7+
print "Result:", y
8+
9+
# Lists
10+
fruits = ["apple", "banana", "cherry"]
11+
print fruits
12+
13+
# Loop
14+
i = 0
15+
loop i < 3:
16+
print "Count:", i
17+
i = i + 1
18+
end
19+
20+
# For-each loop
21+
each f in fruits:
22+
print "Fruit:", f
23+
end

0 commit comments

Comments
 (0)