1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-23 16:08:06 +11:00

basic: add while command

This commit is contained in:
Virgil Dupras 2019-12-12 11:17:10 -05:00
parent 51c977f2ed
commit 3db38b0d89
5 changed files with 43 additions and 22 deletions

View File

@ -126,11 +126,14 @@ specified as an argument. Errors out if line doesn't exist. Argument can be
an expression. If invoked in direct mode, `run` must be called to actually
run the line (followed by the next, and so on).
`if <cond> <cmd>`: If specified condition is true, execute the rest of the
`if <cond> <cmds>`: If specified condition is true, execute the rest of the
line. Otherwise, do nothing. For example, `if 2>1 print 12` prints `12` and `if
2<1 print 12` does nothing. The argument for this command is a "thruth
expression".
`while <cond> <cmds>`: As long as specified condition is true, execute specified
commands repeatedly.
`input [<prompt>]`: Prompts the user for a numerical value and puts that
value in `A`. The prompted value is evaluated as an expression and then stored.
The command takes an optional string literal parameter. If present, that string

View File

@ -267,14 +267,20 @@ basGOTO:
ld (BAS_PNEXTLN), de
ret
basIF:
; evaluate truth condition at (HL) and set A to its value
; Z for success (but not truth!)
_basEvalCond:
push hl ; --> lvl 1. original arg
ld de, SCRATCHPAD
call rdWord
ex de, hl
call parseTruth
pop hl ; <-- lvl 1. restore
ret nz
ret
basIF:
call _basEvalCond
ret nz ; error
or a
ret z
; expr is true, execute next
@ -285,6 +291,26 @@ basIF:
ld de, basCmds2
jp basCallCmds
basWHILE:
push hl ; --> lvl 1
call _basEvalCond
jr nz, .stop ; error
or a
jr z, .stop
ret z
; expr is true, execute next
; (HL) back to beginning of args, skip to next arg
call toSepOrEnd
call rdSep
ret nz
ld de, basCmds2
call basCallCmds
pop hl ; <-- lvl 1
jr basWHILE
.stop:
pop hl ; <-- lvl 1
ret
basINPUT:
; If our first arg is a string literal, spit it
call spitQuoted
@ -478,6 +504,8 @@ basCmds2:
.dw basGOTO
.db "if", 0
.dw basIF
.db "while", 0
.dw basWHILE
.db "input", 0
.dw basINPUT
.db "peek", 0

View File

@ -52,21 +52,12 @@ Let's try an example: You glue yourself a Collapse OS with a mmap starting at
could do to copy memory around:
> m=0xe000
> 10 getc
> 20 poke m a
> 30 m=m+1
> 40 if m<0xe004 goto 10
> run
> while m<0xe004 getc:poke m a:m=m+1
[enter "abcd"]
> bsel 3
> clear
> 10 getb
> 20 puth a
> run
61> run
62> run
63> run
64> bseek 2
> run
63> run
> i=0
> while i<4 getb:puth a:i=i+1
61626364> bseek 2
> getb:puth a
63> getb:puth a
64>

View File

@ -21,9 +21,7 @@ increase a number at memory address `0xa100`. First, compile it:
Now, we'll send that code to address `0xa000`:
> m=0xa000
> 10 getc
> 20 poke m a
> 30 if m<0xa008 goto 10
> while m<0xa008 getc:poke m a:m=m+1
(resulting binary is 8 bytes long)
Now, at this point, it's a bit delicate. To pipe your binary to your serial

View File

@ -61,6 +61,7 @@
.equ STDIO_RAMSTART BLOCKDEV_RAMEND
.equ STDIO_GETC emulGetC
.equ STDIO_PUTC emulPutC
.equ STDIO_BUFSIZE 0x40 ; override
.inc "stdio.asm"
.equ FS_RAMSTART STDIO_RAMEND
@ -70,7 +71,7 @@
; *** BASIC ***
; RAM space used in different routines for short term processing.
.equ SCRATCHPAD_SIZE 0x20
.equ SCRATCHPAD_SIZE STDIO_BUFSIZE
.equ SCRATCHPAD FS_RAMEND
.inc "lib/util.asm"
.inc "lib/ari.asm"