Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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 97

drop - pops off the top element without returning it
ex: a b drop // displays: nothing; Stack: a

pick - 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 a

rot - 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 b

swap - swaps the top two elements
ex: a b // displays: nothing; Stack: b a

over - copies the second-to-last element to the top of the Stack
ex: a b over // displays: nothing; Stack: a b a

dup - copies the top element
ex: a b dup // displays: nothing; Stack: a b b

if ... 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 <= b

do ... [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 c

invert - negates the top element of the Stack
ex: a invert // displays: nothing; Stack: 0 //a != 0; Stack: 1 //a == 0

clear - 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 allocate

cls - 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
```