Measure execution time forth - forth

How to measure execution time of my program? I`ve found this but it doesnot work for me because my program needs some numbers in stack to work so my program and this solution kind of interfiering with each other(as I understand because when i try time: myfunc or time: num1 num2 myfunc nothing works....)
: time: ( "word" -- )
utime 2>R ' EXECUTE
utime 2R> D-
<# # # # # # # [CHAR] . HOLD #S #> TYPE ." seconds" ;
thanks for any help

The tick ' will parse the text immediately following the call to time:. So for a call with arguments it should be num1 num2 time: myfunc
Also, the tick will parse the input stream in run time. If you have the call to time: embedded in the word definition, it will try to get the execution toke for the word from the input stream, not for the word immediately following the time:. Use ['] to get an xt of the next word in compile time, and pass that xt as argument to time:
: time ( xt -- )
utime 2>R EXECUTE
utime 2R> D-
<# # # # # # # [CHAR] . HOLD #S #> TYPE ." seconds" ;
: foo 0 1000000 0 do i + loop drop ;
: bar num1 num2 ['] foo time ; \ pass the foo's xt as argument to time
Note there's no ' call in time, the EXECUTE will grab the token from the stack.

Related

Skip over input stream in ATLAST forth

I'm trying to implement a kind of "conditional :" in ATLAST, the reasoning being I have a file that gets FLOADed multiple times to handle multiple steps of my program flow (I'm essentially abusing Forth as an assembler, step 1 does a first parsing for references, etc. and in step 2 the instruction words actually emit bytes).
So when declaring words for "macros" in that file, it errors out in step 2, because they were already declared in step 1, but I also can't just FORGET them, because that would forget everything that came afterwards, such as the references I just collected in step 1.
So essentially I need a ": that only runs in step 1", my idea being something like this:
VARIABLE STAGE
: ::
STAGE # 0 = IF
[COMPILE] : ( be a word declaration )
EXIT
THEN
BEGIN ( eat the disabled declaration )
' ( get the address of the next word )
['] ; ( get the address of semicolon )
= ( loop until they are equal )
UNTIL
; IMMEDIATE
:: FIVE 5 ; ( declares as expected )
FIVE . ( prints 5 )
1 STAGE ! ( up to here everything's fine )
:: FIVE 6 ; ( is supposed to do nothing, but errors out )
FIVE . ( is supposed to print 5 again )
The traced error message (starting from 1 STAGE !):
Trace: !
Trace: ::
Trace: STAGE
Trace: #
Trace: (LIT) 0
Trace: =
Trace: ?BRANCH
Trace: '
Trace: (LIT) 94721509587192
Trace: =
Trace: ?BRANCH
Trace: '
Word not specified when expected.
Trace: ;
Compiler word outside definition.
Walkback:
;
KEY ( -- ch ) as common in some other Forths for reading a single character from the input stream ( outside the :: declaration, since it's IMMEDIATE ) doesn't exist in ATLAST, the only related words I could find are:
': is supposed to read a word from the input stream, then pushes its compile address
[']: like ' but reads a word from the current line (the inside of the :: declaration)
(LIT)/(STRLIT): are supposed to read literals from the input stream according to the documentation, I could only ever make them segmentation fault, I think they're for compiler-internal use only (e.g., if the compiler encounters a number literal it will compile the (LIT) word to make it push that number onto the stack)
There aren't any WORD or PARSE either, as in some other Forths.
As you can see, ' is struggling actually getting something from the input stream for some weird reason, and it looks like ['] is failing to capture the ; which then errors out because it's suddenly encountering a ; where it doesn't belong.
I suspect it actually ran ' ['], even though it's supposed to work on the input stream, not the immediate line, and I'm clearly in compile mode there.
I did a similar thing with conditionally declaring variables, there it was rather easy to just [COMPILE] ' DROP to skip a single word (turning RES x into ' x DROP), but here I'm pretty sure I can't actually compile those instructions, because I can't emit a loop outside of a declaration. Unless there is a way to somehow compile similar code that recursively gets rid of everything until the ;.
A problem is that ' cannot find a number. A possible solution is to use a special dummy name for the definition, instead of skip it over:
: ::
STAGE # 0 = IF : EXIT THEN
' DROP \ this xt isn't needed
" : _dummy" EVALUATE ( -- n ) DROP
;
Or maybe use a new name every time:
: ::
STAGE # 0 = IF : EXIT THEN
' >NAME # \ ( s1 ) \ should be checked
": _dummy_" DUP >R S+
R> EVALUATE ( -- n ) DROP
;
But due to non standard words it might not work. Another problem is that non colon-definitions are out of the scope.
Perhaps, a better solution is a preprocessing by external means.
It appears that ATLAST is a primitive Forth, that doesn't allow you to go to a more sophisticated handling of sources. But all is not lost!
For example, a Forth implementation according to the ISO standard will handle the matter with ease with one or more of: REQUIRE [IF] [THEN] [DEFINED] SRC >IN NAME WORD FIND.
As you have a Forth, you can steal these words from another Forth and compile the code.
Another solution that may help directly is executing EXIT in interpret mode while loading a file.
You have to find out whether you can create a flag whether to abandon the input source. Then this definition might help:
: ?abandon IF S" EXIT" EVALUATE THEN ;
S" FIVE" FOUND ?abandon
Note that ?abandon must be executed in interpret mode.

How to enter numbers in Forth

Is there something like input in Basic or scanf("%d") in C in Forth?
Probably it will be something like this:
200 buffer: buf
: input ( -- n ) buf 200 accept
some-magic-filter
buf swap evaluate ;
The problem in the above code, is how to define a filter that will pass only numbers, but not any words, definitions, etc?
The standard specifies only a low level >NUMBER word to interpret integer numbers.
OTOH using EVALUATE to convert strings into numbers is a quick and dirty way. Either use it without checks (in the case of trusted input) or do not use it at all. Trying to filter the string before EVALUATE is a bad idea: it has cost of >NUMBER word itself and low reusing factor.
NB: neither >NUMBER nor EVALUATE detects numeric overflow.
In any case, your word to input a single-cell integer can be defined something like:
: accept-number ( -- n )
PAD DUP 80 ACCEPT ( addr u ) StoN ( n )
;
In the case of trusted input you can define StoN like
: StoN ( addr u -- x )
STATE # ABORT" This naive StoN should not be used in compilation state"
DEPTH 2- >R
EVALUATE
DEPTH 1- R> <> IF -24 THROW THEN
\ check depth to accept the single-cell numbers only
;
Otherwise (in the case of untrusted input) you have two choices: to rely on the specific words of a particular Forth system or to use some (perhaps your own) library.
I use the following lexicon to define StoN:
\ ---
\ The words from Substring Matching library
\ (where length is counted in address units)
: MATCH-HEAD ( a u a-key u-key -- a-right u-right true | a u false )
2 PICK OVER U< IF 2DROP FALSE EXIT THEN
DUP >R
3 PICK R# COMPARE IF RDROP FALSE EXIT THEN
SWAP R# + SWAP R> - TRUE
;
\ ---
\ The words from Literals interpreting library
\ (where prefix 'I-' is shortcut for Interpret)
: I-DLIT ( a u -- x x true | a u false )
2DUP S" -" MATCH-HEAD >R
DUP 0= IF NIP RDROP EXIT THEN
0 0 2SWAP >NUMBER NIP IF RDROP 2DROP FALSE EXIT THEN
R> IF DNEGATE THEN 2SWAP 2DROP TRUE
;
: I-LIT ( a u -- x true | a u false )
I-DLIT IF D>S TRUE EXIT THEN FALSE
;
After that StoN can be defined as:
: StoN ( a u -- x ) I-LIT IF EXIT THEN -24 THROW ;
The mentioned libraries can be found at GitHub:
Substring matching functions library
Resolvers example (for various lexemes)
Rosetta Code suggests this code snippet, working with GForth 0.6.2, to determine if an input string is numeric:
: is-numeric ( addr len -- )
2dup snumber? ?dup if
0< if
-rot type ." as integer = " .
else
2swap type ." as double = " <# #s #> type
then
else 2dup >float if
type ." as float = " f.
else
type ." isn't numeric in base " base # dec.
then then ;
I built a BASIC like #INPUT word for Camel Forth to give BASIC users something more familiar. It takes more than one might think. It starts with $ACCEPT which can be used to like input with a string variable or memory block.
The definition of NUMBER? here is for single ints only but it compiles on GForth. It outputs true if conversion is bad; the reverse of SNUMBER?
DECIMAL
: NUMBER? ( addr len -- n ?) \ ?=0 is good conversion
( -- addr len) \ bad conversion
OVER C# [CHAR] - = DUP >R \ save flag for later
IF 1 /STRING THEN \ remove minus sign
0 0 2SWAP >NUMBER NIP NIP \ convert the number
R> IF SWAP NEGATE SWAP THEN \ negate if needed
;
: $ACCEPT ( $addr -- ) CR ." ? " DUP 1+ 80 ACCEPT SWAP C! ;
: #INPUT ( variable -- ) \ made to look/work like TI-BASIC
BEGIN
PAD $ACCEPT \ $ACCEPT text into temp buffer PAD
PAD COUNT NUMBER? \ convert the number in PAD
WHILE \ while the conversion is bad do this
CR ." Input error "
CR DROP
REPEAT
SWAP ! ; \ store the number in the variable
\ USAGE: VARIABLE X
\ X #INPUT

How to count locals in ANS-Forth?

While developing BigZ, mostly used for number theoretical experiments, I've discovered the need of orthogonality in the word-set that create, filter or transform sets. I want a few words that logically combinated cover a wide range of commands, without the need to memorize a large number of words and ways to combinate them.
1 100 condition isprime create-set
put the set of all prime numbers between 1 and 100 on a set stack, while
function 1+ transform-set
transform this set to the set of all numbers p+1, where p is a prime less than 100.
Further,
condition sqr filter-set
leaves the set of all perfect squares on the form p+1 on the stack.
This works rather nice for sets of natural numbers, but to be able to create, filter and transform sets of n-tuples I need to be able to count locals in unnamed words. I have redesigned words to shortly denote compound conditions and functions:
: ~ :noname ;
: :| postpone locals| ; immediate
1 100 ~ :| p | p is prime p 2 + isprime p 2 - isprime or and ;
1 100 ~ :| a b | a dup * b dup * + isprime ;
Executing this two examples gives the parameter stack ( 1 100 xt ) but to be able to handle this right, in the first case a set of numbers and in the second case a set of pairs should be produced, I'll have to complement the word :| to get ( 1 100 xt n ) where n is the numbet of locals used. I think one could use >IN and PARSE to do this, but it was a long time ago I did such things, so I doubt I can do it properly nowadays.
I didn't understand (LOCALS) but with patience and luck I managed to do it with my original idea:
: bl# \ ad n -- m
over + swap 0 -rot
do i c# bl = +
loop negate ;
\ count the number of blanks in the string ad n
variable loc#
: locals# \ --
>in # >r
[char] | parse bl# loc# !
r> >in ! ; immediate
\ count the number of locals while loading
: -| \ --
postpone locals#
postpone locals| ; immediate
\ replace LOCALS|
Now
: test -| a b | a b + ;
works as LOCALS| but leave the number of locals in the global variable loc#.
Maybe you should drop LOCALS| and parse the local variables yourself. For each one, call (LOCAL) with its name, and end with passing an empty string.
See http://lars.nocrew.org/dpans/dpans13.htm#13.6.1.0086 for details.

Nested Perform Loops in COBOL?

Why can I not do this nested perform loop in COBOL?
If I put END-PERFORM. in any line sooner than where I have the last one just before EXIT PROGRAM - it works. But I need the program to display the INPUT C value every time. in the outer perform loop. Its driving me nuts.
PROCEDURE DIVISION USING INPUTC CIPHER.
COMPUTE CIPHERMAX = CIPHER.
MULTIPLY -1 BY CIPHER
---> PERFORM VARYING CIPHER FROM 0 BY 1
UNTIL CIPHERMAX = CIPHER
DISPLAY 'This is loop number: ' CIPHER
INSPECT INPUTC CONVERTING
"avcdefghijklmnopqrstuvwxyz" to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
COMPUTE CONVERTNUM = FUNCTION MOD (CIPHER, 26)
INSPECT FUNCTION REVERSE(INPUTC) TALLYING LENGTHNUM FOR LEADING SPACES
COMPUTE LENGTHNUM = LENGTH OF CIPHER - LENGTHNUM
---> PERFORM UNTIL SENTRY = LENGTHNUM
IF ((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) > (FUNCTION ORD('Z')))
MOVE FUNCTION CHAR((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) - 26) TO RECHAR
ELSE
MOVE FUNCTION CHAR(FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) TO RECHAR
END-IF
IF (((FUNCTION ORD(INPUTC(SENTRY:1))) >= (FUNCTION ORD('A'))) AND
((FUNCTION ORD(INPUTC(SENTRY:1))) <= (FUNCTION ORD('Z'))))
IF ((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) > (FUNCTION ORD('Z')))
INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY RECHAR
ELSE
INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY RECHAR
END-IF
ELSE
INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY INPUTC(SENTRY:1)
END-IF
COMPUTE SENTRY = SENTRY + 1
---> END-PERFORM
DISPLAY INPUTC.
COMPUTE LOOPI = LOOPI + 1
--->END-PERFORM.
EXIT PROGRAM.
END PROGRAM SOLVE.
That nasty scope terminating period after DISPLAY INPUTC. is terminating the scope of the nested PERFORM statements. Get rid of the period and all should work fine.
The only periods you should ever use in the Procedure Division when coding a program to COBOL-85 standard are the ones required to terminate section and paragraph headers and another one to terminate the current paragraph, section or program.
You have a period in the display INPUTC statement. Remove the period and you should be ok. The Period always ends a statement.

Custom interpreter

I'm trying to write an interpreter in Gforth, but it doesn't work. All I get is an infinite list of num num num num ...
: ?refill
source nip >in # =
if
refill drop
then
;
: inter
begin
?refill
bl word find dup
if
state # =
if
." comp "
,
else
." exec "
execute
then
else
dup rot count >number
if
abort
then
drop drop state #
if
." lit "
['] lit , ,
else
." num "
then
then
again
;
inter
: test 10 20 ;
Your interpreter does work, it just does not block, see the first couple of words from the output:
num exec lit lit exec num num num ...
However, you leave a 0 on the stack somewhere, thats why you create a stack overflow, you can use ~~ in the code to check the stack and track the unconsumed 0.
Bernd Paysan has introduced Recognizers to GForth, I suggest you take a look at them, as they would ease your task of writing an interpreter.

Resources