Skip to content

Commit

Permalink
添加几个关于在常量追溯时进行属性和绑定者操作的语法
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Mar 26, 2024
1 parent 1ea5c71 commit 2bfa458
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 28 deletions.
15 changes: 8 additions & 7 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.15.2"
version = "0.15.3"
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 @@ -36,6 +36,7 @@
> [`caller.mdtlbl`](./caller.mdtlbl)<br/>
> [`match.mdtlbl`](./match.mdtlbl)<br/>
> [`builtin_functions.mdtlbl`](./builtin_functions.mdtlbl)<br/>
> [`value_bind_ref.mdtlbl`](./value_bind_ref.mdtlbl)<br/>
如果没有列出那请在看完上述后自行观看, 顺序可以参考文件创建顺序.

Expand Down
46 changes: 46 additions & 0 deletions examples/value_bind_ref.mdtlbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#**
* 这是0.15.3添加的语法, 可以在const追溯时, 拿到某个值的绑定值
* 并且不对绑定值进行take. 用于简化2至3行的内建调用
*
* 语法大致为`Value->Name`, 并且Name的位置还可以有两个特殊值
* 如果是`..`, 那么将返回其绑定者, 这只会对箭头左边的值进行常量追溯而不是take
* 如果是`$`, 那么将其take并返回其句柄, 这在一些const时想要take很好用
*
* 返回绑定者的值只会进行常量追溯而不是进行take
*#

const bind.Fun = (
print "make";
setres 3;
);
print 1;
const Fun = bind->Fun;
print 2 Fun Fun->..;
#* >>>
print 1
print 2
print "make"
print 3
print bind
*#


