Menu
  • HOME
  • TAGS

How do I create a basic class and instance of that class in Forth?

class,oop,object,forth,gforth

With some simple gforth symbols: : symbol ( "name" -- ) create lastxt , does> ( -- xt ) @ ; : .symbol ( xt -- ) >name name>string 1 /string type ; symbol 'volts symbol 'amps Here's an oof.fs equivalent of your Python: require oof.fs object class power-reading float...

C to Forth parser using Bison

c,parsing,bison,yacc,forth

If you use the strategy of immediately producing the translation for each production, it becomes impossible to reorder clauses. That's a severe limitation. There are a number of hacks in common use to get around the problem. One is to provide a mechanism whereby output for a production can be...

New lines in word definition using interpreter directives of Gforth

forth,gforth

It would really be best to use an immediate word instead, f.e. : ones ( n -- ) 0 ?do 1 postpone literal loop ; immediate : foo ( -- ten ones ) [ 10 ] ones ; With SEE FOO resulting in the same as your example. With POSTPONE,...

How to use ReadConsoleOutputCharacterA in Forth?

forth,gforth

hConsoleOutput there is just an appropriate handle as is. Some usage example for ReadConsoleOutputCharacter can be found in devel directory (contribution). Example of this API usage: \ Global variables in dictionary space just for learning, \ -- don't use such approach, especially in multithreading. VARIABLE lpNumberOfCharsRead CREATE lpCharacter 5 CHARS...

Big integer in Forth?

biginteger,forth,gforth

The Forth Scientific Library includes Arithmetic on big signed-magnitude numbers module. Although, a test is required to see what implementation is faster. To use this module in SP-Forth some libraries should be included: REQUIRE [IF] lib/include/tools.f REQUIRE M+ lib/include/double.f S" big.fth" INCLUDED \ just for example In any case, if...

How to write a while() loop in Gforth

forth,gforth

Your first piece of code translates to: \ Assuming x is on the top of the stack. begin dup 3 > while dup . 1- repeat \ Or if x is in memory. begin x @ 3 > while x ? -1 x +! repeat And the second: begin predicate...

Show the return stack

forth,gforth

I think you could use this code if you are curious about the return stack: : RTEST [email protected] DUP >R [email protected] RDROP SWAP - ; RTEST CONSTANT RSTEP [email protected] CONSTANT RBIAS : .RETURNSTACK \ -- [email protected] RBIAS DO I @ U. RSTEP +LOOP CR ; ...

Is it okay to use dictionary memory without 'allot'?

memory-management,forth

Language-lawyer strictly speaking, no. ANS Forth 3.3.3.2 states: A program may perform address arithmetic within contiguously allocated regions. You are performing address arithmetic outside any allocated region. However, it might be perfectly fine in some particular implementation. Such as gforth. Note that there is a word called PAD, which returns...

Error when appending string from word or variable

forth,gforth

append uses resize on the first string make space to append the second string. This requires that the string be allocated on the heap. When you compile a string with s" into a word, it gets allocated in the dictionary. If you try resize (directly or indirectly through append) on...

How do 2>r and 2r> work?

forth,gforth

2>r (and the Forth 200x word n>r) preserves the order of the elements pushed to the return stack. So if the you have ( 1 0 ) on the data stack, with 0 as the top of the stack, then after 2>r you will have 0 at the top of...

Load from the terminal input buffer to parameter stack

forth,gforth

The parameter stack grows towards low memory The main problem with the sample code is that the parameter stack grows towards low memory. So the starting point for the destination of the copy should be at a higher memory address (inside the existing/defined parameter stack). So instead of TIB [email protected]