From 518fbe74a1722c7d4a66130d246265004b23bc91 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 29 Jan 2025 03:31:24 -0600 Subject: [PATCH] cl-ast: Fix `AddrOf` misbehavior --- compiler/cl-ast/src/ast.rs | 1 - compiler/cl-ast/src/ast_impl.rs | 7 ++----- compiler/cl-ast/src/ast_visitor/fold.rs | 3 +-- compiler/cl-ast/src/ast_visitor/visit.rs | 2 +- compiler/cl-interpret/src/interpret.rs | 6 +----- compiler/cl-parser/src/parser.rs | 26 ++++++++++++++++-------- compiler/cl-repl/examples/yaml.rs | 7 ++----- 7 files changed, 24 insertions(+), 28 deletions(-) diff --git a/compiler/cl-ast/src/ast.rs b/compiler/cl-ast/src/ast.rs index d07c9cd..36d97bf 100644 --- a/compiler/cl-ast/src/ast.rs +++ b/compiler/cl-ast/src/ast.rs @@ -546,7 +546,6 @@ pub struct ArrayRep { /// An address-of expression: `&` `mut`? [`Expr`] #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct AddrOf { - pub count: usize, pub mutable: Mutability, pub expr: Box, } diff --git a/compiler/cl-ast/src/ast_impl.rs b/compiler/cl-ast/src/ast_impl.rs index 24f7aa6..1220f13 100644 --- a/compiler/cl-ast/src/ast_impl.rs +++ b/compiler/cl-ast/src/ast_impl.rs @@ -616,11 +616,8 @@ mod display { impl Display for AddrOf { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let Self { count, mutable, expr } = self; - for _ in 0..*count { - f.write_char('&')?; - } - write!(f, "{mutable}{expr}") + let Self { mutable, expr } = self; + write!(f, "&{mutable}{expr}") } } diff --git a/compiler/cl-ast/src/ast_visitor/fold.rs b/compiler/cl-ast/src/ast_visitor/fold.rs index 1252a4f..082c461 100644 --- a/compiler/cl-ast/src/ast_visitor/fold.rs +++ b/compiler/cl-ast/src/ast_visitor/fold.rs @@ -322,9 +322,8 @@ pub trait Fold { } } fn fold_addrof(&mut self, a: AddrOf) -> AddrOf { - let AddrOf { count, mutable, expr } = a; + let AddrOf { mutable, expr } = a; AddrOf { - count, mutable: self.fold_mutability(mutable), expr: Box::new(self.fold_expr_kind(*expr)), } diff --git a/compiler/cl-ast/src/ast_visitor/visit.rs b/compiler/cl-ast/src/ast_visitor/visit.rs index df4b74d..aa99d58 100644 --- a/compiler/cl-ast/src/ast_visitor/visit.rs +++ b/compiler/cl-ast/src/ast_visitor/visit.rs @@ -279,7 +279,7 @@ pub trait Visit<'a>: Sized { self.visit_expr_kind(repeat); } fn visit_addrof(&mut self, a: &'a AddrOf) { - let AddrOf { count: _, mutable, expr } = a; + let AddrOf { mutable, expr } = a; self.visit_mutability(mutable); self.visit_expr_kind(expr); } diff --git a/compiler/cl-interpret/src/interpret.rs b/compiler/cl-interpret/src/interpret.rs index 66a18e7..1163de9 100644 --- a/compiler/cl-interpret/src/interpret.rs +++ b/compiler/cl-interpret/src/interpret.rs @@ -681,13 +681,9 @@ impl Interpret for ArrayRep { } impl Interpret for AddrOf { fn interpret(&self, env: &mut Environment) -> IResult { - let Self { count: _, mutable: _, expr } = self; + let Self { mutable: _, expr } = self; match expr.as_ref() { ExprKind::Index(_) => todo!("AddrOf array index"), - // ExprKind::Path(Path { absolute: false, parts }) => match parts.as_slice() { - // [PathPart::Ident(id)] => env.get_ref(id), - // _ => todo!("Path traversal in addrof"), - // }, ExprKind::Path(_) => todo!("Path traversal in addrof"), _ => Ok(ConValue::Ref(Rc::new(expr.interpret(env)?))), } diff --git a/compiler/cl-parser/src/parser.rs b/compiler/cl-parser/src/parser.rs index 939f472..dc29c5d 100644 --- a/compiler/cl-parser/src/parser.rs +++ b/compiler/cl-parser/src/parser.rs @@ -962,16 +962,24 @@ impl Parse<'_> for AddrOf { /// [AddrOf] = (`&`|`&&`)* [Expr] fn parse(p: &mut Parser) -> PResult { const P: Parsing = Parsing::AddrOf; - let mut count = 0; - loop { - count += match p.peek_kind(P)? { - TokenKind::Amp => 1, - TokenKind::AmpAmp => 2, - _ => break, - }; - p.consume_peeked(); + match p.peek_kind(P)? { + TokenKind::Amp => { + p.consume_peeked(); + Ok(AddrOf { mutable: Mutability::parse(p)?, expr: ExprKind::parse(p)?.into() }) + } + TokenKind::AmpAmp => { + p.consume_peeked(); + Ok(AddrOf { + mutable: Mutability::Not, + expr: ExprKind::AddrOf(AddrOf { + mutable: Mutability::parse(p)?, + expr: ExprKind::parse(p)?.into(), + }) + .into(), + }) + } + got => Err(p.error(ExpectedToken { want: TokenKind::Amp, got }, P)), } - Ok(AddrOf { count, mutable: Mutability::parse(p)?, expr: ExprKind::parse(p)?.into() }) } } diff --git a/compiler/cl-repl/examples/yaml.rs b/compiler/cl-repl/examples/yaml.rs index b297d80..80de6a6 100644 --- a/compiler/cl-repl/examples/yaml.rs +++ b/compiler/cl-repl/examples/yaml.rs @@ -525,11 +525,8 @@ pub mod yamlify { } impl Yamlify for AddrOf { fn yaml(&self, y: &mut Yamler) { - let Self { count, mutable, expr } = self; - y.key("AddrOf") - .pair("count", count) - .yaml(mutable) - .pair("expr", expr); + let Self { mutable, expr } = self; + y.key("AddrOf").yaml(mutable).pair("expr", expr); } } impl Yamlify for Group {