Shop OBEX P1 Docs P2 Docs Learn Events
How to use MLOAD? - Solved: ( EVALUATE) — Parallax Forums

How to use MLOAD? - Solved: ( EVALUATE)

Christof Eb.Christof Eb. Posts: 1,213
edited 2023-12-03 06:41 in Forth

Hi,
can anyone direct me to some example, how to use mload ( or another word ) to immediately interprete source code, which is given in a string?
The problem is, that the code is only executed, AFTER switching back to the interpreter.
Thank You in advance! Christof

TAQOZ# : ml ---
   " 1 .  " ---
   dup ---
   dup len$ 1- + $000D swap ! ---
   mload ---
   2 . ---
; ---  ok
TAQOZ#  ---  ok

TAQOZ# ml --- 2  ok
TAQOZ# 1 .  --- 1  ok

Comments

  • Christof Eb.Christof Eb. Posts: 1,213
    edited 2023-12-03 07:11

    .... OK solved, it's EVALUATE and you have to append CR NULL to the string.

    : ml
       " 1 .  2 . fedFile$ PRINT$   "
       dup dup len$ 1- + $000D swap w!
       evaluate
       ." End"
    ;
    ---  ok
    TAQOZ# ml --- 1 . 1  2 . 2 fedFile$ PRINT$ men.fth End ok
    TAQOZ#
    

    Mload like fload do not switch into the interpreter, they only switch the keyboard stream to ram or to file, so that it will be used for any KEY command.

    Edit: Switching back to normal after evaluate is still not always fine.

  • Here is a modified evaluate, which already has CR appended instead of BLANK and which switches back to normal serial keyboard before it envokes a word. Also it stops evaluation, if backslash comment appears. In this case you have to type CR.

    : evalKey
       nx' @ C@
       DUP   dup 92 <> and IF \ not NULL and not backslash
          nx' ++
       ELSE drop 0 ukey W~ THEN
    ;
    
    : eval ( str -- ) \ evaluate
       $0D OVER DUP LEN$ + W! \ add return was $20
       nx' !
       ' evalKey ukey W!
       BEGIN
       GET$ DUP LEN$ WHILE
          NUMBER 0= IF
             @WORD ukey W~ CALL$
             ' evalKey ukey W!
          THEN
       REPEAT
       DROP
    ;
    
    : evTest
       1 .
       " 2 . 3 . " eval
       4 .
    ;
    
    TAQOZ#  ---  ok
    TAQOZ# evTest --- 1 2 . 2 3 . 3 4  ok
    TAQOZ#
    
Sign In or Register to comment.