Skip to content

Commit 12e6591

Browse files
committed
First try at allowing procedure parameters.
1 parent 6c2a09b commit 12e6591

File tree

7 files changed

+124
-5
lines changed

7 files changed

+124
-5
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ COMMON_AS_SRC=\
183183
src/interp/peekb.asm\
184184
src/interp/pmgraphics.asm\
185185
src/interp/poke.asm\
186+
src/interp/pop.asm\
186187
src/interp/position.asm\
187188
src/interp/print_str.asm\
188189
src/interp/print_tab.asm\

src/actions.asm

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
.export E_CONST_STRING
2727
.export E_VAR_CREATE, E_VAR_WORD, E_VAR_SEARCH
2828
.export E_VAR_SET_TYPE, E_LABEL_SET_TYPE
29-
.export E_LABEL, E_LABEL_DEF
29+
.export E_LABEL, E_LABEL_DEF, E_LABEL_EXEC, E_DO_EXEC
3030
.export E_PUSH_VAR, E_POP_VAR
3131
.exportzp VT_WORD, VT_STRING, VT_FLOAT, VT_UNDEF
3232
.exportzp VT_ARRAY_WORD, VT_ARRAY_BYTE, VT_ARRAY_STRING, VT_ARRAY_FLOAT
@@ -610,6 +610,34 @@ nfound: lda #0
610610
ret: rts
611611
.endproc
612612

613+
; Label search only
614+
.proc E_LABEL_EXEC
615+
ldx #label_buf - prog_ptr
616+
ldy label_count
617+
jsr list_search
618+
bcs do_create
619+
beq set_var
620+
sec
621+
rts
622+
623+
do_create:
624+
; Create a new label
625+
ldx #label_ptr - prog_ptr
626+
jsr name_new
627+
ldx label_count
628+
inc label_count
629+
set_var:
630+
stx E_POP_VAR+1
631+
clc
632+
rts
633+
.endproc
634+
635+
.proc E_DO_EXEC
636+
ldx E_POP_VAR+1
637+
jsr label_create::no_create
638+
jmp E_LABEL::cloop
639+
.endproc
640+
613641
; PUSH/POP variables
614642
.proc E_PUSH_VAR
615643
jsr get_last_tok ; Get variable ID from last token

