New community dev tool uploaded: alpha xForth Compiler

All aspects of programming on the Commander X16.
mobluse
Posts: 175
Joined: Tue Aug 04, 2020 2:16 pm
Location: Lund, Sweden
Contact:

New community dev tool uploaded: alpha xForth Compiler

Post by mobluse »


A limitation is that you cannot have definitions longer than 79 characters without newlines.

I had:


Quote




: .MATRIX ( R C -- ) SWAP 0 ?DO CR DUP 0 ?DO ." (" J 1 + . ." ," I 1 + . ." ) " LOOP LOOP DROP ; 5 3 .MATRIX



but had to change it to:


Quote




: .M SWAP 0 ?DO CR DUP 0 ?DO ." (" J 1 + . ." ," I 1 + . ." )" LOOP LOOP DROP ;

5 3 .M



But you can have it on several lines:


Quote




: .MATRIX ( R C -- ) SWAP 0 ?DO CR DUP 0 ?DO ." (" J 1 + . ." ," I 1 + . ." ) "

LOOP LOOP DROP ; 5 3 .MATRIX



I pasted in the long lines.

X16&C64 Quiz: Try It Now! Huge Char Demo: Try It Now! DECPS: Try It Now! Aritm: Try It Now!
mobluse
Posts: 175
Joined: Tue Aug 04, 2020 2:16 pm
Location: Lund, Sweden
Contact:

New community dev tool uploaded: alpha xForth Compiler

Post by mobluse »


Can you write mutually recursive functions in this Forth? I noticed DEFER, IS, and DEFER@ doesn't exist.

https://rosettacode.org/wiki/Mutual_recursion#Forth

X16&C64 Quiz: Try It Now! Huge Char Demo: Try It Now! DECPS: Try It Now! Aritm: Try It Now!
BruceMcF
Posts: 1336
Joined: Fri Jul 03, 2020 4:27 am

New community dev tool uploaded: alpha xForth Compiler

Post by BruceMcF »



3 hours ago, mobluse said:




A limitation is that you cannot have definitions longer than 79 characters without newlines.



Why is that a limitation? Just hit return and keep defining. Because you are not in interpret mode when you do the return, you will not get an "OK" prompt.

BruceMcF
Posts: 1336
Joined: Fri Jul 03, 2020 4:27 am

New community dev tool uploaded: alpha xForth Compiler

Post by BruceMcF »



4 hours ago, mobluse said:




Can you write mutually recursive functions in this Forth? I noticed DEFER, IS, and DEFER@ doesn't exist.



https://rosettacode.org/wiki/Mutual_recursion#Forth



For self-recursion, use RECURSE (and report if it works!)

For mutual recursion, you always COULD do it with variables, ' and EXECUTE ... DEFER IS and DEFER@ are just semantic sugar.

VARIABLE '2NDWORLD 

: 1STWORD ( n1 -- n2 ) ?DUP IF '2NDWORD @EXECUTE THEN ;

: 2NDWORD ( ... -- ... ) ... {whatever 2NDWORD does} ... 1STWORD ;

' 2NDWORD '2NDWORD !



Or, if you prefer:

VARIABLE '1STWORD

: 1STWORD ( n1 -- n2 ) '1STWORD @EXECUTE ;

: 2NDWORD ( ... -- ... ) ... {whatever 2NDWORD does } ... 1STWORD ;

:NONAME ( n1 -- n2 ) ?DUP IF 2NDWORD THEN ; '1STWORD !

mobluse
Posts: 175
Joined: Tue Aug 04, 2020 2:16 pm
Location: Lund, Sweden
Contact:

New community dev tool uploaded: alpha xForth Compiler

Post by mobluse »



12 minutes ago, BruceMcF said:




Why is that a limitation? Just hit return and keep defining. Because you are not in interpret mode when you do the return, you will not get an "OK" prompt.



It's a limitation since you need to insert line-breaks. The source you paste from doesn't always have line-breaks (CR).



A related topic: In GForth they type "compiled" when they reach new-line (CR) and the definition is not finished, e.g.


Quote




: test 1 2  compiled

3 ;  ok



The "compiled" is useful feedback.

X16&C64 Quiz: Try It Now! Huge Char Demo: Try It Now! DECPS: Try It Now! Aritm: Try It Now!
BruceMcF
Posts: 1336
Joined: Fri Jul 03, 2020 4:27 am

