From e002a6ddc9ecaa3096d008e89a6db2bb9741b3cd Mon Sep 17 00:00:00 2001 From: Janis Date: Fri, 27 Dec 2024 03:49:25 +0100 Subject: [PATCH] small fixes/visiter pattern/pub visibility --- src/ast2/intern.rs | 3 +-- src/ast2/mod.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++- src/triples.rs | 18 ++++++------- 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/ast2/intern.rs b/src/ast2/intern.rs index 66edcbd..8b116e3 100644 --- a/src/ast2/intern.rs +++ b/src/ast2/intern.rs @@ -2,7 +2,6 @@ use std::{ collections::BTreeMap, fmt::Display, hash::{Hash, Hasher}, - marker::PhantomData, }; use num_bigint::{BigInt, BigUint, Sign}; @@ -490,7 +489,7 @@ static_keys!( U128 => Key::UIntType { bit_width: 128 }, TRUE => Key::TrueValue, FALSE => Key::FalseValue, - EMPTY_STRING => Key::String { str: "" }, + EMPTY_STRING => Key::String { str: "" }, EMPTY_BYTES => Key::Bytes { bytes: &[] }, ); diff --git a/src/ast2/mod.rs b/src/ast2/mod.rs index 848f50f..6780d83 100644 --- a/src/ast2/mod.rs +++ b/src/ast2/mod.rs @@ -1276,7 +1276,10 @@ impl Ast { { return_type } else { - eprintln!("lhs of call expr is not a function!"); + eprintln!( + "lhs of call expr is not a function: {fn_ty}: {:?}", + ip.get_key(fn_ty) + ); void } } @@ -2162,6 +2165,65 @@ pub mod visitor { where AstT: AstExt, { + pub fn visit< + F: FnMut(&mut AstT, &[Index], Index, Tag, Data), + G: FnMut(&mut AstT, &[Index], Index, Tag, Data), + >( + &mut self, + mut pre: F, + mut post: G, + ) { + while let Some(node) = self.nodes.pop() { + match node { + A::PushChildren(i) => { + self.nodes.push(A::PopSelf(i)); + let children_iter = self + .ast + .get_node_children(i) + .into_iter() + .map(|i| A::PushChildren(i)); + // inverse because we are popping from the end + if !self.rev { + self.nodes.extend(children_iter.rev()) + } else { + self.nodes.extend(children_iter) + }; + + let (tag, data) = self.ast.get_node_tag_and_data(i); + + let _ = pre(&mut self.ast, &self.scopes, i, tag, data); + + match tag { + Tag::File + | Tag::FunctionDecl + | Tag::GlobalDecl + | Tag::Block + | Tag::BlockTrailingExpr => { + self.scopes.push(i); + } + _ => {} + } + } + A::PopSelf(i) => { + // already popped. + let (tag, data) = self.ast.get_node_tag_and_data(i); + + let _ = post(&mut self.ast, &self.scopes, i, tag, data); + + match tag { + Tag::File + | Tag::FunctionDecl + | Tag::GlobalDecl + | Tag::Block + | Tag::BlockTrailingExpr => { + self.scopes.pop(); + } + _ => {} + } + } + } + } + } pub fn visit_pre( &mut self, mut cb: F, diff --git a/src/triples.rs b/src/triples.rs index 1956bcf..afb6cea 100644 --- a/src/triples.rs +++ b/src/triples.rs @@ -237,36 +237,36 @@ impl Inst { } #[derive(Debug, Clone, Copy)] -struct Data { +pub struct Data { lhs: u32, rhs: u32, } impl Data { - fn new(lhs: u32, rhs: u32) -> Self { + pub fn new(lhs: u32, rhs: u32) -> Self { Self { lhs, rhs } } - fn lhs(lhs: u32) -> Data { + pub fn lhs(lhs: u32) -> Data { Self { lhs, rhs: 0 } } - fn as_u32(&self) -> u32 { + pub fn as_u32(&self) -> u32 { self.lhs } - fn as_u64(&self) -> u64 { + pub fn as_u64(&self) -> u64 { self.lhs as u64 | (self.rhs as u64) << u32::BITS as u64 } - fn as_index(&self) -> intern::Index { + pub fn as_index(&self) -> intern::Index { intern::Index::from_u32(self.lhs) } - fn as_index_index(&self) -> (intern::Index, intern::Index) { + pub fn as_index_index(&self) -> (intern::Index, intern::Index) { ( intern::Index::from_u32(self.lhs), intern::Index::from_u32(self.rhs), ) } - fn as_lhs_rhs(&self) -> (u32, u32) { + pub fn as_lhs_rhs(&self) -> (u32, u32) { (self.lhs, self.rhs) } } @@ -838,7 +838,7 @@ impl IR { data: Vec::new(), } } - fn push(&mut self, inst: Inst, data: Option) -> u32 { + pub fn push(&mut self, inst: Inst, data: Option) -> u32 { let node = self.nodes.len() as u32; self.nodes.push(inst); self.data.push(data);