Skip to content

Commit

Permalink
fix: 修复一元运算符合并顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Mar 30, 2024
1 parent f906e20 commit 0e8200a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
12 changes: 2 additions & 10 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@

//mod D:\code\rs\key-native\target\debug\key_native.dll> m;
mod samples\testmod.ks> m;
//mod samples\testmod.ks> m;

let a = "结城理"

let i = 0;
for n: a {
log(n);
2/0;
}

a = "233"
log(--Int::parse("-3") == -3.0);
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static VERSION:usize = 100000;
static DISTRIBUTION:&str = "Subkey";

fn main()-> ExitCode {
// unary bug
// 字符串捕获变量
// str的index
// let [] = x
Expand All @@ -47,7 +48,7 @@ fn main()-> ExitCode {
// 参数类型检查
// 同名省略struct属性
// 如果不加分号报错会错行,记得提示用户
// 科学计数法
// 科学计数法0x 0b
// wasm版本实现
// linux macos支持
// 脚本打包exe
Expand Down Expand Up @@ -81,6 +82,7 @@ fn main()-> ExitCode {
// 运行并返回
let scanned = scan::scan(&fs::read(&path).unwrap_or_else(|e|
panic!("无法读取'{}': {}", path, e)));
println!("{scanned:?}");
let exit = runtime::run(&scanned);
if let primitive::litr::Litr::Int(code) = exit.returned {
return ExitCode::from(code as u8);
Expand Down
18 changes: 9 additions & 9 deletions src/scan/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,6 @@ impl Scanner<'_> {
});
}

// 在新运算符加入之前, 合并一元运算符
if precedence < charts::PREC_UNARY && unary.len() > 0 {
let mut right = expr_stack.pop().unwrap();
while let Some(op) = unary.pop() {
right = Expr::Unary { right:Box::new(right), op }
}
expr_stack.push(right);
}

// 如果没匹配到运算符就说明匹配结束
if op.len() == 0 {
assert_eq!(expr_stack.len(), 1);
Expand Down Expand Up @@ -251,6 +242,15 @@ impl Scanner<'_> {
let mut una = self.operator_unary();
unary.append(&mut una);

// 优先级够的话,合并一元运算符
if precedence < charts::PREC_UNARY && unary.len() > 0 {
let mut right = expr_stack.pop().unwrap();
while let Some(op) = unary.pop() {
right = Expr::Unary { right:Box::new(right), op }
}
expr_stack.push(right);
}

// 在此之前判断有没有括号来提升优先级
let right = if self.cur() == b'(' {
self.expr_group()
Expand Down

0 comments on commit 0e8200a

Please sign in to comment.