11个不稳定版本 (4个破坏性更新)
使用旧的Rust 2015
0.5.0 | 2020年2月2日 |
---|---|
0.4.0 | 2020年1月18日 |
0.3.2 | 2019年6月16日 |
0.2.1 | 2019年2月26日 |
0.1.2 | 2019年1月12日 |
#359 in 编程语言
被用于 izia
125KB
3K SLoC
zia
Zia编程语言的解释器
Zia项目旨在开发一种可以用来编写自身的编程语言。与将源代码存储为纯文本并编辑原始文本(这很容易破坏程序)不同,解释器的运行环境(即 Context
)可以保存到磁盘并在其他程序中使用。所有编程都是通过交互式shell(如 IZia
或通过 在线IDE)完成的。发送的命令根据 Context
进行解释。它们用于逐步修改、测试和调试 Context
。
Zia命令的表达式表示一个二叉树,其中括号分组一对表达式,而空格分隔一对表达式。例如 "(ll lr) (rl rr)"
表示一个高度为2的完美二叉树,其叶子为从左到右的 "ll"
、"lr"
、"rl"
、"rr"
。
树的叶子可以是任何没有空格或括号的Unicode字符串。这些符号可以被解释器识别为概念,或者如果不使用,则可以用来标记新的概念。
目前,只有最低级别的功能已实现。为了实现自我描述的系统,程序必须在 Context
中一致且透明地表示。下面显示的语法可能看起来有些不自然,但一旦添加更多功能,就会有可能出现更方便的语法。例如,通过设置概念的相对优先级和结合性,可以减少在括号中分组表达式对的需求。
迄今为止共有10个内置概念。一个新的 Context
用以下符号标记这些概念:"label_of"
,"->"
,":="
,"let"
,"true"
,"false"
,"assoc"
,"right"
,"left"
,"prec","deafult",">",但这些标签可以根据不同的语言或学科进行更改。
示例
extern crate zia;
use zia::{Context, ZiaError};
// Construct a new `Context` using the `new` method
let mut context = Context::new();
// Specify operator precedence for `let` and `->`.
assert_eq!(context.execute("let default > prec ->"), "");
assert_eq!(context.execute("let (prec ->) > prec let"), "");
// Cannot yet infer partial order. Requires implication to express transitive property
assert_eq!(context.execute("let default > prec let"), "");
// Specify the rule that the concept "a b" reduces to concept "c"
assert_eq!(context.execute("let a b -> c"), "");
assert_eq!(context.execute("a b"), "c");
// Change the rule so that concept "a b" instead reduces to concept "d"
assert_eq!(context.execute("let a b -> d"), "");
assert_eq!(context.execute("a b"), "d");
// Change the rule so "a b" doesn't reduce any further
assert_eq!(context.execute("let a b -> a b"), "");
assert_eq!(context.execute("a b"), "a b");
// Try to specify a rule that already exists
assert_eq!(context.execute("let a b -> a b"), ZiaError::RedundantReduction.to_string());
assert_eq!(context.execute("let a b -> c"), "");
assert_eq!(context.execute("let a b -> c"), ZiaError::RedundantReduction.to_string());
// Relabel "label_of" to "표시"
assert_eq!(context.execute("let 표시 := label_of"), "");
assert_eq!(context.execute("표시 a b"), "\'c\'");
// You can reduce a labelled concept
assert_eq!(context.execute("let a -> d"), "");
// Try to specify the composition of a concept in terms of itself
assert_eq!(context.execute("let b := a b"), ZiaError::InfiniteDefinition.to_string());
// Try to specify the reduction of concept in terms of itself
assert_eq!(context.execute("let c d -> (c d) e"), ZiaError::ExpandingReduction.to_string());
// Determine the truth of a reduction
assert_eq!(context.execute("a -> d"), "true");
assert_eq!(context.execute("d -> a"), "false");
// A concept never reduces to itself
assert_eq!(context.execute("a -> a"), "false");
// Cannot reduce a reduction expression between unrelated concepts
assert_eq!(context.execute("d -> f"), "d -> f");
// Can ask whether a reduction is true or false
assert_eq!(context.execute("(a -> d) -> true"), "true");
assert_eq!(context.execute("(d -> a) -> false"), "true");
// Let an arbitary symbol be true
assert_eq!(context.execute("let g"), "");
assert_eq!(context.execute("g"), "true");
// Let an arbitary expression be true
assert_eq!(context.execute("let h i j"), "");
assert_eq!(context.execute("h i j"), "true");
// Determine associativity of symbol
assert_eq!(context.execute("assoc a"), "right");
// Define patterns
assert_eq!(context.execute("let _x_ and false -> false"), "");
assert_eq!(context.execute("foo and false"), "false");
许可证:GPL-3.0
依赖关系
~2–10MB
~94K SLoC