Sujet : Having fun with embedded Forth in BASIC
De : the.beez.speaks (at) *nospam* gmail.com (Hans Bezemer)
Groupes : comp.lang.forthDate : 03. May 2025, 17:01:56
Autres entêtes
Organisation : KPN B.V.
Message-ID : <nnd$1a552bd6$78ef4e91@6764aaa802de3ebc>
User-Agent : Mozilla Thunderbird
I watched this ZX Spectrum episode on YT:
https://www.youtube.com/watch?v=xyfXxW8UuBM&t=516sForth inside a BASIC program? Sounds cool. And then it dawned upon me. Mu uBasic/4tH interpreter has a native data stack - with commands like PUSH, POP() and TOS(). And it doubles as a parameter stack - meaning, whatever you put on the parameter stack is visible in the data stack.
So I tried to amuse myself a bit with that idea today. I came up with a 150 lines uBasic/4tH program that transpiles a Basic program with embedded Forth into a true Basic program - like this:
$ ubasic ubforth.bas forth.bas basic.bas
>> Transpiling "forth.bas" to "basic.bas" <<
19 lines processed. Done.
0 OK, 0:5520
This is the source:
Print "This is still Basic"
forth{
: square dup * ;
: printstr
begin
dup len
while
dup 0 peek emit 1 chop
repeat cr ." The length is " len . ." now!" cr
;
." The square of 25 =" 25 square 2 spaces . cr cr
s" This is the end!" printstr cr
}basic
Print "This was Forth"
Print FN(_square(16))
BTW, it is a dialect of course - but it is understandable for most of you, I guess. And this the generated Basic code (abridged):
Print "This is still Basic"
If 0 Then
_square
Proc _dup : Proc _mul
Return
EndIf
If 0 Then
_printstr
Do
Proc _dup : Proc _len
While Pop()
Proc _dup : Push 0 : Proc _peek : Proc _emit : Push 1 : Proc _chop
Loop
Proc _cr : Print "The length is "; : Proc _len : Proc _dot : Print "now!"; : Proc _cr
Return
EndIf
Print "The square of 25 ="; : Push 25 : Proc _square : Push 2 : Proc _spaces : Proc _dot : Proc _cr : Proc _cr
Push "This is the end!" : Proc _printstr : Proc _cr
Print "This was Forth"
Print FN(_square(16))
End
Now - you won't hear me say this thing is serious, but it is actually fun.
Hans Bezemer