22个版本 (破坏性更新)

0.18.0 2019年12月29日
0.17.0 2019年11月20日
0.16.0 2019年10月19日
0.14.0 2019年5月26日
0.7.0 2018年6月28日

#354 in 编程语言

Download history 14/week @ 2024-03-29 1/week @ 2024-04-05 2/week @ 2024-05-24 1/week @ 2024-05-31

每月63次 下载
用于 smpli

MIT 许可证

670KB
18K SLoC

Build Status

SMPL

S(ome) M(ore) P(rogramming) L(anguage),发音为 'simple'。

它是做什么的?

SMPL 是一种简单的静态类型编程语言,旨在轻松嵌入 Rust 并编写独立程序。

动机

SMPL 是为了替代 Popstcl,作为我的选择你自己的冒险引擎 CYOA 的脚本语言而构建的。

优点

  • Rust-like 语法。
  • 静态类型
  • 词法作用域
  • 带有 Rust 代码生成器的编译器
  • 可嵌入(异步)解释器
  • 函数管道
  • 基于宽度的结构子类型
  • 泛型(基于宽度的结构约束)

缺点

  • 与 Rust 1:1 匹配
    • 没有移动语义
    • 没有生命周期概念
  • 基础标准库

使用代码

该项目分为两部分

  • smpl
    • 定义语言的内核 crate
    • 包括
      • 解析器
      • 静态分析器
      • 字节码数据结构
      • 字节码生成器
      • 元数据收集器
  • smpli
    • SMPL 字节码的解释器
      • 从解析和分析的 SMPL 模块创建 AVM
      • 为 SMPL 函数启动指令执行器
      • 提供映射 Rust -> SMPL(内建)函数的接口
    • 运行时数据结构

示例

有关更多嵌入示例,请参阅 examples/tic-tac-toe

SMPL 代码

// In a file or as a String
mod test;

// From interpreter's stdlib 
use log;

struct Point3d {
    x: int,
    y: int,
    z: int,
}

fn modify2d(type P)(point: P, x: int, y: int) -> P 
    where P: { x: int, y: int } {

    point.x = x;
    point.y = y;

    return point;
}

fn getX(point: { x: int }) -> int {
    return point.x;
}

fn add(l: int, r: int) -> int {
    return l + r;
}

fn main() {
    let p = init Point {
        x: 0,
        y: 0,
        z: 0,
    };

    let p = modify2d(type Point)(p, 1, 2);

    // Should print '4'
    log::println(add(getX(p), 1) |> add(2));
}

Rust 代码

// Running the above SMPL code using the 'smpli' crate
let scripts = vec![
    // If you have a path, can use:
    //   parse_module(UnparsedModule::file(path, &str_buff))
    VmModule::new(
      parse_module(UnparsedModule::anonymous(module_string))
        .unwrap()
    )
];

let std = StdBuilder::default().log(true).build().unwrap();
let vm = AVM::new(std, scripts)?;

let fn_handle = vm.query_module("rt", "run").unwrap().unwrap(); 
let executor = match vm
    .spawn_executor(fn_handle, None, SpawnOptions {
        type_check: false
    }) {

    Ok(executor) => executor,

    Err(e) => {
        println!("{:?}", e);
        process::exit(1);
    }

};

let _result = match executor.execute_sync() {

    Ok(val) => val,

    Err(e) => {
        println!("{:?}", e);
    }
};

运行 SMPL 代码。

Rust 后端暂时不支持。

SMPL 意在嵌入其他 Rust 程序中。解释器是保证支持所有现有和未来语言特征的 SMPL 代码执行的唯一方法。

许可证

MIT 许可证 下发布(见 LICENSE-MIT)。

依赖关系

~8MB
~139K SLoC