New community dev tool uploaded: alpha xForth Compiler

Post by BruceMcF »



2 hours ago, mobluse said:




It's a limitation since you need to insert line-breaks. The source you paste from doesn't always have line-breaks (CR).



A related topic: In GForth they type "compiled" when they reach new-line (CR), e.g.



The "compiled" is useful feedback.



CR is how the CX16 line input mode works. If you are copying and pasting on a 32bit or 64bit system into an emulator of an 8bit system, I guess you are just going to have to get the 32bit or 64bit system to be a bit smarter about how it does things. Might need to copy it into the system text editor, save it in a text format with CR, and INCLUDE it from there.

Mind, xForth is still alpha, including that there is no INCLUDE yet, but that is deferred pending emulator development. Since it is mostly necessary to open the file and made it the default input until EOF, and then make the console the default input again, most of the tools to do it will already be available from the first prototype BLOCK system.

I could also note that copy and pasting from an online source directly into xForth might be putting more faith in an alpha build than is warranted. Copy and pasting line by line and looking for smoke as you do might be a better protocol anyway.



Gforth is a much bigger Forth than xForth, which saves an ongoing log of every line entered if you run it correctly, so it would never have need to edit the text on the screen and run it again. Plus going back, editing it on the screen and hitting return doesn't work on any system that gforth runs on. I've already got OK as a NOP, but I am not going to make a word as potentially useful as "COMPILED" into a NOP too.

xForth is a port of eForth, which is generally used today on smaller microcontrollers, like AVR8's. It IS a lot more space conscious than gforth is. Indeed, 10K is far too big for the final release build of xForth ... once INCLUDE and SAVE4TH are working, some of what is now included in the base image are going to get sent off to a CORE.FS include script to make a less overweight base image size. 7K would be a lot more respectable for an eForth descended Forth.

Gold
Posts: 6
Joined: Sat Jun 27, 2020 9:01 am

New community dev tool uploaded: alpha xForth Compiler

Post by Gold »


Awesome, I've been waiting for a Forth to be available on the x16. Looking forward to finally getting around to learning it.

BruceMcF
Posts: 1336
Joined: Fri Jul 03, 2020 4:27 am

New community dev tool uploaded: alpha xForth Compiler

Post by BruceMcF »


Excellent. For command line entry, it will already take you through chapters 1 and 2 of Starting Forth.

Starting Forth is (c) Forth Inc., and while they make it freely available at their site, they don't permit free redistribution. So get it online or PDF at:

https://www.forth.com/starting-forth/

Gold
Posts: 6
Joined: Sat Jun 27, 2020 9:01 am

New community dev tool uploaded: alpha xForth Compiler

Post by Gold »


I've mostly been learning with gforth in the mean time. It seems like eforth (and by extension xforth) doesn't have a greater than word as default, is there any particular reason for that? All the ">" word seems to do is push 7 onto the stack.

BruceMcF
Posts: 1336
Joined: Fri Jul 03, 2020 4:27 am

New community dev tool uploaded: alpha xForth Compiler

Post by BruceMcF »



On 10/25/2020 at 8:05 PM, Gold said:




I've mostly been learning with gforth in the mean time. It seems like eforth (and by extension xforth) doesn't have a greater than word as default, is there any particular reason for that? All the ">" word seems to do is push 7 onto the stack.



Oops, haven't been tracking comments, since I was waiting on the $0000/$0001 in the emulator to start playing around with banking.

">" is not a CORE ANS Forth word, it is a CORE EXT word ... its meaning is standardized, but since it can be defined using two existing CORE words ... SWAP < ... minimal Forth's may omit it from the dictionary. Eforth attempts to be a minimal base wordset to simplify porting, and then additional words may be written on top of it ... or, if increased efficiency is desired, the words defined to allow a more efficient Forth to be coded using eForth.

gForth by contrast aims to be a more comprehensive ANS Forth, so AFAIR it has all of the CORE and CORE EXT words along with most of the other ANS wordsets ... the only exception that I can recall off the top of my head is the BLOCK implementation is provided in source code rather than included in the build by default. It also has most of the Forth 2012 words, since its two primary developers are active participants in the Forth20xx process.

When I have loadable scripts:

: > ( n1 n2 -- fl ) SWAP < ;

... would provide > as a convenience, but if someone wants > at present they can include that definition first.

Post Reply