Stackbased programming language

Related tags

Scripting rack
Overview

Rack is a stackbased programming language inspired by Forth, every operation push or pop on the stack. Because the language is stackbased and for a very simple parsing, the language uses the reverse polish notation (2 3 + instead of 2 + 3). For easier reading of a codebase, some functionnality can be written without this notation (like function argument) but this is not required.

This language is simply a toy but is fully fleged, if you want to do something there is a way to do it. The standard library implement some default feature documented bellow, if more are necessery you must develop them yourself. Rack will never be optimised, it is outside the scope of this project. Implementing rack with LLVM (or an alternative) is not really an option because of the paradigm of the language. It could be done but would remove the reason why this paradigm was choosen (easier compiler devlopment).

Rack only support windows because it uses the windows api.

Compiling

To compile a rack file, use the next command to generate assembly:

rack.exe 
   

   

And you will also need to compile and link the assembly file:

nasm -f win64 output.asm -o output.obj & link output.obj /subsystem:console /entry:_start /out:output.exe kernel32.lib

And finally, run the output:

.\output.exe

Hello world

You can write the Hello world with a common syntax like this:

std::println_str("Hello, world!")

But this will be changed internally to something like this before being compiled:

"Hello, world!" std::println_str

This change is done because rack uses the reverse polish notation. You can pass arguments to functions ubt arguments of functions are only the top most values on the stack, therefore you can also just push the arguments on the stack.

Types (all immutable)

str: Static c-string like. They are written with double quotes.

char: One character, represented internally as a number. They are written with single quotes.

int: 64 bits integer.

ptr: 64 bits integer pointing to an other value.

Intrinsics

Memory operations

mem: Push a pointer to an internal memory buffer of 256 bytes that can be used freely. For more memory, see std::alloc

dup: Duplicate and push the top most value on the stack.

over: Duplicate and push the second value on the stack.

drop: Drop (pop) the topmost value of the stack.

swap: Swap the 2 top most values of the stack.

rot: Bring the 3rd value of the stack to the top.

put : Save a value to a variable named (pop the last value off the stack).

fetch or ! : Push the value of the variable on the stack.

Arithmetic operations

+, -, *, /, %: Pop the 2 top most values off the stack and push back the result.

&, |: Pop the 2 top most values off the stack and push back the result of a binary or and binary and. Can also be used as a logical and and logical or.

Comparison operations

=, !=, >, >=, <, <=: Pop the 2 top most values off the stack and push 1 if true or 0 if false

Control flow

Execute the while the condition is still true.

while 
   
     do 
    
end

   

Execute the if the condition is true and execute the if the condition is false.


   
     if
    
    
     
else
    
     
      
end

     
    
   

Save a constant to be used (without a fetch instruction). The can only be an integer.

const 
    
    
      end

    
   

A function that can be called. Arguments are passed on the stack. Arguments type annotation is mandatory and a check is being done.

fn 
   
    [
    
      -> 
     
      ]
    
end

     
    
   

System calls (with the windows api)

Before using a windows api function, you must declare the number of argument that the function uses as a const

const WriteConsoleA 4 end
const GetStdHandler 1 end

You can now call the WriteConsoleA windows function by appending sys:: before the function name:

sys::WriteConsoleA(sys::GetStdHandler(-11), "Hello", 5, mem)

In this example, WriteConsoleA will print to the standard output "Hello".

Some standard functions

str::find_char[int, str|ptr -> int]

Will find the first occurence of a char in a str and return the index of the char (or -1 if the char was not found). Usage : str::find_char( , )

str::find_char_from[int, int, str|ptr -> int]

Will find the first occurence of a char in a str starting at the n position and return the index of the char (or -1 if the char was not found). Usage : str::find_char( , , )

str::len[str|ptr -> int]

Return the length of a string. Usage: str::len( )

str::print[str|ptr -> void]