const bind.X = (x: print "makeX";);
const bind.Y = (y: print "makeY";);
print 1;
const X = bind->X;
print 2 X;
const Y = X->..->Y;
print 3 Y;
print X->.. Y->..;
#* >>>
print 1
print 2
print "makeX"
print x
print 3
print "makeY"
print y
print bind
print bind
*#
1 change: 1 addition & 0 deletions syntax/vim/mdtlbl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ syn match mdtlblResultHandle /\$/
syn match mdtlblDefineResultHandle /\%(([%?]\=\%(\s\|#\*.*\*#\|\%(#[^*].*\|#\)\=\n\)*\)\@<=\I\i*:/

syn match mdtlblQuickDExpTakeIdent /\I\i*\%(\%(\s\|#\*.*\*#\|\%(#[^*].*\|#\)\=\n\)*\[\)\@=/
syn match mdtlblQuickDExpTakeIdent /->/
syn match mdtlblIdentLabel /:\I\i*/

" Fold {{{1
Expand Down
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.10"
version = "0.3.11"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
18 changes: 18 additions & 0 deletions tools/display_source/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl DisplaySource for Value {
Self::Binder => meta.push(".."),
Self::DExp(dexp) => dexp.display_source(meta),
Self::ValueBind(value_attr) => value_attr.display_source(meta),
Self::ValueBindRef(bindref) => bindref.display_source(meta),
Self::Cmper(cmp) => {
meta.push("goto");
meta.push("(");
Expand Down Expand Up @@ -119,6 +120,23 @@ impl DisplaySource for DExp {
meta.push(")");
}
}
impl DisplaySource for ValueBindRef {
fn display_source(&self, meta: &mut DisplaySourceMeta) {
if let val @ Value::DExp(_) = self.value() {
meta.push("(%");
val.display_source(meta);
meta.push(")");
} else {
self.value().display_source(meta);
}
meta.push("->");
match self.bind_target() {
ValueBindRefTarget::NameBind(bind) => bind.display_source(meta),
ValueBindRefTarget::Binder => meta.push(".."),
ValueBindRefTarget::ResultHandle => meta.push("$"),
}
}
}
impl DisplaySource for ValueBind {
fn display_source(&self, meta: &mut DisplaySourceMeta) {
if let Value::DExp(_) = &*self.0 {
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser"
version = "0.3.5"
version = "0.3.6"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
12 changes: 12 additions & 0 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use ::syntax::{
Var,
DExp,
ValueBind,
ValueBindRef,
ValueBindRefTarget,
ClosuredValue,
ClosuredValueMethod,
op_expr_build_op,
Expand Down Expand Up @@ -151,6 +153,16 @@ NonConstRangeValue: Value = {
MFTuple<Value>,
OpExprDExp => <>.into(),
ClosuredValue => <>.into(),
<value:NonConstRangeValue> "->" <name:NoStringVar> => {
let target = ValueBindRefTarget::NameBind(name);
ValueBindRef::new(value.into(), target.into()).into()
},
<NonConstRangeValue> "->" ".." => {
ValueBindRef::new(<>.into(), ValueBindRefTarget::Binder).into()
},
<NonConstRangeValue> "->" "$" => {
ValueBindRef::new(<>.into(), ValueBindRefTarget::ResultHandle).into()
},
}

pub Value: Value = {
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.16"
version = "0.1.17"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
175 changes: 175 additions & 0 deletions tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5364,4 +5364,179 @@ fn closure_value_test() {
r#"jump 4 always 0 0"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
print ([X:1](
print "foo";
setres X;
));
"#).unwrap()).compile().unwrap(),
vec![
r#"print "foo""#,
r#"print 1"#,
],
);
}

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

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const bind.V = (
print "take";
setres "finish";
);
print 1;
const V = bind->V;
print 2;
const bind.V = (
print "fail";
);
print V;
"#).unwrap()).compile().unwrap(),
vec![
r#"print 1"#,
r#"print 2"#,
r#"print "take""#,
r#"print "finish""#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Bind = bind;
const Bind.V = (
print "take";
setres "finish";
);
print 1;
const V = Bind->V;
const V1 = bind->V;
print 2;
const Bind.V = (
print "fail";
);
print V V1;
"#).unwrap()).compile().unwrap(),
vec![
r#"print 1"#,
r#"print 2"#,
r#"print "take""#,
r#"print "finish""#,
r#"print "take""#,
r#"print "finish""#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const bind.V = (
print "take";
setres "finish";
);
print 1;
const V = bind->V;
print 2;
const bind.V = (
print "fail";
);
print V->..;
"#).unwrap()).compile().unwrap(),
vec![
r#"print 1"#,
r#"print 2"#,
r#"print bind"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const bind.V = (
print "take";
setres "finish";
);
print 1;
const B = bind->V->..;
print 2;
const bind.V = (
print "fail";
);
print B;
"#).unwrap()).compile().unwrap(),
vec![
r#"print 1"#,
r#"print 2"#,
r#"print bind"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const Res = (%
print "maked";
const $.M = 2;
%)->$;
print 1 Res.M;
"#).unwrap()).compile().unwrap(),
vec![
r#"print "maked""#,
r#"print 1"#,
r#"print 2"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const F = (print 2;);
print 1;
const Attr = F->X;
print 3 Attr;
"#).unwrap()).compile().unwrap(),
vec![
r#"print 1"#,
r#"print 2"#,
r#"print 3"#,
r#"print __1"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const bind.next.X = 2;
const F = bind->next->X;
print bind.next F->.. F->..->..;
"#).unwrap()).compile().unwrap(),
vec![
r#"print __0"#,
r#"print __0"#,
r#"print __"#,
],
);

assert_eq!(
CompileMeta::new().compile(parse!(parser, r#"
const bind.X = (x: print "makeX";);
const bind.Y = (y: print "makeY";);
print 1;
const X = bind->X;
print 2 X;
const Y = X->..->Y;
print 3 Y;
print X->.. Y->..;
"#).unwrap()).compile().unwrap(),
vec![
r#"print 1"#,
r#"print 2"#,
r#"print "makeX""#,
r#"print x"#,
r#"print 3"#,
r#"print "makeY""#,
r#"print y"#,
r#"print bind"#,
r#"print bind"#,
],
);
}
Loading

0 comments on commit 2bfa458

Please sign in to comment.