Skip to content

Commit 9951cca

Browse files
committed
COMMON: fixed issues with CHAIN
1 parent 7cd274c commit 9951cca

File tree

5 files changed

+104
-38
lines changed

5 files changed

+104
-38
lines changed

ChangeLog

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414
Fixed editor highlighting
1515
Runtime errors now show source screen with red error highlighter
1616
Form refresh command now takes an boolean arg, true=push ui state to vars
17+
The IMAGE argument can now be PNG data stored in an INT array
18+
The IMAGE argument can now be x,y,w,h screen corordinates
19+
The IMAGE argument can now be a 2-D array of POINTS
20+
Updated IMAGE sub-command to save 2-D array of POINT
1721
Added window.setFont command to set font size, bold and italic. example:
1822
w = window():w.setFont(10, "pt", false, true)
23+
TRUE is now always returned as 1
24+
Added file manager to main shell program
1925
Fixed problem with escaped chars using FORMAT
2026
Fixed problem with XNOR command result
21-
TRUE is now always returned as 1
2227
Fixed problem with IMP and EQV command result
2328
Fixed problem with INKEY command
2429
Fixed capslock handling
25-
Added file manager to main shell program
2630
Fixed issues with TRY/CATCH
2731
Fixed using POINT to retrieve IMAGE data
28-
The IMAGE argument can now be PNG data stored in an INT array
32+
Fixed issues with CHAIN
2933

3034
2016-02-11
3135
Added export to mobile command (SDL)

samples/distro-examples/tests/chain.bas

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
code = "print \"hello\""
2-
chain code
1+
'
2+
' Tests for CHAIN
3+
'
4+
5+
chain "print \"error1\""
6+
chain "throw \"error2\""
7+
chain "throw \"error3\""
8+
chain "throw \"error4\""
39

410
dim ar_code
511
ar_code << "for i=0 to 10"
612
ar_code << " print i"
713
ar_code << "next i"
814
chain ar_code
915

16+
chain "throw \"error5\""
17+
1018
Const FILENAME = "chain.tmp"
1119

1220
Sub open_1
@@ -34,8 +42,10 @@ Chain FILENAME
3442
' BUG?: prints 16 (reads ONLY the first line,
3543
' should read file as Array - not as String...):
3644
open_1
45+
? #1, "Print \"POW 2,4\""
3746
? #1, "Print Pow(2, 4)"
3847
? #1, "Print Pow(2, 5)"
48+
? #1, "Print \"done\""
3949
Close #1
4050
Chain FILENAME
4151

@@ -56,3 +66,4 @@ Chain FILENAME
5666
' BUG?: does not report an ERROR (entering endless/long loop - ONLY IF previous "Chain FILENAME" is uncommented):
5767
Chain "Print Power(2, 1)" ' using a "string" instead of filename.
5868

69+
kill FILENAME

samples/distro-examples/tests/output/chain.out

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
hello
1+
error1
2+
3+
4+
* RTE-ERROR AT CH_MAIN:1 *
5+
Description:
6+
error2
7+
8+
9+
10+
* RTE-ERROR AT CH_MAIN:1 *
11+
Description:
12+
error3
13+
14+
15+
16+
* RTE-ERROR AT CH_MAIN:1 *
17+
Description:
18+
error4
19+
220
0
321
1
422
2
@@ -10,3 +28,31 @@ hello
1028
8
1129
9
1230
10
31+
32+
33+
* RTE-ERROR AT CH_MAIN:1 *
34+
Description:
35+
error5
36+
37+
2
38+
4
39+
8
40+
POW 2,4
41+
16
42+
32
43+
done
44+
45+
64
46+
128
47+
48+
49+
* RTE-ERROR AT CH_MAIN:1 *
50+
Description:
51+
Expr/RT: Variable is NOT an array (Use DIM)
52+
53+
54+
55+
* RTE-ERROR AT CH_MAIN:1 *
56+
Description:
57+
Expr/RT: Variable is NOT an array (Use DIM)
58+