Print a string to the stdout on the current line. Alias for std::print_str Usage: str::print( )

std::exit[void]

Exit the current program with an exit code 0. Usage: std::exit

std::assert[int, int, str|ptr -> void]

Assert that the 2 values are equal, if not print the string and exit the program. Usage: std::assert(0, 0, "0 equals to 0")

std::print_str[str|ptr -> void]

Print a string to the stdout on the current line. Alias for str::print Usage: std::print_str( )

std::print_int[int -> void]

Print an integer to the stdout on the current line and add a new line after. Usage: std::print_int( )

std::throw[str|ptr -> void]

Print the given message and exit the program with error code 0. Usage: std::throw( )

std::alloc[int -> ptr]

Allocate the specifed size on the heap and return a pointer to the memory allocated. Usage: std::alloc( )

std::realloc[ptr, int -> ptr]

Reallocate a memory block with bigger capacity and return the new address of the pointer. Usage: std::realloc( , )

std::free[ptr -> void]

Free an allocated memory block. Usage: std::free( )

More functions

For more functions and better documentation, directly read /tests/std.rk, /tests/string.rk, /tests/str.rk and /tests/vec.rk. They are also good examples of how to write a program in rack.

Rack binary usage

Compiling a program

use rack.exe

Debug the stack

This helps when you want to visualize the stack of a function and see what each operation really does: rack.exe --debug-stack

You might also like...
Lisp dialect scripting and extension language for Rust programs

Ketos Ketos is a Lisp dialect functional programming language. The primary goal of Ketos is to serve as a scripting and extension language for program

Source code for the Mun language and runtime.

Mun Mun is a programming language empowering creation through iteration. Features Ahead of time compilation - Mun is compiled ahead of time (AOT), as

Rhai - An embedded scripting language for Rust.

Rhai - Embedded Scripting for Rust Rhai is an embedded scripting language and evaluation engine for Rust that gives a safe and easy way to add scripti

Interpreted language developed in Rust

Xelis VM Xelis is an interpreted language developed in Rust. It supports constants, functions, while/for loops, arrays and structures. The syntax is s

Interactive interpreter for a statement-based proof-of-concept language.

nhotyp-lang Nhotyp is a conceptual language designed for ease of implementation during my tutoring in an introductive algorithmic course at Harbin Ins

Scripting language focused on processing tabular data.
Scripting language focused on processing tabular data.

ogma Welcome to the ogma project! ogma is a scripting language focused on ergonomically and efficiently processing tabular data, with batteries includ

The Devils' Programming Language (Quantum Programming Language)

devilslang has roots in Scheme and ML-flavored languages: it's the culmination of everything I expect from a programming language, including the desire to keep everything as minimalistic and concise as possible. At its core, devilslang is lambda-calculus with pattern-matching, structural types, fiber-based concurrency, and syntactic extension.

An Interpreter for Brainfuck programming language implemented in the Rust programming language with zero dependencies.

Brainfuck Hello, Visitor! Hey there, welcome to my project showcase website! It's great to have you here. I hope you're ready to check out some awesom

Programming language made by me to learn other people how to make programming languages :3
Programming language made by me to learn other people how to make programming languages :3

Spectra programming language Programming language made for my tutorial videos (my youtube channel): Syntax Declaring a variable: var a = 3; Function

