Like Lua, but in Rust, and different

Overview

AirScript

It's like Lua, but in Rust, and different.

Introduction

AirScript is a dynamically typed, interpreted language inspired by Lua and written in Rust.

Just like Lua, the interpreter is a stack-based virtual machine. The VM provides an API to manipulate this stack, allowing you to:

  • push/pop primitive values
  • push/pop custom Rust types
  • push/pop (and call) Rust functions
  • ...

Example:

use airscript::prelude::*;
use airscript::interpreter::{VM, FuncReturns};

fn square(vm: &mut VM<()>) -> Result<usize> {
  let a = vm.pop_integer()?;
  vm.push_integer(a * a);
  Ok(1)
}

fn main() {
  let mut vm: VM<()> = VM::new();

  vm.push_integer(2);
  vm.push_rust_function(square);
  vm.call(1, FuncReturns::Exactly(1)).unwrap();

  let res = vm.pop_integer().unwrap();
  println!("{}", res); // prints: 4
}

Unlike Lua, the syntax is inspired by Rust and Go.

Example:

func make_action(kind) {
  func action(fn) {
    print(kind <> ": " <> fn() <> "\n");
  }

  return action;
}

func greeter(name) {
  func greet() {
    return "hello " <> name;
  }

  return greet;
}

let say := make_action("say");
let greet := greeter("world");

say(greet); // prints: say: hello world

To provide your own Rust types to the VM:

use airscript::prelude::*;
use airscript::interpreter::{VM, FuncReturns};

#[derive(Debug, Clone)]
struct Vector {
  pub x: f64,
  pub y: f64,
}

fn vec_mag(vm: &mut VM<Vector>) -> Result<usize> {
  let v_ref = vm.pop_userdata()?;
  let v = v.lock().unwrap();
  let m = (v.x * v.x + v.y * v.y).sqrt();
  vm.push_float(m);
  Ok(1)
}

fn main() {
  let mut vm: VM<Vector> = VM::new();

  vm.push_userdata(Vector { x: 3f64, y: 4f64 });
  vm.push_rust_function(vec_mag);
  vm.call(1, FuncReturns::Exactly(1)).unwrap();

  let res = vm.pop_float().unwrap();
  println!("{}", res); // prints: 5
}

Disclaimer

This is still a Work In Progress:

  • the VM's API is not complete nor definitive
  • the syntax is not complete nor definitive
  • the semantics are not specified

License

This project is released under the terms of the MIT License.

You might also like...
It's like pixelfix, but quix.
It's like pixelfix, but quix.

pixelquix It's like pixelfix, but quix. Written in Rust for low-level speed Uses rayon for work-stealing parallelism Based on Jump Flood for impressiv

Web-based tool that allows browsing and comparing symbol and type information of Microsoft Windows binaries across different versions of the OS.
Web-based tool that allows browsing and comparing symbol and type information of Microsoft Windows binaries across different versions of the OS.

WinDiff About WinDiff is an open-source web-based tool that allows browsing and comparing symbol and type information of Microsoft Windows binaries ac

A tool for transposing harmonica tabs into different positions and tunings
A tool for transposing harmonica tabs into different positions and tunings

A tool for transposing harmonica tabs into different positions and tunings

Terminal UI for leetcode. Lets you browse questions through different topics. View, solve, run and submit questions from TUI.
Terminal UI for leetcode. Lets you browse questions through different topics. View, solve, run and submit questions from TUI.

Leetcode TUI Use Leetcode in your terminal. Why this TUI: My motivation for creating leetcode-tui stemmed from my preference for tools that are lightw

Terminal UI to chat with large language models (LLM) using different model backends, and integrations with your favourite editors!
Terminal UI to chat with large language models (LLM) using different model backends, and integrations with your favourite editors!

Oatmeal Terminal UI to chat with large language models (LLM) using different model backends, and integrations with your favourite editors! Overview In

KAIVM is a multiplatform Command Line Interface (CLI) designed to simplify the process of downloading, managing, configuring, and running different versions of Shinkai Node
KAIVM is a multiplatform Command Line Interface (CLI) designed to simplify the process of downloading, managing, configuring, and running different versions of Shinkai Node

KAIVM - Shinkai Version Manager KAIVM is a multiplatform Command Line Interface (CLI) designed to simplify the process of downloading, managing, confi

Learning rust by coding different sorting algorithms in it
Learning rust by coding different sorting algorithms in it

Sorting in Rust Sorting a 32 bit unsigned integer vector by using: Radix Sort Quick Sort Merge Sort Bubble Sort Inbuilt Sort (Probably Tim Sort) This

Easy access of struct fields in strings using different/custom pre/postfix:
Easy access of struct fields in strings using different/custom pre/postfix: "Hello, {field}" in rust

Easy access to struct fields in strings 🐠 add strung to the dependencies in the Cargo.toml: [dependencies] strung = "0.1.3" 🦀 use/import everything

Irx-config - The library provides convenient way to represent/parse configuration from different sources

The irx-config library provides convenient way to represent/parse configuration from different sources. The main goals is to be very easy to use and t

Owner
David Delassus
Autodidact Python, Erlang and C developer since 2005, really interested in Artificial Intelligence and Data Science.
David Delassus
Zenith - sort of like top or htop but with zoom-able charts, CPU, GPU, network, and disk usage

Zenith - sort of like top or htop but with zoom-able charts, CPU, GPU, network, and disk usage

Benjamin Vaisvil 1.6k Jan 4, 2023
Like HashSet but retaining INSERTION order and without `Hash` requirement on the Element type.

identified_vec A collection of unique identifiable elements which retains insertion order, inspired by Pointfree's Swift Identified Collections. Simil

Alexander Cyon 4 Dec 11, 2023
du + rust = dust. Like du but more intuitive.

Dust du + rust = dust. Like du but more intuitive. Why Because I want an easy way to see where my disk is being used. Demo Install Cargo cargo install

andy.boot 5.4k Jan 4, 2023
🔭 Search Dash.app from Neovim with Telescope. Built with Rust 🦀 and Lua

Dash.nvim Query Dash.app within Neovim with a Telescope picker! The theme used in the recording is lighthaus.nvim. Note: Dash is a Mac-only app, so yo

Mat Jones 193 Dec 28, 2022
Like grep, but uses tree-sitter grammars to search

tree-grepper Works like grep, but uses tree-sitter to search for structure instead of strings. Installing This isn't available packaged anywhere. That

Brian Hicks 219 Dec 25, 2022
A command-line shell like fish, but POSIX compatible.

A command-line shell like fish, but POSIX compatible.

Seiya Nuta 813 Dec 29, 2022
Like a cell, but make lifetimes dynamic instead of ownership

LendingCell is a mutable container that allows you to get an owned reference to the same object. When the owned reference is dropped, ownership return

Kalle Samuels 19 Dec 15, 2022
Like wee_alloc, but smaller since I used skinnier letters in the name.

lol_alloc A laughably simple wasm global_allocator. Like wee_alloc, but smaller since I used skinnier letters in the name. lol_alloc is a experimental

Craig 44 Dec 28, 2022
`0x` - like `xxd` but colorful!

0x - like xxd but colorful! Note: for technical reasons, this library is listed as ohx on crates.io. 0x is a simple utility similar to xxd with a few

Miguel Young 57 Jun 12, 2023
It's like monkeytype but from your terminal

DoggyType It's like monkeytype but from ur terminal (ik very unoriginal lmao) Preview doggytype-preview.mp4 Requirements: Rust Build: git clone https:

Nirlep 6 Aug 29, 2023