src/basic.syn

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ TOKENS {
2727
TOK_SHL8, TOK_0, TOK_1
2828
# PUSH plus composed PUSH ops
2929
TOK_PUSH, TOK_PUSH_VAR_LOAD
30-
TOK_PUSH_NUM, TOK_PUSH_BYTE, TOK_PUSH_0, TOK_PUSH_1
30+
TOK_PUSH_NUM, TOK_PUSH_BYTE, TOK_PUSH_0, TOK_PUSH_1, TOK_POP
3131
# Numeric operators
3232
TOK_NEG, TOK_ABS, TOK_SGN, TOK_ADD, TOK_SUB, TOK_MUL, TOK_DIV, TOK_MOD
3333
# Operators on VAR
@@ -90,7 +90,7 @@ EXTERN {
9090
E_CONST_STRING
9191
E_VAR_CREATE, E_VAR_SET_TYPE, E_LABEL_SET_TYPE
9292
E_VAR_WORD, E_VAR_SEARCH
93-
E_LABEL, E_LABEL_DEF
93+
E_LABEL, E_LABEL_DEF, E_LABEL_EXEC, E_DO_EXEC
9494
E_PUSH_VAR, E_POP_VAR
9595

9696
#@if EXTENDED
@@ -629,6 +629,16 @@ PAUSE_OPT:
629629
EXPR
630630
E_EOL emit { TOK_0 }
631631

632+
# Optional expression for EXEC
633+
OPT_EXPR:
634+
EXPR emit { TOK_PUSH }
635+
pass
636+
637+
# Optional variable in PROC
638+
OPT_PROC_VAR:
639+
VAR_WORD_SAVE emit { TOK_POP, TOK_VAR_STORE } E_POP_VAR
640+
pass
641+
632642
# Parse a line
633643
PARSE_LINE_COMMAND: statement
634644
"." E_REM
@@ -678,9 +688,9 @@ PARSE_LINE_COMMAND: statement
678688
"PAuse" PAUSE_OPT emit TOK_PAUSE
679689
"INC" VAR_WORD_LVALUE_SADDR emit TOK_INC
680690
"DEc" VAR_WORD_LVALUE_SADDR emit TOK_DEC
681-
"PRoc" emit { TOK_JUMP, LT_PROC_DATA } E_PUSH_LT E_LABEL_DEF emit LT_PROC_2 E_PUSH_LT
691+
"PRoc" emit { TOK_JUMP, LT_PROC_DATA } E_PUSH_LT E_LABEL_DEF emit LT_PROC_2 E_PUSH_LT OPT_PROC_VAR
682692
"ENDProc" E_POP_PROC_2 emit TOK_RET E_POP_PROC_DATA
683-
"EXEc" emit { TOK_CALL, VT_UNDEF } E_LABEL
693+
"EXEc" E_LABEL_EXEC OPT_EXPR emit { TOK_CALL } E_DO_EXEC
684694
"DAta" DATA_VAR DATA_END
685695
"END" emit TOK_END
686696
#@if FASTBASIC_FP

src/compiler/parser.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,35 @@ bool SMB_E_LABEL(parse &s)
616616
return true;
617617
}
618618

619+
static std::string v_last_name;
620+
bool SMB_E_LABEL_EXEC(parse &s)
621+
{
622+
s.debug("E_LABEL_EXEC");
623+
// Get type
624+
auto &v = s.labels;
625+
std::string name;
626+
if( !s.get_ident(name) )
627+
return false;
628+
if ( v.find(name) == v.end() )
629+
{
630+
v[name] = VT_UNDEF;
631+
}
632+
// Check type
633+
if( v[name] != VT_UNDEF )
634+
return false;
635+
// Store variable name
636+
s.add_text(name);
637+
v_last_name = name;
638+
return true;
639+
}
640+
641+
bool SMB_E_DO_EXEC(parse &s)
642+
{
643+
s.debug("E_DO_EXEC");
644+
s.emit_word("fb_lbl_" + v_last_name);
645+
return true;
646+
}
647+
619648
bool SMB_E_LABEL_SET_TYPE(parse &s)
620649
{
621650
s.debug("E_LABEL_SET_TYPE");

src/interp/pop.asm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
;
2+
; FastBasic - Fast basic interpreter for the Atari 8-bit computers
3+
; Copyright (C) 2017-2021 Daniel Serpell
4+
;
5+
; This program is free software; you can redistribute it and/or modify
6+
; it under the terms of the GNU General Public License as published by
7+
; the Free Software Foundation, either version 2 of the License, or
8+
; (at your option) any later version.
9+
;
10+
; This program is distributed in the hope that it will be useful,
11+
; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
; GNU General Public License for more details.
14+
;
15+
; You should have received a copy of the GNU General Public License along
16+
; with this program. If not, see <http://www.gnu.org/licenses/>
17+
;
18+
; In addition to the permissions in the GNU General Public License, the
19+
; authors give you unlimited permission to link the compiled version of
20+
; this file into combinations with other programs, and to distribute those
21+
; combinations without any restriction coming from the use of this file.
22+
; (The General Public License restrictions do apply in other respects; for
23+
; example, they cover modification of the file, and distribution when not
24+
; linked into a combine executable.)
25+
26+
27+
; Stack pop
28+
; ---------
29+
30+
.importzp next_ins_incsp
31+
.import stack_l, stack_h
32+
33+
.segment "RUNTIME"
34+
35+
.proc EXE_POP ; pop AX from stack
36+
lda stack_l, y
37+
ldx stack_h, y
38+
jmp next_ins_incsp
39+
.endproc
40+
41+
.include "../deftok.inc"
42+
deftoken "POP"
43+
44+
; vi:syntax=asm_ca65

testsuite/tests/testproc.bas

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ endproc
2020

2121
exec before
2222

23+
exec param 123
24+
25+
' Proc with a parameter
26+
proc param x
27+
? "X="; x
28+
endproc

testsuite/tests/testproc.chk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Loop: 8
1313
Loop: 9
1414
Loop: 10
1515
-- test --
16+
X=123

0 commit comments

Comments
 (0)