Skip to content

Commit

Permalink
Fixed: OpExpr中的if不会注册标记导致重复展开出现问题
Browse files Browse the repository at this point in the history
Example
------------------------------
```mdtlbl
const Foo = (
    $ = if _0 ? _1 : _2;
);
take A = Foo[true 1 2] B = Foo[false 1 2];
print A B;
```
  • Loading branch information
A4-Tacks committed Dec 14, 2023
1 parent 6c34d3f commit e269f02
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion 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.13.4"
version = "0.13.5"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
17 changes: 11 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
extern crate lalrpop;

use std::fs::remove_dir_all;
use std::{fs::remove_dir_all, path::Path};

fn main() {
//lalrpop::process_root().unwrap(); // build to cargo tmp

// build to src
// 最多尝试构建三次
// 最多尝试构建
let mut res = Ok(());
for _ in 0..3 {
for _ in 0..5 {
res = lalrpop::Configuration::new()
.generate_in_source_tree()
.always_use_colors()
.process();
.set_in_dir(Path::new("src"))
.set_out_dir(Path::new("src"))
.emit_comments(false)
.process()
;
if res.is_ok() {
break;
}
Expand All @@ -36,5 +39,7 @@ fn remove_incremental() {
},
"/incremental"
].concat();
remove_dir_all(incremental_path).unwrap();
remove_dir_all(incremental_path).unwrap_or_else(|e| {
eprintln!("Warn: 删除增量目录失败 {e}")
});
}
34 changes: 21 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use mindustry_logic_bang_lang::{
Expand,
Meta
},
syntax::def::TopLevelParser,
syntax::{def::TopLevelParser, line_first_add},
tag_code::TagCodes,
};

Expand Down Expand Up @@ -208,18 +208,26 @@ fn display_ast(ast: &Expand) -> String {
}

fn build_tag_down(meta: &mut CompileMeta) {
let tag_codes = meta.tag_codes_mut();
tag_codes.build_tagdown()
.unwrap_or_else(|(_line, tag)| {
let (tag_str, _id) = meta.tags_map()
.iter()
.filter(|&(_k, v)| *v == tag)
.take(1)
.last()
.unwrap();
err!("重复的标记: {:?}", tag_str);
exit(4)
})
let result = meta.tag_codes_mut().build_tagdown();
result.unwrap_or_else(|(_line, tag)| {
let (tag_str, _id) = meta.tags_map()
.iter()
.filter(|&(_k, v)| *v == tag)
.take(1)
.last()
.unwrap();
let mut tags_map = meta.debug_tags_map();
let mut tag_codes = meta.tag_codes().iter().map(ToString::to_string).collect();
line_first_add(&mut tags_map, "\t");
line_first_add(&mut tag_codes, "\t");
err!(
"TagCode:\n{}\nTagsMap:\n{}\n重复的标记: {:?}",
tag_codes.join("\n"),
tags_map.join("\n"),
tag_str,
);
exit(4)
})
}

type ParseResult<'a> = Result<Expand, ParseError<usize, Token<'a>, Error>>;
Expand Down
6 changes: 3 additions & 3 deletions src/syntax/def.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ pub Value: Value = {
},
<value:Value> "." <attr:NoStringVar> => ValueBind(value.into(), attr).into(),
// consted-dexp
"const" ConstStart <DExp> => {
"const" ConstStart <dexp:DExp> <labels:ConstStop> => {
let tmp_name = meta.get_tmp_var();
let dexp_const = Const(tmp_name.clone(), <>.into(), meta.pop_label_scope());
let dexp_const = Const(tmp_name.clone(), dexp.into(), labels);
DExp::new("__".into(), vec![
dexp_const.into(),
LogicLine::SetResultHandle(tmp_name.into()),
Expand Down Expand Up @@ -573,7 +573,7 @@ OpExprAtom: OpExprInfo = {

// 开始一个const, 开启了必须负责清理
ConstStart: () = () => meta.add_label_scope();
ConstStop: Vec<Var> = () => meta.pop_label_scope();
ConstStop: Vec<Var> = () => Vec::from_iter(meta.pop_label_scope());

pub BuiltinCommand: LogicLine = {
"const" <mut values:(<Var> "=" ConstStart <Value> <ConstStop>)+> LEnd
Expand Down
29 changes: 15 additions & 14 deletions src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod def;
use std::{
ops::Deref,
num::ParseIntError,
collections::HashMap,
collections::{HashMap, HashSet},
iter::{
zip,
repeat_with,
Expand Down Expand Up @@ -475,7 +475,7 @@ pub struct Meta {
tmp_var_count: usize,
tag_number: usize,
/// 被跳转的label
defined_labels: Vec<Vec<Var>>,
defined_labels: Vec<HashSet<Var>>,
break_labels: Vec<Option<Var>>,
continue_labels: Vec<Option<Var>>,
}
Expand All @@ -484,7 +484,7 @@ impl Default for Meta {
Self {
tmp_var_count: 0,
tag_number: 0,
defined_labels: vec![Vec::new()],
defined_labels: vec![HashSet::new()],
break_labels: Vec::new(),
continue_labels: Vec::new(),
}
Expand All @@ -509,26 +509,26 @@ impl Meta {
pub fn get_tag(&mut self) -> String {
let tag = self.tag_number;
self.tag_number += 1;
format!("___{}", tag)
self.add_defined_label(format!("___{}", tag))
}

/// 添加一个被跳转的label到当前作用域
/// 使用克隆的形式
pub fn add_defined_label(&mut self, label: Var) -> Var {
// 至少有一个基层定义域
self.defined_labels.last_mut().unwrap().push(label.clone());
self.defined_labels.last_mut().unwrap().insert(label.clone());
label
}

/// 添加一个标签作用域,
/// 用于const定义起始
pub fn add_label_scope(&mut self) {
self.defined_labels.push(Vec::new())
self.defined_labels.push(HashSet::new())
}

/// 弹出一个标签作用域,
/// 用于const定义完成收集信息
pub fn pop_label_scope(&mut self) -> Vec<Var> {
pub fn pop_label_scope(&mut self) -> HashSet<Var> {
self.defined_labels.pop().unwrap()
}

Expand Down Expand Up @@ -2729,13 +2729,14 @@ impl CompileMeta {
= self.get_const_value(name).unwrap();
let mut labels_map = HashMap::with_capacity(labels.len());
for (tmp_tag, label) in zip(tmp_tags, labels.iter().cloned()) {
let maped_label = format!(
"{}_const_{}_{}",
tmp_tag,
&name,
&label
);
labels_map.insert(label, maped_label);
labels_map.entry(label).or_insert_with_key(|label| {
format!(
"{}_const_{}_{}",
tmp_tag,
&name,
&label
)
});
}
let res = value.clone();
self.const_expand_tag_name_map.push(labels_map);
Expand Down
2 changes: 1 addition & 1 deletion tools/tag_code/Cargo.lock

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

2 changes: 2 additions & 0 deletions tools/tag_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ impl TagCodes {
// :b
// :b
// ```
self.lines = lines;
return Err((i, tag));
}
map_stack.push(tag);
Expand All @@ -633,6 +634,7 @@ impl TagCodes {
if let &mut Some(tag) = tag {
if tag_alias_map.get(&tag).is_some() {
// 映射到的目标是重复的
self.lines = lines;
return Err((i, tag));
}
tag_alias_map.insert(tag, tag); // 将自己插入
Expand Down

0 comments on commit e269f02

Please sign in to comment.