small fixes/visiter pattern/pub visibility

This commit is contained in:
Janis 2024-12-27 03:49:25 +01:00
parent 1b5cc3302b
commit e002a6ddc9
3 changed files with 73 additions and 12 deletions

View file

@ -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: "<this string is empty>" },
EMPTY_STRING => Key::String { str: "" },
EMPTY_BYTES => Key::Bytes { bytes: &[] },
);

View file

@ -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<F: FnMut(&mut AstT, &[Index], Index, Tag, Data)>(
&mut self,
mut cb: F,

View file

@ -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<Data>) -> u32 {
pub fn push(&mut self, inst: Inst, data: Option<Data>) -> u32 {
let node = self.nodes.len() as u32;
self.nodes.push(inst);
self.data.push(data);