Skip to content

Commit

Permalink
✨ orbit文のフォーマッタを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
liebe-magi committed Aug 21, 2024
1 parent e6e2824 commit e332b89
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ license = "MIT"
clap = { version = "4.0", features = ["derive"] }
colored = "2.1.0"
dirs = "5.0.1"
itertools = "0.13.0"
pest = "2.7.11"
pest_derive = "2.7.11"
rustyline = "14.0.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/oracle.aby
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ oracle (a = 3, b = 2) {
_ => unveil("a is not 1 and b is not 2");
};

// xが正の場合、xを表示する、xが負の場合、xを反転して表示する
// xが正の場合xを表示、x + 5が正の場合x + 5を表示、それ以外の場合-999を表示
forge x: arcana = -10;
forge y: arcana = oracle (x > 0) {
(boon) => reveal x;
Expand Down
6 changes: 4 additions & 2 deletions examples/orbit.aby
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ orbit(i = 0..3, j = 0..3) {
unveil("Continue:");
orbit(i = 0..3, j = 0..3) {
oracle(i==j) {
(boon) => resume j; // 内側のループをスキップ
// 内側のループをスキップ
(boon) => resume j;
};
unveil(i, " ", j);
};
Expand All @@ -31,7 +32,8 @@ orbit(i = 0..3) {
orbit(j = 0..3) {
unveil(i, " ", j);
oracle(i==2) {
(boon) => eject i; // 外側のループを抜ける
// 外側のループを抜ける
(boon) => eject i;
};
};
};
42 changes: 37 additions & 5 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ast::{AssignmentOp, Type, AST};

pub fn format_ast(ast: &AST, indent_level: usize) -> String {
let indent = " ".repeat(indent_level); // インデントを生成
let indent = " ".repeat(indent_level); // インデントを生成

// 優先順位テーブル
let precedence = |node: &AST| match node {
Expand Down Expand Up @@ -190,13 +190,13 @@ pub fn format_ast(ast: &AST, indent_level: usize) -> String {
})
.collect::<Vec<_>>()
.join(", ");
result.push_str(&format!("({})", conditions));
result.push_str(&format!(" ({})", conditions));
}
result.push_str(" {\n");
for branch in branches {
// コメントノードの場合はスキップ
if let AST::Comment(text, _) = branch {
result.push_str(&format!("{}{}\n", " ".repeat(indent_level + 1), text));
result.push_str(&format!("{}{}\n", " ".repeat(indent_level + 1), text));
continue;
}

Expand All @@ -208,7 +208,7 @@ pub fn format_ast(ast: &AST, indent_level: usize) -> String {
.join(", ");
result.push_str(&format!(
"{}{} => {}\n",
" ".repeat(indent_level + 1),
" ".repeat(indent_level + 1),
if pattern == "" {
"_".to_string()
} else {
Expand All @@ -222,7 +222,39 @@ pub fn format_ast(ast: &AST, indent_level: usize) -> String {
result
}
AST::OracleDontCareItem(_) => format!("_"),
AST::Orbit { params, body, .. } => {
let mut result = "orbit".to_string();
if !params.is_empty() {
let params_str = params
.iter()
.map(|param| format_ast(param, indent_level))
.collect::<Vec<_>>()
.join(", ");
result.push_str(&format!(" ({})", params_str));
}
result.push_str(&format_ast(body, indent_level).trim());
result
}
AST::OrbitParam {
name,
start,
end,
op,
..
} => {
let start_expr = format_ast(start, 0);
let end_expr = format_ast(end, 0);
format!("{} = {}{}{}", name, start_expr, op, end_expr)
}
AST::Resume(value, _) => match value {
Some(idendifier) => format!("resume {}", idendifier),
None => "resume".to_string(),
},
AST::Eject(value, _) => match value {
Some(idendifier) => format!("eject {}", idendifier),
None => "eject".to_string(),
},
AST::Comment(text, _) => text.clone(),
_ => "".to_string(),
_ => format!("Not implemented: {:?}", ast),
}
}

0 comments on commit e332b89

Please sign in to comment.