Introducing Forth.Net - Luca Bolognese

Introducing Forth.Net

Luca -

☕ 2 min. read

An im­ple­men­ta­tion of the Forth pro­gram­ming lan­guage for the .Net Framework. You can use it as a C# li­brary, in­ter­ac­tively with a CLI or com­pile the code to a com­pact bi­nary for­mat.

It is on GitHub.

Installation

dotnet tool install -g forth.net.program

Usage

# Start the CLI.
nforth

# Interpret some files and exit.
nforth Test1.fth Test2.fth -e bye

# Compile files to binary
nforth Test1.fth Test2.fth -e 's\" myfile.io" save'

# Execute a binary file
nforth -e 's\" myfile.io" load aword'

Notes

  • Save the user por­tion of the dic­tio­nary with S" myfile.io" save. You load it with S" myfile.io" load.
  • Save the whole dic­tio­nary, sys­tem in­cluded, with S" myfile.io" savesys. You load it with S" myfile.io" loadsys.
  • To use the li­brary, copy the file Vm.cs or ref­er­ence the nuget pack­age Forth.Net. Go here for us­age notes and im­ple­men­ta­tion de­tails.
  • See all the sup­ported words with words.
  • Perform a sys­tem test with testsys. Your re­sults should look like the text at the bot­tom of this doc.
  • Type debug if cu­ri­ous.
  • Type nforth --help for less used op­tions.
  • Call the .NET APIs with these words. (FYI just sta­tic meth­ods with strings or num­bers for now).
: escape s" System.Uri, System" s" EscapeDataString" .net ;
: sqrt s" System.Math" s" Sqrt" .net ;

Test out­put

CR CR SOURCE TYPE ( Preliminary test ) CR
SOURCE ( These lines test SOURCE, TYPE, CR and parenthetic comments ) TYPE CR
( The next line of output should be blank to test CR ) SOURCE TYPE CR CR

( Pass #1: testing 0 >IN +! ) 0 >IN +! SOURCE TYPE CR
( Pass #2: testing 1 >IN +! ) 1 >IN +! xSOURCE TYPE CR
( Pass #3: testing 1+ ) 1 1+ >IN +! xxSOURCE TYPE CR
( Pass #4: testing @ ! BASE ) 0 1+ 1+ BASE ! BASE @ >IN +! xxSOURCE TYPE CR
( Pass #5: testing decimal BASE ) BASE @ >IN +! xxxxxxxxxxSOURCE TYPE CR
( Pass #6: testing : ; ) : .SRC SOURCE TYPE CR ; 6 >IN +! xxxxxx.SRC
( Pass #7: testing number input ) 19 >IN +! xxxxxxxxxxxxxxxxxxx.SRC
( Pass #8: testing VARIABLE ) VARIABLE Y 2 Y ! Y @ >IN +! xx.SRC
( Pass #9: testing WORD COUNT ) 5 MSG abcdef) Y ! Y ! >IN +! xxxxx.SRC
( Pass #10: testing WORD COUNT ) MSG ab) >IN +! xxY ! .SRC
Pass #11: testing WORD COUNT .MSG
Pass #12: testing = returns all 1's for true
Pass #13: testing = returns 0 for false
Pass #14: testing -1 interpreted correctly
Pass #15: testing 2*
Pass #16: testing 2*
Pass #17: testing AND
Pass #18: testing AND
Pass #19: testing AND
Pass #20: testing ?F~ ?~~ Pass Error
Pass #21: testing ?~
Pass #22: testing EMIT
Pass #23: testing S"

Results:

Pass messages #1 to #23 should be displayed above
and no error messages

0 tests failed out of 57 additional tests
0 Webmentions

These are webmentions via the IndieWeb and webmention.io.