Bell is a work in progress programming language that compiles to MCfunction (Minecraft's language for creating datapacks).

Bell is a work in progress programming language that compiles to MCfunction (Minecraft's language for creating datapacks). It provides a higher level,

tr-lang is a language that aims to bring programming language syntax closer to Turkish.

tr-lang Made with ❤️ in 🇹🇷 tr-lang is a language that aims to bring programming language syntax closer to Turkish. tr-lang is a stack based language

A repository for showcasing my knowledge of the Rust programming language, and continuing to learn the language.

Learning Rust I started learning the Rust programming language before using GitHub, but increased its usage afterwards. I have found it to be a fast a

Trulang is an interpreted language that is designed to be a simple, easy to learn, and easy to use programming language.

Trulang is an interpreted language that is designed to be a simple, easy to learn, and easy to use programming language.

Nyah is a programming language runtime built for high performance and comes with a scripting language.

🐱 Nyah ( Unfinished ) Nyah is a programming language runtime built for high performance and comes with a scripting language. 🎖️ Status Nyah is not c

Virtual Machine Language - Yet another stack-based programming language
Virtual Machine Language - Yet another stack-based programming language

Virtual Machine Language - Yet another stack-based programming language

lelang programming language is a toy language based on LLVM.

lelang leang是一门使用Rust编写,基于LLVM(inkwell llvm safe binding library)实现的编程语言,起初作为课程实验项目,现在为个人长期维护项目。 Target Features 支持8至64位的整形类型和32/64位浮点 基本的函数定义,调用,声明外部

The Amp programming language: a language designed for building high performance systems.

A language designed for building high performance systems. Platform Support x86_64-pc-windows ✅ x86_64-unknown-linux ⚠️ untested x86_64-unknown-darwin

Nexa programming language. A language for game developers by a game developer
Nexa programming language. A language for game developers by a game developer

NexaLang Nexa programming language. A language for game developers by a game developer. Features High-Level: Nexa is an easy high level language Two M

RustBoyAdvance-NG is a Nintendo™ Game Boy Advance emulator and debugger, written in the rust programming language.
RustBoyAdvance-NG is a Nintendo™ Game Boy Advance emulator and debugger, written in the rust programming language.

RustBoyAdvance-NG Nintendo GameBoy Advance ™ emulator and debugger, written in rust. WebAssembly Demo: https://michelhe.github.io/rustboyadvance-ng/ P

Owner
Xavier Hamel
Xavier Hamel
A computer programming language interpreter written in Rust

Ella lang Welcome to Ella lang! Ella lang is a computer programming language implemented in Rust.

Luke Chu 64 May 27, 2022
Oxide Programming Language

Oxide Programming Language Interpreted C-like language with a Rust influenced syntax. Latest release Example programs /// recursive function calls to

Arthur Kurbidaev 113 Nov 21, 2022
The hash programming language compiler

The Hash Programming language Run Using the command cargo run hash. This will compile, build and run the program in the current terminal/shell. Submit

Hash 13 Nov 3, 2022
🍖 ham, general purpose programming language

?? ham, a programming language made in rust status: alpha Goals Speed Security Comfort Example fn calc(value){ if value == 5 { return 0

Marc Espín 19 Nov 10, 2022
A small programming language created in an hour

Building a programming language in an hour This is the project I made while doing the Building a programming language in an hour video. You can run it

JT 40 Nov 24, 2022
The Loop programming language

Loop Language Documentation | Website A dynamic type-safe general purpose programming language Note: currently Loop is being re-written into Rust. Mea

LoopLanguage 20 Oct 21, 2022
REPL for the Rust programming language

Rusti A REPL for the Rust programming language. The rusti project is deprecated. It is not recommended for regular use. Dependencies On Unix systems,

Murarth 1.3k Dec 20, 2022
sublingual: toy versions of existing programming languages

sublingual: toy versions of existing programming languages This is a collection of toy languages created by taking much "larger" languages (e.g. Rust)

Eduard-Mihai Burtescu 20 Dec 28, 2022
A rusty dynamically typed scripting language

dyon A rusty dynamically typed scripting language Tutorial Dyon-Interactive Dyon Snippets /r/dyon Dyon script files end with .dyon. To run Dyon script

PistonDevelopers 1.5k Dec 27, 2022
A static, type inferred and embeddable language written in Rust.

gluon Gluon is a small, statically-typed, functional programming language designed for application embedding. Features Statically-typed - Static typin

null 2.7k Dec 29, 2022