Skip to content

Commit ec12b9b

Browse files
committed
Default initialisation of values to 0
1 parent 5f2d426 commit ec12b9b

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

mas/interpreter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@
333333
}
334334
if (!value)
335335
{
336-
fprintf(stderr, "Undefined variable: %s\n", node->data.var_name);
337-
exit(1);
336+
//fprintf(stderr, "Undefined variaaable: %s\n", node->data.var_name);
337+
//exit(1);
338+
return create_number(0.0);
338339
}
339340
mas_object_incref(value);
340341
return value;

mas/mas.exe

0 Bytes
Binary file not shown.

mas/parser.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ ASTNode* parse_program() {
5656
statements[stmt_count++] = parse_statement();
5757

5858
// After a statement, we must have a newline or EOF.
59-
if (current_token->type != TOK_EOF && current_token->type != TOK_EOF) {
59+
if (current_token->type == TOK_EOF ) {
60+
advance();
61+
}
62+
else if (current_token->type != TOK_EOF ) {
6063
consume(TOK_NEWLINE, "Expected newline after statement");
6164
}
6265
}
@@ -68,6 +71,7 @@ ASTNode* parse_program() {
6871

6972
// Parse statement
7073
ASTNode* parse_statement() {
74+
int start_line = current_token->line;
7175
if (match(KW_DEF)) {
7276
advance(); // consume 'def'
7377

@@ -112,7 +116,7 @@ ASTNode* parse_statement() {
112116

113117
ASTNode* func = malloc(sizeof(ASTNode));
114118
func->type = AST_FUNCDEF;
115-
func->line = current_token->line;
119+
func->line = start_line;
116120
func->data.funcdef.name = func_name;
117121
func->data.funcdef.params = params;
118122
func->data.funcdef.param_count = param_count;
@@ -121,6 +125,7 @@ ASTNode* parse_statement() {
121125
return func;
122126
}
123127
else if (match(KW_LOOP)) {
128+
int loop_line = current_token->line;
124129
advance(); // consume 'loop'
125130
ASTNode* condition = parse_expression();
126131
consume(TOK_COLON, "Expected ':'");
@@ -139,14 +144,15 @@ ASTNode* parse_statement() {
139144

140145
ASTNode* loop = malloc(sizeof(ASTNode));
141146
loop->type = AST_LOOP;
142-
loop->line = current_token->line;
147+
loop->line = loop_line;
143148
loop->data.loop.condition = condition;
144149
loop->data.loop.body = body;
145150
loop->data.loop.body_count = body_count;
146151
return loop;
147152
}
148153
else if (match(KW_EACH)) {
149-
advance(); // consume 'each'
154+
int each_line = current_token->line;
155+
advance(); // consume 'each'
150156

151157
if (!match(TOK_ID)) {
152158
fprintf(stderr, "Parse error at line %d: Expected variable name\n", current_token->line);
@@ -193,7 +199,7 @@ ASTNode* parse_statement() {
193199

194200
ASTNode* each = malloc(sizeof(ASTNode));
195201
each->type = AST_EACH;
196-
each->line = current_token->line;
202+
each->line = each_line;
197203
each->data.each.target = target;
198204
each->data.each.iterable = iterable; // NULL for ranges
199205
each->data.each.range_start = range_start; // NULL for iterables
@@ -203,6 +209,7 @@ ASTNode* parse_statement() {
203209
return each;
204210
}
205211
else if (match(KW_IF)) {
212+
int if_line = current_token->line;
206213
advance(); // consume 'if'
207214
ASTNode* condition = parse_expression();
208215
consume(TOK_COLON, "Expected ':'");
@@ -241,7 +248,7 @@ else if (match(KW_IF)) {
241248

242249
ASTNode* if_node = malloc(sizeof(ASTNode));
243250
if_node->type = AST_IF;
244-
if_node->line = condition->line;
251+
if_node->line = if_line;
245252
if_node->data.if_stmt.condition = condition;
246253
if_node->data.if_stmt.then_body = then_body;
247254
if_node->data.if_stmt.then_body_count = then_body_count;
@@ -250,29 +257,33 @@ else if (match(KW_IF)) {
250257
return if_node;
251258
}
252259
else if (match(KW_GIVE)) {
260+
int give_line = current_token->line;
253261
advance(); // consume 'give'
254262
ASTNode* value = parse_expression();
255263
ASTNode* ret = malloc(sizeof(ASTNode));
256264
ret->type = AST_RETURN;
257-
ret->line = current_token->line;
265+
ret->line = give_line;
258266
ret->data.expr = value;
259267
return ret;
260268
}
261269
else if (match(KW_STOP)) {
270+
int stop_line = current_token->line;
262271
advance(); // consume 'stop'
263272
ASTNode* brk = malloc(sizeof(ASTNode));
264273
brk->type = AST_BREAK;
265-
brk->line = current_token->line;
274+
brk->line = stop_line;
266275
return brk;
267276
}
268277
else if (match(KW_NEXT)) {
278+
int next_line = current_token->line;
269279
advance(); // consume 'next'
270280
ASTNode* cont = malloc(sizeof(ASTNode));
271281
cont->type = AST_CONTINUE;
272-
cont->line = current_token->line;
282+
cont->line = next_line;
273283
return cont;
274284
}
275285
else if (match(KW_PRINT)) {
286+
int print_line = current_token->line;
276287
advance(); // consume 'print'
277288
// Handle print as a function call expression
278289
ASTNode** args = malloc(sizeof(ASTNode*) * 10); // Allow multiple args
@@ -288,7 +299,7 @@ else if (match(KW_IF)) {
288299

289300
ASTNode* call = malloc(sizeof(ASTNode));
290301
call->type = AST_CALL;
291-
call->line = current_token ? current_token->line : -1;
302+
call->line = current_token ? print_line : -1;
292303
call->data.call.name = strdup("print"); // The name of the built-in
293304
call->data.call.args = args;
294305
call->data.call.arg_count = arg_count;

mas/test.mas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
print "Hello, MAS!"
33

44
# Variables and math
5+
z
56
x = 10
67
y = x * 2 + 5
78
print "Result:", y
9+
print z
810

911
# Lists
1012
fruits = ["apple", "banana", "cherry"]

0 commit comments

Comments
 (0)