src/common/blib.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,14 +2767,12 @@ void cmd_call_vfunc() {
27672767
if (v_func == NULL || v_func->type != V_FUNC) {
27682768
rt_raise(ERR_NO_FUNC);
27692769
} else {
2770-
if (code_peek() != kwTYPE_LEVEL_BEGIN) {
2771-
err_missing_lp();
2772-
} else {
2770+
if (code_peek() == kwTYPE_LEVEL_BEGIN) {
2771+
code_skipnext();
2772+
}
2773+
v_func->v.fn.cb(v_func->v.fn.self);
2774+
if (code_peek() == kwTYPE_LEVEL_END) {
27732775
code_skipnext();
2774-
v_func->v.fn.cb(v_func->v.fn.self);
2775-
if (!prog_error && code_peek() != kwTYPE_LEVEL_END) {
2776-
err_missing_rp();
2777-
}
27782776
}
27792777
}
27802778
}

src/common/brun.c

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ void brun_break() {
366366
*/
367367
void cmd_chain(void) {
368368
var_t var;
369-
const char *code = NULL;
370-
char *code_alloc = NULL;
369+
char *code = NULL;
371370

372371
v_init(&var);
373372
eval(&var);
@@ -380,20 +379,18 @@ void cmd_chain(void) {
380379
if (var.type == V_STR) {
381380
if (access(var.v.p.ptr, R_OK) == 0) {
382381
// argument is a file name
383-
FILE *f = fopen(var.v.p.ptr, "r");
384-
if (!fseek(f, 0, SEEK_END)) {
385-
int len = ftell(f);
386-
fseek(f, 0, SEEK_SET);
387-
if (len) {
388-
code_alloc = malloc(len + 1);
389-
fgets(code_alloc, len, f);
390-
code = code_alloc;
391-
}
382+
int h = open(var.v.p.ptr, O_BINARY | O_RDONLY, 0644);
383+
if (h != -1) {
384+
int len = lseek(h, 0, SEEK_END);
385+
lseek(h, 0, SEEK_SET);
386+
code = (char *)malloc(len + 1);
387+
len = read(h, code, len);
388+
code[len] = '\0';
389+
close(h);
392390
}
393-
fclose(f);
394391
}
395392
if (!code) {
396-
code = var.v.p.ptr;
393+
code = strdup(var.v.p.ptr);
397394
}
398395
} else if (var.type == V_ARRAY) {
399396
int el;
@@ -403,19 +400,16 @@ void cmd_chain(void) {
403400
if (el_p->type == V_STR) {
404401
int str_len = strlen(el_p->v.p.ptr) + 2;
405402
if (len) {
406-
code_alloc = realloc(code_alloc, len + str_len);
407-
strcat(code_alloc, el_p->v.p.ptr);
403+
code = realloc(code, len + str_len);
404+
strcat(code, el_p->v.p.ptr);
408405
} else {
409-
code_alloc = malloc(str_len);
410-
strcpy(code_alloc, el_p->v.p.ptr);
406+
code = malloc(str_len);
407+
strcpy(code, el_p->v.p.ptr);
411408
}
412-
strcat(code_alloc, "\n");
409+
strcat(code, "\n");
413410
len += str_len + 1;
414411
}
415412
}
416-
if (len) {
417-
code = code_alloc;
418-
}
419413
}
420414

421415
if (code == NULL) {
@@ -424,24 +418,30 @@ void cmd_chain(void) {
424418
return;
425419
}
426420

421+
v_free(&var);
422+
427423
int tid_base = create_task("CH_BASE");
428424
int tid_prev = activate_task(tid_base);
429425

430426
// compile the buffer
431427
sys_before_comp();
432428
int success = comp_compile_buffer(code);
433429

434-
v_free(&var);
435-
if (code_alloc) {
436-
free(code_alloc);
437-
}
430+
free(code);
431+
code = NULL;
432+
438433
if (success == 0) {
439434
close_task(tid_base);
440435
activate_task(tid_prev);
441436
prog_error = 1;
442437
return;
443438
}
444439

440+
char last_file[OS_PATHNAME_SIZE + 1];
441+
char bas_dir[OS_PATHNAME_SIZE + 1];
442+
strcpy(last_file, gsb_last_file);
443+
strcpy(bas_dir, gsb_bas_dir);
444+
445445
int tid_main = brun_create_task("CH_MAIN", ctask->bytecode, 0);
446446
exec_sync_variables(0);
447447

@@ -452,6 +452,13 @@ void cmd_chain(void) {
452452
close_task(tid_base); // cleanup task container
453453
activate_task(tid_prev); // resume calling task
454454

455+
// reset globals
456+
gsb_last_line = 0;
457+
gsb_last_error = 0;
458+
strcpy(gsb_last_file, last_file);
459+
strcpy(gsb_bas_dir, bas_dir);
460+
strcpy(gsb_last_errmsg, "");
461+
455462
if (success == 0) {
456463
prog_error = 1;
457464
}

0 commit comments

Comments
 (0)