Giter Site home page Giter Site logo

runpack's Introduction

RunPack

Minimalistic, yet practical, scripting language

Introduction

RunPack is a small and modular scripting language written in ~1000 lines of Rust code, and designed to be embedded.

It is a concatenative, stack-based, homoiconic, async/await oriented programming language, strongly inspired by Forth, and to a lesser extent by Factor, Racket, and Rust.

Language and implementation are designed to be extensible, providing a lightweight and powerful Rust API that allows to control any aspect of the language. Many features that in other languages are a core part of the compiler, like if/else/while statements, anonymous functions or variables, in RunPack are just words defined using the public API, or even RunPack itself.

RunPack is what you are looking for, if you...

  1. Need a scripting language for your Rust application.
  2. Want it to be small, simple and modular.
  3. Require something fully customizable and hackable.
  4. Appreciate concatenative programming languages.

Installation

Add the following line to your Cargo dependencies:

runpack = { git = "https://github.com/asllop/RunPack" }

Usage

To run a simple script that adds two numbers:

use runpack::{Pack, Cell};

let script = r#"
    "Add two integers, leaving the result in the stack"
    10 20 +
"#;

// Create the pack
let mut pack = Pack::new();
// Append code
pack.code(script);
// Run
pack.run().expect("Error running the script");
// Check results in the stack
if let Some(Cell::Integer(i)) = pack.stack.pop() {
    println!("Result = {}", i);
}

It's easy to define a new word in Rust and then call it from a script in RunPack:

use runpack::{self, Pack, Cell};

let script = r#"
    "Put a string in the stack and then execute the 'hi' word"
    'Andreu' hi
"#;

let mut pack = Pack::new();
// Define a word "hi" in Rust
pack.dictionary.native("hi", hi_word);
pack.code(script);
pack.run().expect("Error running the script");

fn hi_word(pack: &mut Pack) -> Result<bool, runpack::Error> {
    // Get a string from the stack and print it
    if let Some(Cell::String(name)) = pack.stack.pop() {
        println!("Hi {}!", name);
        Ok(true)
    }
    else {
        Err(runpack::Error::new("Couldn't get a string".into()))
    }
}

And it's also easy to define a word in RunPack and call it from Rust:

use runpack::{self, Pack, Cell};

let script = r#"
    "Define the word 'pi' that puts the number π in the stack"
    3.14159 def pi
"#;

let mut pack = Pack::new();
pack.code(script);
pack.run().expect("Error running the script");
// Execute word "pi"
pack.exec("pi").expect("Failed executing 'pi'");
// Check the stack for results
if let Some(Cell::Float(f)) = pack.stack.pop() {
    println!("The number π is {}", f);
}

Learn RunPack

Learning is easy, you only need a couple of hours of your time and this introductory tutorial. Additionally, we offer the RunPack REPL, a cli tool to facilitate the development of RunPack programs.

Enjoy the trip!

Documentation

There are two sources of documentation, one is the standard Rust autodoc, generated with cargo doc as usual. The other is the vocabulary of RunPack words offered by a module, that comes in a DOC.md file.

runpack's People

Contributors

asllop avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.