Skip to content

Commit 9fcaece

Browse files
small fixes, and display error locations
1 parent 1acf03c commit 9fcaece

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

os/programs/test2.rrbasic

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
d$ = ARGS$
2+
3+
IF d$ = "/" THEN
4+
parent$ = "/"
5+
ELSE
6+
i = LEN(d$)
7+
WHILE i > 1 AND MID$(d$, i, 1) <> "/"
8+
i = i - 1
9+
ENDWHILE
10+
11+
IF i <= 1 THEN
12+
parent$ = "/"
13+
ELSE
14+
parent$ = LEFT$(d$, i - 1)
15+
ENDIF
16+
ENDIF
17+
18+
PRINT parent$

src/basic/flow_control.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ bool conditional(struct basic_ctx* ctx)
1313
strlcpy(current_line, ctx->ptr, pos ? pos - ctx->ptr + 1 : end - ctx->ptr + 1);
1414
for (char* n = current_line; *n && *n != '\n'; ++n) {
1515
if (strlen(n) >= 2 && isalnum(*n) && *(n + 1) == '$') {
16-
stringlike = true; /* String variable */
16+
stringlike = true; // String variable
1717
break;
1818
} else if (strlen(n) >= 2 && isalnum(*n) && *(n + 1) == '#') {
19-
real = true; /* Real variable */
19+
real = true; // Real variable
2020
break;
2121
} else if (strlen(n) >= 3 && isdigit(*n) && *(n + 1) == '.' && isdigit(*(n + 2))) {
22-
real = true; /* Decimal number */
22+
real = true; // Decimal number
2323
break;
2424
} else if (strlen(n) >= 5 && *n == ' ' && *(n + 1) == 'T' && *(n + 2) == 'H' && *(n + 3) == 'E' && *(n + 4) == 'N') {
2525
break;
@@ -47,13 +47,10 @@ void else_statement(struct basic_ctx* ctx)
4747
*/
4848
accept_or_return(ELSE, ctx);
4949
accept_or_return(NEWLINE, ctx);
50-
while (tokenizer_token(ctx) != END && !tokenizer_finished(ctx)) {
50+
while (tokenizer_token(ctx) != ENDIF && !tokenizer_finished(ctx)) {
5151
tokenizer_next(ctx);
52-
if (*ctx->ptr == 'I' && *(ctx->ptr+1) == 'F') {
53-
accept_or_return(IF, ctx);
54-
accept_or_return(NEWLINE, ctx);
55-
return;
56-
}
52+
accept_or_return(NEWLINE, ctx);
53+
return;
5754
}
5855
tokenizer_error_print(ctx, "Block IF/THEN/ELSE without ENDIF");
5956
}

src/basic/tokenizer.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,22 @@ void tokenizer_error_print(struct basic_ctx* ctx, const char* error)
399399
}
400400
setforeground(COLOUR_LIGHTRED);
401401
kprintf("Error on line %ld: %s\n", ctx->current_linenum, error);
402+
setforeground(COLOUR_DARKRED);
403+
ub_line_ref* line = hashmap_get(ctx->lines, &(ub_line_ref){ .line_number = ctx->current_linenum });
404+
if (line) {
405+
char l[MAX_STRINGLEN];
406+
char* p = strchr(line->ptr, '\n');
407+
strlcpy(l, line->ptr, p ? p - line->ptr + 1 : strlen(line->ptr));
408+
size_t offset = ctx->ptr - line->ptr;
409+
if (offset > strlen(l)) {
410+
offset = 0;
411+
}
412+
kprintf("%s\n", l);
413+
for (size_t x = 0; x < offset - 1; ++x) {
414+
put(' ');
415+
}
416+
kprintf("^\n");
417+
}
402418
setforeground(COLOUR_WHITE);
403419
ctx->ended = true;
404420
}

0 commit comments

Comments
 (0)