Skip to content

Commit

Permalink
添加一系列内置函数
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 4, 2024
1 parent 9f0a036 commit 007aa86
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.14.5"
version = "0.14.6"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
> [`dexp_binder.mdtlbl`](./dexp_binder.mdtlbl)<br/>
> [`caller.mdtlbl`](./caller.mdtlbl)<br/>
> [`match.mdtlbl`](./match.mdtlbl)<br/>
> [`builtin_functions.mdtlbl`](./builtin_functions.mdtlbl)<br/>
如果没有列出那请在看完上述后自行观看, 顺序可以参考文件创建顺序.

Expand Down
46 changes: 46 additions & 0 deletions examples/builtin_functions.mdtlbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#**
* 这是0.14.6添加的新功能, 在初始化时会增加一系列绑定,
* 其中绑定到的值类型为内置的函数.
*
* 以下为这些函数的作用和参数清单
*
* * `Type[var]`: 获取右侧量的类型
* * `Stringify[var]`: 将传入的Var全部转换成字符串格式的Var
* * `Concat[a b]`: 将传入的两个字符串连接
* * `Info[var]`: 以info级日志形式将传入Var输出
* * `Err[var]`: 以err级日志形式将传入Var输出
* * `Unbind[var]`: 传入一个值绑定, 返回绑定者
* * `Const[name value]`: 动态目标的进行一个const, 并泄露到上层
* * `Binder[name value]`: 传入一个值绑定, 将其被绑定值const给给定名称
* * `Debug[value]`: 以debug形式将传入值输出到日志
* * `Exit[code]`: 直接使编译器以给定的退出码值退出
* * `Status[]`: 获取上一个内置函数的退出代码, 通常情况下, 零代表正常, 非零即异常
*#

print Builtin.Type[x];
print Builtin.Type[2];
print Builtin.Type[()];
print Builtin.Type[x.y];
print Builtin.Type[..];
print Builtin.Type[$];
#* >>>
print var
print var
print dexp
print valuebind
print binder
print resulthandle
*#


take Builtin.Info["This is a info"];
take Builtin.Err["This is a err"];


const Name = `X`;
const Value = (m:);
take Builtin.Const[Name Value];
print X;
#* >>>
print m
*#
2 changes: 1 addition & 1 deletion tools/display_source/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "display_source"
version = "0.3.7"
version = "0.3.8"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
8 changes: 7 additions & 1 deletion tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ impl DisplaySource for Value {
meta.push("(");
cmp.display_source(meta);
meta.push(")");
}
},
Self::BuiltinFunc(builtin_func) => {
meta.push("(#*");
meta.push("BuiltinFunc: ");
meta.push(builtin_func.name());
meta.push("*#)");
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser-tests"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
154 changes: 154 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4630,3 +4630,157 @@ fn dexp_expand_binder_test() {
],
);
}

#[test]
fn builtin_func_test() {
let parser = TopLevelParser::new();

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print Builtin.Type[x];
"#).unwrap()).compile().unwrap(),
vec![
"print var",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print Builtin.Stringify[2];
print Builtin.Stringify[x];
print Builtin.Stringify["x"];
"#).unwrap()).compile().unwrap(),
vec![
r#"print "2""#,
r#"print "x""#,
r#"print "x""#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print Builtin.Concat["abc" "def"];
print Builtin.Status;
print Builtin.Concat["abc" def];
print Builtin.Status;
"#).unwrap()).compile().unwrap(),
vec![
r#"print "abcdef""#,
r#"print 0"#,
r#"print __"#,
r#"print 2"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print Builtin.Type[()];
print Builtin.Type[`m`];
"#).unwrap()).compile().unwrap(),
vec![
"print dexp",
"print var",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const A = ();
const B = `emmm`;
const C = A.B;
const D = $;
const E = ..;
print Builtin.Type[A];
print Builtin.Type[B];
print Builtin.Type[C];
print Builtin.Type[D];
print Builtin.Type[E];
"#).unwrap()).compile().unwrap(),
vec![
"print dexp",
"print var",
"print valuebind",
"print resulthandle",
"print binder",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const A.B = 2;
print Builtin.Type[A.B];
"#).unwrap()).compile().unwrap(),
vec![
"print valuebind",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const A = x;
print Builtin.Info[A];
print Builtin.Info[y];
print Builtin.Err[A];
print Builtin.Err[y];
"#).unwrap()).compile().unwrap(),
vec![
"print x",
"print y",
"print x",
"print y",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const A = x.y;
print Builtin.Unbind[A];
print Builtin.Unbind[x.y];
"#).unwrap()).compile().unwrap(),
vec![
"print y",
"print y",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
{
const Name = `X`;
const Value = (h:);
take Builtin.Const[Name Value];
print X;
}
{
{ # 击穿
const Name = `Y`;
const Value = (i:);
match Name Value { @ {} }
take Builtin.Const;
}
print Y;
}
"#).unwrap()).compile().unwrap(),
vec![
"print h",
"print i",
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Value = (x:);
const Binded = Value.x;
print Builtin.Type[Binded];
take Builtin.Binder[Res Binded];
print Builtin.Type[Res];
print Res;
"#).unwrap()).compile().unwrap(),
vec![
"print valuebind",
"print dexp",
"print x",
],
);
}
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syntax"
version = "0.2.2"
version = "0.2.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Loading

0 comments on commit 007aa86

Please sign in to comment.