Skip to content

Commit 7fcd019

Browse files
committed
locate x,y
1 parent 26004d4 commit 7fcd019

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

basic

0 Bytes
Binary file not shown.

basic.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ static void statement_sleep(char **p);
222222
static void statement_def(char **p);
223223
static void statement_get(char **p);
224224
static void do_sleep_ticks(double ticks);
225+
static void statement_locate(char **p);
225226
static int function_lookup(const char *name, int len);
226227

227228
enum func_code {
@@ -2088,6 +2089,29 @@ static void statement_print(char **p)
20882089
fflush(stdout);
20892090
}
20902091

2092+
2093+
static void statement_locate(char **p)
2094+
{
2095+
// Passed as LOCATE x, y
2096+
struct value vx, vy;
2097+
int x, y;
2098+
2099+
// Validation
2100+
vx = eval_expr(p); ensure_num(&vx);
2101+
skip_spaces(p);
2102+
if (**p != ',') { runtime_error("LOCATE: expected ',' after X"); return; }
2103+
(*p)++;
2104+
vy = eval_expr(p); ensure_num(&vy);
2105+
skip_spaces(p);
2106+
2107+
x = (int)vx.num; if (x < 0) x = 0;
2108+
y = (int)vy.num; if (y < 0) y = 0;
2109+
2110+
// Move cursor with ANSI: rows/cols are 1-based
2111+
printf("\033[%d;%dH", y + 1, x + 1);
2112+
fflush(stdout);
2113+
}
2114+
20912115
static void statement_input(char **p)
20922116
{
20932117
char prompt[MAX_STR_LEN];
@@ -2514,6 +2538,11 @@ static void execute_statement(char **p)
25142538
statement_let(p);
25152539
return;
25162540
}
2541+
if (c == 'L' && starts_with_kw(*p, "LOCATE")) {
2542+
*p += 6;
2543+
statement_locate(p);
2544+
return;
2545+
}
25172546
if (c == 'G' && starts_with_kw(*p, "GET")) {
25182547
*p += 3;
25192548
statement_get(p);

tests/locate.bas

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
print chr$(147)
2+
print
3+
print CHR$(5)
4+
for y=0 to 24
5+
print y
6+
next y
7+
locate 0,0
8+
print "Hello, world!"
9+
locate 20,22
10+
print "20,22"
11+
locate 1, 1
12+
print "1,1"
13+
locate 30, 1
14+
print "30,1"
15+
locate 10, 10
16+
print "10,10!"
17+
locate 1, 10
18+
print "1,10!"
19+
locate 24,24
20+
loop:
21+
get k$
22+
if k$ = "" then goto loop

0 commit comments

Comments
 (0)