All Projects → ForthScriptLang → forthscript

ForthScriptLang / forthscript

Licence: MIT license
Forthscript programming language interpreter

Programming Languages

C++
36643 projects - #6 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to forthscript

Dictu
Dictu is a high-level dynamically typed, multi-paradigm, interpreted programming language.
Stars: ✭ 191 (+1093.75%)
Mutual labels:  interpreter
AlchemyVM
WebAssembly Virtual Machine Built In Elixir
Stars: ✭ 184 (+1050%)
Mutual labels:  interpreter
SOMns
SOMns: A Newspeak for Concurrency Research
Stars: ✭ 62 (+287.5%)
Mutual labels:  interpreter
jsish
Jsi is a small, C-embeddable javascript interpreter with tightly woven Web and DB support.
Stars: ✭ 32 (+100%)
Mutual labels:  interpreter
slox
Swift implementation of a Lox interpreter
Stars: ✭ 39 (+143.75%)
Mutual labels:  interpreter
webasm-solidity
[DEPRECATED] On-chain interpreter for WebAssembly written in Solidity
Stars: ✭ 65 (+306.25%)
Mutual labels:  interpreter
monkey
The Monkey Programming Language & Interpreter written in PHP.
Stars: ✭ 21 (+31.25%)
Mutual labels:  interpreter
clri
An unfinished CIL interpreter in Rust
Stars: ✭ 22 (+37.5%)
Mutual labels:  interpreter
HaxeVM
Prototype compiler/virtual machine in Haxe for Haxe
Stars: ✭ 24 (+50%)
Mutual labels:  interpreter
basicv2
A Commodore (CBM) BASIC V2 interpreter/compiler written in Java
Stars: ✭ 73 (+356.25%)
Mutual labels:  interpreter
quickjs-build
Build for QuickJS JavaScript Engine
Stars: ✭ 25 (+56.25%)
Mutual labels:  interpreter
PDDL.jl
Julia parser, interpreter and compiler interface for the Planning Domain Definition Language (PDDL). Planners not included.
Stars: ✭ 52 (+225%)
Mutual labels:  interpreter
cidk
interpreter devkit
Stars: ✭ 23 (+43.75%)
Mutual labels:  interpreter
pyccolo
Declarative instrumentation for Python.
Stars: ✭ 70 (+337.5%)
Mutual labels:  interpreter
xemime
The Xemime programming language
Stars: ✭ 13 (-18.75%)
Mutual labels:  interpreter
foxscheme
An R5RS Scheme in JavaScript.
Stars: ✭ 15 (-6.25%)
Mutual labels:  interpreter
blade
A simple, fast, clean, and dynamic language that allows you to develop applications quickly.
Stars: ✭ 113 (+606.25%)
Mutual labels:  interpreter
fint
.NET CIL interpreter written in simple subset of F#
Stars: ✭ 50 (+212.5%)
Mutual labels:  interpreter
riptide
The Riptide Programming Language: Shell scripting redesigned.
Stars: ✭ 24 (+50%)
Mutual labels:  interpreter
uwuscript
World's first uwu-oriented language.
Stars: ✭ 75 (+368.75%)
Mutual labels:  interpreter

forthscript

Run on Repl.it C/C++ CI

This repository contains code for the reference implementation of the ForthScript programming language).

ForthScript is a stack-based programming language with automatic memory management, heavily inspired by Forth and Lisp. ForthScript and Forth are both stack-based concatenative programming languages. ForthScript extends Forth with additional features that make ForthScript slightly higher level - local and global variables, array operations and tracing garbage collection.

ForthScript borrows homoiconicity from Lisp - any ForthScript function is an array of instructions. In fact, there is no difference between code and data - all values in ForthScript can be interpreted as either.

The ForthScript is dynamically typed (variables and arrays can store values of any kind). Yet, types are still a thing in ForthScript, and so are type errors - one cannot add integer to a string.

Building ForthScript

ForthScript interpreter can be built as any other CMake C/C++ project. All you need is CMake and a C++ compiler. No third-party dependencies required.

mkdir build
cd build
cmake ..
cmake --build .

Build directory should now contain forthscript (or forthscript.exe) executable.

Running ForthScript

To launch ForthScript interpreter in REPL (Read-Eval-Print-Loop) mode, simply run the forthscript executable with no arguments

# Run REPL
./forthscript

To execute a file with ForthScript code, pass the path to the file as the first argument

# Run example.fscript
./forthscript example.fscript

.fscript is a standard file extension for ForthScript source files.

More on ForthScript REPL

You can try all examples below with REPL. When you run the interpreter in REPL mode (no CLI arguments), you should see the REPL prompt

[]
#

The first line visualises values on the stack. As the interpreter stack is empty on startup, there won't be anything between the brackets.

The second line is the prompt. Everything after # is interpreted as ForthScript code.

To quit the REPL session, type quit and press Enter.

Examples

"Hello, world"

"Hello, world!" writeln

Swap function

# $a declares a local variable and sets it to TOS (which gets removed)
[$a $b a b] $swap
"3" "4" swap! writeln writeln

FizzBuzz

# =a assings TOS to a
# for loop creates two scopes - one with $i and one with loop body
[1 $i] [i 30 <] [i 1 + =i] [
	i 15 % 0 == ["FizzBuzz" writeln continue] if
	i 5 % 0 == ["Buzz" writeln continue] if
	i 3 % 0 == ["Buzz" writeln continue] if
	i to_string writeln
] for

Infix arithmetic expressions (using homoiconicity)

# Run examples/expressions_compiler/main.fscript in this scope
# "parse" instruction parses string and outputs ForthScript array with instructions
# "," runs this array in this scope
"examples/expressions_compiler/main.fscript" readfile parse ,

# arith_compile function is now available in this scope
"arith_compile source:" writeln
arith_compile serialize writeln

# use arith_compile to transform arithmetic expressions
"compiling: " write
[2 + 2 * 2] $source source serialize writeln
"got: " write
source arith_compile! $compiled compiled serialize writeln
"result: " write
# arith_compile also returns an array of instructions, which we can just run
compiled! to_string writeln # 6
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].