16 KiB
guitarvydas/forthish
Source: https://github.com/guitarvydas/forthish
GitHub - guitarvydas/forthish
Navigation Menu
Toggle navigation
Appearance settings
-
Platform
- AI CODE CREATION
- DEVELOPER WORKFLOWS
- APPLICATION SECURITY
- EXPLORE
-
Solutions
- BY COMPANY SIZE
- BY USE CASE
- BY INDUSTRY
-
Resources
- EXPLORE BY TOPIC
- EXPLORE BY TYPE
- SUPPORT & SERVICES
-
Open Source
- COMMUNITY
- PROGRAMS
- REPOSITORIES
-
Enterprise
Search or jump to...
Search code, repositories, users, issues, pull requests...
Search
Clear
Provide feedback
We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contacted
Cancel Submit feedback
Saved searches
Use saved searches to filter your results more quickly
Name
Query
To see all available qualifiers, see our documentation.
Cancel Create saved search
Appearance settings
Resetting focus
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
{{ message }}
guitarvydas / forthish Public
- Notifications You must be signed in to change notification settings
- Fork 0
- Star 1
1 star 0 forks Branches Tags Activity
Notifications You must be signed in to change notification settings
Additional navigation options
guitarvydas/forthish
main
Go to file
Code
Open more actions menu
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
| Latest commit History26 Commits 26 Commits | ||||
| .hg | .hg | |||
| .obsidian | .obsidian | |||
| assets | assets | |||
| basic | basic | |||
| course | course | |||
| frish | frish | |||
| .gitignore | .gitignore | |||
| .hgignore | .hgignore | |||
| INSTALL-hg.md | INSTALL-hg.md | |||
| INSTALL.md | INSTALL.md | |||
| README.md | README.md | |||
| View all files |
Repository files navigation
Forth in $WHATEVER
This is a collection of Forth (or Forth-like) implementations in various languages. It is intended to provide examples of how you can leverage Forth to simplify and add extensibility to your programs.
Python Implementations
simple.py
The simplest thing that could be called a Forth (Mr. Moore will probably disagree with me). However, it provides a useful parser, stack and interface to the host system. Words in this implementation:
- + ( a b -- sum) Add two numbers on top of stack.
- - ( a b -- difference) Subtracts top number from next on stack.
- . ( n --) Pop top of stack and print it.
- .s ( --) Print contents of stack (nondestructive).
- bye ( --) Exit interpreter.
- interpret ( string --) Execute word on top of stack.
- swap ( a b -- b a) Swaps top two items on stack.
- word ( c -- string) Collect characters in input stream up to character c.
fram.py
This implements a semi-traditional Forth dictionary in the RAM array. Words are stored with a name field, link field and code field. Instead of finding things directly with a Python dictionary, the ' word searches for the xt, or "execution token", to be executed.
In real terms, a little slower, but offers some exciting benefits, which I'll explore later.
- ' ( name -- xt) Finds word's xt in dictionary.
- execute ( xt --) Executes the code at xt.
- negate ( n -- -n) Makes number negative (or positive, if you use it twice).
- words ( --) Prints list of all words in dictionary.
fvars.py
Now we're getting into memory manipulations with variables and such. This introduces some new stuff in the code field -- a constant or variable is just like any other Forth word, except the code retrieves the values (or address of the values, for variables).
- ! ( v a --) Store value at address a.
- , ( v --) Store v as next value in dictionary.
- @ ( a -- v) Fetch value from address a.
- constant ( name | v --) Create constant with value v.
- create ( name | --) Create word name in dictionary.
- dump ( start n --) Dump n values starting from RAM address a.
- variable ( name | v --) Create variable name, with initial value v.
fcomp.py
Stuff is getting tricky now... This implements user definitions. Also reworks some previous words.
Variables
- state Variable for interpreter state -- 0 = interpret, 1 = compile.
Special Words for Compilation
These words are used during compilation to handle literal values and control flow. You will generally not need to use them.
- (literal) Used when compiling literal values in definitions.
- branch Unconditional branch.
- 0branch Branch on false.
Words for Definitions
The colon and semicolon begin and end a Forth definition. The if/else/then triad can only be used inside a definition (in traditional Forth).
- : ( name | --) Start compiling new definition.
- ; ( --) End definition.
- if ( f --) Evaluate based on flag f.
- else ( --) What to do when if is false.
- then ( --) End if/else/then clause, continue with normal execution.
Regular Words
This is a grab bag of stuff that I found useful as I went. Notably, the " (quote) word is state-smart, meaning it can be used outside and inside a definition. Now that you know that, forget it...
- find ( name | -- name 0|xt 1|xt -1) Search for word name.
- ( ( --) Start inline comment, reads until closing paren.
- ." ( --) Prints text up to closing quote.
- " ( -- s) Reads text up to closing quote as a string, puts on stack.
- / ( a b -- div) Divides a by b.
- * ( a b -- product) Multiplies a times b.
- cr ( --) Carriage return.
- emit ( c --) Prints ascii character c.
- dup ( a -- a a) Duplicate TOS.
- drop ( a --) Discards TOS.
If you care, this is effectively a DTC (Direct Threaded Code) implementation.
Explanation of how colon definitions work goes here...
References
Some links to more information that will help in understanding why you might do with this and what to do with it once you've got it. If nothing else, please read Walker's essay at the first link.
Essential
- ATLAST -- My first encounter with this concept, written by the legendary John Walker. He explains it better than I can.
- Starting Forth -- Classic Forth tutorial and textbook. Don't be fooled by the illustrations, this book not only teaches you how to use Forth, but actually gives you enough information to write your own as well.
- Levels of FORTH -- Glen Haydon's taxonomy of Forth implementations, slightly outdated, but useful if you're trying to figure out what functionality to implement next.
Supplemental
- Forth Standard Yes, Forth has a language standard. Don't feel like you need to follow it slavishly, though...
- JONESFORTH -- A full implementation of Forth, in assembly.
- Thinking Forth -- A Language and Philosophy for Solving Problems -- Leo Brodie's masterwork, read this to figure out how to really use your new Forth implementation.
Historical
- Forth - The Early Years -- Potted history of Forth, by its creator, Chuck Moore.
- Programming a Problem Oriented Language -- Unpublished book by Chuck Moore describing early Forth.
About
No description, website, or topics provided.
Resources
Uh oh!
There was an error while loading. Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published
Languages
- Python 66.6%
- JavaScript 22.6%
- HTML 10.4%
- Other 0.4%
Footer
© 2026 GitHub, Inc.
Footer navigation
- Terms
- Privacy
- Security
- Status
- Community
- Docs
- Contact
- Manage cookies
- Do not share my personal information
You can’t perform that action at this time.