FORTH for the SHARP PC-E500 (S)
10-07-2021, 05:55 PM (This post was last modified: 10-25-2021 08:52 PM by robve.)
Post: #23
 robve Senior Member Posts: 304 Joined: Sep 2020
RE: FORTH for the SHARP PC-E500 (S)
(10-06-2021 11:35 PM)Helix Wrote:  I've noticed that if there is an error in a definition, the corresponding word cannot be deleted from the dictionary with FORGET. I have to delete the previous valid definition if I want to get rid of this false entry. Is this normal?

Good point! Yep, this is normal. A word cannot be found (with FORGET, ' tick etc.) if it is hidden. To unhide and delete the last definition, use REVEAL then FORGET. Incomplete definitions are hidden to prevent accidentally running them, which would lead to a crash obviously. WORDS still shows all hidden words but FORGET can't find them.

FORGET implementations in Forth may slightly differ in this respect, but FORGET is considered obsolescent by the standard anyway. However, FORGET is still very useful as an easy way to redo a definition interactively. In general, placing a MARKER is preferred to delete code (see MARKER and ANEW). FORGET was removed by Sébastien from his pceForth (it was commented out). I rewrote parts of it to correct a bug in FORGET that caused a dictionary memory leak.

To go back to the question about improving error reporting, the word that caused the exception should be shown to the user with some context. This should be easy to implement by changing the last part of (ERROR) to report location of the error on the line by showing the line up to and including the word that caused the error:

SOURCE >IN @ UMIN TYPE ." << exception #" S>D (D.) TYPE

where SOURCE returns a pointer and size to the input buffer (TIB or FIB) and >IN is the location in this buffer of the next word after the last word executed. This will report the error in user input and in source files (albeit without a line number alas).

Closing a file after INCLUDE should be done as follows by catching INTERPRET exceptions in a new definition of INCLUDE-FILE:

Code:
: INCLUDE-FILE     save-input n>r     to source-id     begin       refill     while       ['] interpret catch ?dup if         source-id close-file drop         n>r restore-input drop         throw       then     repeat     source-id close-file drop     nr> restore-input drop

This new definition uses updated SAVE-INPUT and RESTORE-INPUT combined with N>R and NR>, making the code of INCLUDE-FILE and EVALUATE more compact, thus saving some memory.

(10-06-2021 11:35 PM)Helix Wrote:  I'm too in favor of simple systems. When I tried different Forth packages some years ago, I found that F-PC Forth has 1523 words! I think this defeats the purpose of Forth.
Adding optional definitions for those who are interested is a better solution.

Wow. 1523 words is way too much and unnecessary for most applications. However, at least we should incorporate the most useful standard word sets in Forth500 and include E500-specific words for graphics, sound and the file system. It is always possible to load extra words from source files.

I'd like to add that at this point anyone interested in this project can suggest improvements and additions to the Forth500 core, as long as there is sufficient space for user programs. Placing the additions in source files to load on demand is probably best.

As a note about standard Forth compliance, I noticed that there is no REQUIRE and REQUIRED implemented yet in Forth500. So I came up with the following quick-and-dirty implementation that simply stores the filename in the dictionary with a trailing blank in the name:

Code:
: REQUIRED     which-pocket dup>r swap dup>r cmove     bl 2r@ + c!     2r@ 1+ find-word nip if r>drop r>drop exit then     2r@ included     2r> 1+ (created) ; : REQUIRE     parse-name required ;

Adding a trailing blank means that the filename word in the dictionary cannot clash with other words and cannot even be executed by accident. This word will be added after the file was successfully INCLUDED. ANEW and MARKER in the loaded file will also delete the filename, thus the next REQUIRE will load the file again if it was deleted from memory. It's a bit of a hack, but should work I believe. Note that the code above uses a new built-in system word (CREATED), which I added as a replacement of (LINK) and (NAME).

Updating the code and testing all of the additions and improvements will take a bit of time, but hopefully not too long.

I also would like to run the NQUEENS benchmark as suggested by xerxes. To this end, based on my understanding of the NQUEENS benchmarks I changed the NQUEENS Forth code slightly to use "nicer" standard Forth constructs, such as VALUE and by using POSTPONE to compile inlined versions of RCLAA and STOAA instead of calling them (disclaimer: this is yet untested on my end):

Code:
ANEW _NQUEENS_  8 CONSTANT RR  0 VALUE SS  0 VALUE XX  0 VALUE YY  CREATE AA RR 1+ ALLOT  : RCLAA POSTPONE AA POSTPONE + POSTPONE C@ ; IMMEDIATE  : STOAA POSTPONE AA POSTPONE + POSTPONE C! ; IMMEDIATE  : NQCORE    0 TO SS    0 TO XX    BEGIN      1 +TO XX RR XX STOAA      BEGIN        1 +TO SS        XX TO YY        BEGIN YY 1 > WHILE          -1 +TO YY          XX RCLAA YY RCLAA - DUP          0= SWAP ABS XX YY - = OR IF            0 TO YY            BEGIN XX RCLAA 1- DUP XX STOAA 0= WHILE              -1 +TO XX            REPEAT          THEN        REPEAT      YY 1 = UNTIL    RR XX = UNTIL  ;  : NQUEENS    ( STARTTIMER )    NQCORE    ." S=" SS    ( DISPLAYTIMER ) CR  ;
NQCORE should be run in a loop to execute multiple times to get a manual stopwatch timing (the E500 has no RTC or system clock).

There are still a few speed and code size optimizations to make in Forth500, which can affect this benchmark. Safety versus performance is an important consideration. For example, I opted to test for the BREAK key (+15 CPU cycles) and to check stack overflows (+19 CPU cycles) but only when necessary and not too frequently. BREAK key tests are only done when a colon definition is called and in loops. Stack overflow checks are done only in loops and when interpreting Forth code from an input source to keep the overhead low. I also reduced the overhead of stack checking to just 19 cycles with some coding tricks. Removing these tests speeds things up, but at the cost of possible runaway programs when a coding mistake was made.

Inspiration for picking up pceForth to create an updated version Forth500 came from SuperForth and Forth for HP-71B. Back in the 80s my first encounter and tryout with Forth was SuperForth for the QL by Garry Jackson. I learned the language, studied the Forth implementation in detail and wrote some stuff, but found the lack of file access to load files wanting. It supported blocks, which even at that time it felt like a huge step back to ancient times. So no blocks in Forth500, but blocks can be added from a source file if necessary.

There will be more to come soon, hopefully in a couple of days when I'm back to work on this project.

- Rob

"I count on old friends" -- HP 71B,Prime|Ti VOY200,Nspire CXII CAS|Casio fx-CG50...|Sharp PC-G850,E500,2500,1500,14xx,13xx,12xx...
 « Next Oldest | Next Newest »

 Messages In This Thread FORTH for the SHARP PC-E500 (S) - Helix - 09-06-2021, 11:41 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-12-2021, 02:00 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-12-2021, 11:40 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-13-2021, 01:12 AM RE: FORTH for the SHARP PC-E500 (S) - xerxes - 09-15-2021, 11:51 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-08-2021, 04:22 AM RE: FORTH for the SHARP PC-E500 (S) - xerxes - 11-09-2021, 10:59 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-16-2021, 12:47 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-16-2021, 05:06 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-17-2021, 08:45 PM RE: FORTH for the SHARP PC-E500 (S) - toml_12953 - 09-17-2021, 09:25 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-22-2021, 10:55 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-21-2021, 06:12 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-19-2021, 07:14 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-28-2021, 03:28 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-28-2021, 09:03 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-29-2021, 09:23 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 09-29-2021, 10:27 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 09-30-2021, 09:42 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-04-2021, 06:43 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-05-2021, 12:28 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-05-2021, 01:02 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-06-2021, 02:41 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-06-2021, 11:35 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-07-2021 05:55 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-07-2021, 11:48 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-08-2021, 01:04 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-10-2021, 01:38 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-08-2021, 05:36 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-08-2021, 07:16 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-10-2021, 09:11 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-10-2021, 01:22 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-10-2021, 01:36 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-13-2021, 02:38 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-26-2021, 05:45 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-29-2021, 11:46 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-28-2021, 03:11 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 10-29-2021, 04:03 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-30-2021, 07:09 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 10-30-2021, 09:12 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 10-31-2021, 01:57 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-01-2021, 12:34 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-02-2021, 07:58 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-03-2021, 11:58 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-04-2021, 05:48 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-05-2021, 02:04 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-06-2021, 10:40 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-06-2021, 07:31 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-07-2021, 01:15 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-07-2021, 01:00 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-07-2021, 03:40 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-07-2021, 05:44 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-07-2021, 08:19 PM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-09-2021, 08:23 AM RE: FORTH for the SHARP PC-E500 (S) - Klaus Overhage - 11-13-2021, 12:07 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-13-2021, 07:19 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-22-2021, 05:42 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 11-24-2021, 11:33 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 11-25-2021, 02:54 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 01-12-2022, 11:06 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 01-13-2022, 03:40 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 01-16-2022, 09:22 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 01-18-2022, 12:32 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 02-19-2022, 09:40 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 02-20-2022, 09:13 PM RE: FORTH for the SHARP PC-E500 (S) - Helix - 02-20-2022, 11:12 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 04-03-2022, 12:53 AM RE: FORTH for the SHARP PC-E500 (S) - Helix - 04-03-2022, 11:06 PM RE: FORTH for the SHARP PC-E500 (S) - robve - 04-04-2022, 12:47 AM RE: FORTH for the SHARP PC-E500 (S) - robve - 06-21-2022, 02:16 AM RE: FORTH for the SHARP PC-E500 (S) - F-73P - 06-22-2022, 02:10 AM

User(s) browsing this thread: 1 Guest(s)