Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eatonphil/jsforth
A Forth REPL in Javascript.
https://github.com/eatonphil/jsforth
forth
Last synced: 23 days ago
JSON representation
A Forth REPL in Javascript.
- Host: GitHub
- URL: https://github.com/eatonphil/jsforth
- Owner: eatonphil
- License: mit
- Created: 2015-04-05T22:11:13.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-02-05T01:23:31.000Z (almost 2 years ago)
- Last Synced: 2024-10-13T04:13:05.728Z (about 1 month ago)
- Topics: forth
- Language: JavaScript
- Homepage: http://codepen.io/eatonphil/full/YPbWVN/
- Size: 120 KB
- Stars: 43
- Watchers: 5
- Forks: 12
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# JSForth
This is a micro-Forth implementation in Javascript. It is built around an HTML REPL.
I wrote this two years ago during a PL course in college. The code is not the greatest; it only took a few hours to throw together. That said, it is pretty neat.
# Try It Out
A demonstration REPL is available via CodePen [here](http://codepen.io/eatonphil/full/YPbWVN/).
Alternatively, it can be run locally by navigating to the provided index.html file.
# Built-in Commands
```
+ - / * ^ < > <= >= = !=
ex: a b + // displays: Stack: (a+b). - returns the top element of the Stack
ex: a b . // displays: b; Stack: a.s - displays the current Stack and the size
ex: a b .s // displays: a b <2>; Stack: a b.c - displays the top of the Stack as a character
ex: 0 97 .c // displays: a ; Stack: 0 97drop - pops off the top element without returning it
ex: a b drop // displays: nothing; Stack: apick - puts a copy of the nth element on the top of the Stack
ex: a b c 2 pick // displays: nothing; Stack: a b c arot - rotates the Stack clockwise
ex: a b c rot // displays: nothing; Stack: b c a-rot - rotates the Stack counter-clockwise
ex: a b c -rot // displays: nothing; Stack c a bswap - swaps the top two elements
ex: a b // displays: nothing; Stack: b aover - copies the second-to-last element to the top of the Stack
ex: a b over // displays: nothing; Stack: a b adup - copies the top element
ex: a b dup // displays: nothing; Stack: a b bif ... then - executes what follows "if" if it evaluates true, continues on normally after optional "then"
ex: a b > if c then d // displays: nothing; Stack: a b c d //if a > b; Stack: a b d //if a <= bdo ... [loop] - executes what is between "do" and "loop" or the end of the line
ex: a b c do a + // displays: nothing; Stack: adds a to itself b times starting at cinvert - negates the top element of the Stack
ex: a invert // displays: nothing; Stack: 0 //a != 0; Stack: 1 //a == 0clear - empties the Stack
ex: a b clear // displays: nothing; Stack:: - creates a new custom (potentially recursive) definition
ex: a b c : add2 + + ; add2 // displays: nothing; Stack: (a+b+c)allocate - reallocates the max recursion for a single line of input
ex: 10 allocatecls - clears the screen
debug - toggles console debug mode
```# Examples
## Basics
```
>>> 3 4 +
>>> .s
7
>>> 3 4 - .s
-1
>>> 3 4 < .s
1
>>> 3 4 > .s
0
>>> 3 dup .s
3 3
>>> = .s
1
>>> drop
>>> .s
```## Conditions
```
>>> 3 4 < if 11 then 12
>>> .s
11 12
>>> 3 4 > if 12 then 14 .s
14
```## Functions
```
>>> : plus + ;
>>> 2 3 plus
5
```## Loops
### Power Function
```
>>> : pow over swap 1 do over * loop swap drop ;
>>> 2 3 pow .s
8
```## Recursion
### Fibonacci
```
>>> : fib dup 1 > if 1 - dup fib swap 1 - fib + then ;
>>> 6 fib .s
8
```### Factorial
```
>>> : fac dup 1 > if dup 1 - fac * then dup 0 = if drop 1 then dup 1 = if drop 1 then ;
>>> 3 fac
6
```