Skip to content

Commit

Permalink
Adds CLR statement.
Browse files Browse the repository at this point in the history
This fixes #18
  • Loading branch information
dmsc committed Mar 5, 2020
1 parent f90baab commit e63c6ad
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
13 changes: 13 additions & 0 deletions manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,19 @@ General Statements
apostrophe will be ignored. This is
analogous to REM in Atari BASIC.


**Clears variables and free memory**
**CLR**

Clears all integer and floating-point
variables to 0, all strings to empty
strings and frees all memory
associated with arrays.

After `CLR` you can't access arrays
without allocating again with `DIM`.


**Defines array with initial values**
**DATA _arr()_ [type] = n1,n2, / DA.**

Expand Down
1 change: 1 addition & 0 deletions src/basic.syn
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ PARSE_LINE_COMMAND:
"PMHpos" EXPR emit { TOK_PUSH_NUM, &HPOSP0, TOK_ADD, TOK_SADDR } "," EXPR emit TOK_POKE
"Timer" emit { TOK_0, TOK_BYTE_POKE, 20, TOK_0, TOK_BYTE_POKE, 19 }
"DLi" DLI_COMMAND
"CLR" emit { TOK_NUM, &CLEAR_DATA, TOK_USR_ADDR, TOK_USR_CALL }

PARSE_LINE_ASSIGN:
VAR_WORD_SAVE EQUAL EXPR emit TOK_VAR_STORE E_POP_VAR
Expand Down
5 changes: 4 additions & 1 deletion src/interp/clearmem.asm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
; ------------

.export clear_data, alloc_array, mem_set, err_nomem, saved_cpu_stack
.export compiled_num_vars
.export compiled_num_vars, CLEAR_DATA

.import putc
.importzp var_page, tmp1, tmp2, array_ptr
Expand All @@ -39,6 +39,9 @@ MEMTOP= $2E5
; Allocation size
alloc_size= tmp1

; Uppercase to export as FastBasic symbol
CLEAR_DATA = clear_data

;----------------------------------------------------------
; Following routines are part of the runtime
.segment "RUNTIME"
Expand Down
2 changes: 1 addition & 1 deletion src/parse.asm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
.importzp DEGFLAG, DEGFLAG_DEG, DEGFLAG_RAD
.import EXE_END, saved_cpu_stack, PMGMODE, PMGBASE
; Interpreted commands
.import SOUND_OFF
.import CLEAR_DATA, SOUND_OFF
; From errors.asm
.import error_msg_list
.importzp ERR_PARSE, ERR_NO_ELOOP, ERR_LABEL, ERR_TOO_LONG
Expand Down
25 changes: 25 additions & 0 deletions testsuite/tests/stmt-clr.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
' Test for statement "CLR"
? "Start"

n = 0
f = fre()

' Dim inside a PROC to call multiple times
proc dima
? "---- DIM "; n; " ----"
? "Before: "; f-fre()
dim a(n)
? "After: "; f-fre()
endproc

n=10 : exec dima
n=10 : exec dima

? "---- CLR ----"
clr

? f, n, adr(a)

f = fre()
n=100 : exec dima

15 changes: 15 additions & 0 deletions testsuite/tests/stmt-clr.chk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Name: Test statement "CLR"
Test: run
Output:
Start
---- DIM 10 ----
Before: 0
After: 22
---- DIM 10 ----
Before: 22
After: 44
---- CLR ----
0 0 0
---- DIM 100 ----
Before: 0
After: 202

0 comments on commit e63c6ad

Please sign in to comment.