Use default for ItemId

This commit is contained in:
Greg Shuflin 2021-10-18 17:39:20 -07:00
parent 5bba900a3d
commit c697c929a4
2 changed files with 18 additions and 23 deletions

View File

@ -11,7 +11,8 @@ pub use visitor::ASTVisitor;
pub use walker::walk_ast; pub use walker::walk_ast;
use crate::tokenizing::Location; use crate::tokenizing::Location;
/// An abstract identifier for an AST node /// An abstract identifier for an AST node. Note that
/// the u32 index limits the size of an AST to 2^32 nodes.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Default)] #[derive(Debug, PartialEq, Eq, Hash, Clone, Default)]
pub struct ItemId { pub struct ItemId {
idx: u32, idx: u32,
@ -31,13 +32,7 @@ impl ItemIdStore {
pub fn new() -> ItemIdStore { pub fn new() -> ItemIdStore {
ItemIdStore { last_idx: 0 } ItemIdStore { last_idx: 0 }
} }
/// Always returns an ItemId with internal value zero
#[cfg(test)]
pub fn new_id() -> ItemId {
ItemId { idx: 0 }
}
/// This limits the size of the AST to 2^32 tree elements
pub fn fresh(&mut self) -> ItemId { pub fn fresh(&mut self) -> ItemId {
let idx = self.last_idx; let idx = self.last_idx;
self.last_idx += 1; self.last_idx += 1;

View File

@ -45,7 +45,7 @@ macro_rules! parse_test {
}; };
} }
macro_rules! parse_test_wrap_ast { macro_rules! parse_test_wrap_ast {
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemIdStore::new_id(), statements: vec![$correct] }) } ($string:expr, $correct:expr) => { parse_test!($string, AST { id: Default::default(), statements: vec![$correct] }) }
} }
macro_rules! parse_error { macro_rules! parse_error {
($string:expr) => { assert!(parse($string).is_err()) } ($string:expr) => { assert!(parse($string).is_err()) }
@ -57,12 +57,12 @@ macro_rules! qname {
$( $(
components.push(rc!($component)); components.push(rc!($component));
)* )*
QualifiedName { components, id: ItemIdStore::new_id() } QualifiedName { components, id: Default::default() }
} }
}; };
} }
macro_rules! val { macro_rules! val {
($var:expr) => { Value(QualifiedName { components: vec![Rc::new($var.to_string())], id: ItemIdStore::new_id() }) }; ($var:expr) => { Value(QualifiedName { components: vec![Rc::new($var.to_string())], id: Default::default() }) };
} }
macro_rules! ty { macro_rules! ty {
($name:expr) => { Singleton(tys!($name)) } ($name:expr) => { Singleton(tys!($name)) }
@ -90,8 +90,8 @@ macro_rules! module {
} }
macro_rules! ex { macro_rules! ex {
($expr_type:expr) => { Expression::new(ItemIdStore::new_id(), $expr_type) }; ($expr_type:expr) => { Expression::new(Default::default(), $expr_type) };
($expr_type:expr, $type_anno:expr) => { Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno) }; ($expr_type:expr, $type_anno:expr) => { Expression::with_anno(Default::default(), $expr_type, $type_anno) };
(s $expr_text:expr) => { (s $expr_text:expr) => {
{ {
let mut parser = make_parser($expr_text); let mut parser = make_parser($expr_text);
@ -105,14 +105,14 @@ macro_rules! inv {
} }
macro_rules! binexp { macro_rules! binexp {
($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression::new(ItemIdStore::new_id(), $lhs).into()), bx!(Expression::new(ItemIdStore::new_id(), $rhs).into())) } ($op:expr, $lhs:expr, $rhs:expr) => { BinExp(BinOp::from_sigil($op), bx!(Expression::new(Default::default(), $lhs).into()), bx!(Expression::new(Default::default(), $rhs).into())) }
} }
macro_rules! prefexp { macro_rules! prefexp {
($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression::new(ItemIdStore::new_id(), $lhs).into())) } ($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression::new(Default::default(), $lhs).into())) }
} }
macro_rules! exst { macro_rules! exst {
($expr_type:expr) => { make_statement(StatementKind::Expression(Expression::new(ItemIdStore::new_id(), $expr_type).into())) }; ($expr_type:expr) => { make_statement(StatementKind::Expression(Expression::new(Default::default(), $expr_type).into())) };
($expr_type:expr, $type_anno:expr) => { make_statement(StatementKind::Expression(Expression::with_anno(ItemIdStore::new_id(), $expr_type, $type_anno).into())) }; ($expr_type:expr, $type_anno:expr) => { make_statement(StatementKind::Expression(Expression::with_anno(Default::default(), $expr_type, $type_anno).into())) };
($op:expr, $lhs:expr, $rhs:expr) => { make_statement(StatementKind::Expression(ex!(binexp!($op, $lhs, $rhs)))) }; ($op:expr, $lhs:expr, $rhs:expr) => { make_statement(StatementKind::Expression(ex!(binexp!($op, $lhs, $rhs)))) };
(s $statement_text:expr) => { (s $statement_text:expr) => {
{ {
@ -137,7 +137,7 @@ fn parsing_number_literals_and_binexps() {
parse_test! {"3; 4; 4.3", parse_test! {"3; 4; 4.3",
AST { AST {
id: ItemIdStore::new_id(), id: Default::default(),
statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)), statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
exst!(FloatLiteral(4.3))] exst!(FloatLiteral(4.3))]
} }
@ -217,7 +217,7 @@ fn qualified_identifiers() {
parse_test_wrap_ast! { parse_test_wrap_ast! {
"let q_q = Yolo::Swaggins", "let q_q = Yolo::Swaggins",
decl!(Binding { name: rc!(q_q), constant: true, type_anno: None, decl!(Binding { name: rc!(q_q), constant: true, type_anno: None,
expr: Expression::new(ItemIdStore::new_id(), Value(qname!(Yolo, Swaggins))), expr: Expression::new(Default::default(), Value(qname!(Yolo, Swaggins))),
}) })
} }
@ -583,7 +583,7 @@ fn more_advanced_lambdas() {
r#"fn wahoo() { let a = 10; \(x) { x + a } }; r#"fn wahoo() { let a = 10; \(x) { x + a } };
wahoo()(3) "#, wahoo()(3) "#,
AST { AST {
id: ItemIdStore::new_id(), id: Default::default(),
statements: vec![ statements: vec![
exst!(s r"fn wahoo() { let a = 10; \(x) { x + a } }"), exst!(s r"fn wahoo() { let a = 10; \(x) { x + a } }"),
exst! { exst! {
@ -746,7 +746,7 @@ fn imports() {
parse_test_wrap_ast! { parse_test_wrap_ast! {
"import harbinger::draughts::Norgleheim", "import harbinger::draughts::Norgleheim",
import!(ImportSpecifier { import!(ImportSpecifier {
id: ItemIdStore::new_id(), id: Default::default(),
path_components: vec![rc!(harbinger), rc!(draughts), rc!(Norgleheim)], path_components: vec![rc!(harbinger), rc!(draughts), rc!(Norgleheim)],
imported_names: ImportedNames::LastOfPath imported_names: ImportedNames::LastOfPath
}) })
@ -758,7 +758,7 @@ fn imports_2() {
parse_test_wrap_ast! { parse_test_wrap_ast! {
"import harbinger::draughts::{Norgleheim, Xraksenlaigar}", "import harbinger::draughts::{Norgleheim, Xraksenlaigar}",
import!(ImportSpecifier { import!(ImportSpecifier {
id: ItemIdStore::new_id(), id: Default::default(),
path_components: vec![rc!(harbinger), rc!(draughts)], path_components: vec![rc!(harbinger), rc!(draughts)],
imported_names: ImportedNames::List(vec![ imported_names: ImportedNames::List(vec![
rc!(Norgleheim), rc!(Norgleheim),
@ -773,7 +773,7 @@ fn imports_3() {
parse_test_wrap_ast! { parse_test_wrap_ast! {
"import bespouri::{}", "import bespouri::{}",
import!(ImportSpecifier { import!(ImportSpecifier {
id: ItemIdStore::new_id(), id: Default::default(),
path_components: vec![rc!(bespouri)], path_components: vec![rc!(bespouri)],
imported_names: ImportedNames::List(vec![]) imported_names: ImportedNames::List(vec![])
}) })
@ -786,7 +786,7 @@ fn imports_4() {
parse_test_wrap_ast! { parse_test_wrap_ast! {
"import bespouri::*", "import bespouri::*",
import!(ImportSpecifier { import!(ImportSpecifier {
id: ItemIdStore::new_id(), id: Default::default(),
path_components: vec![rc!(bespouri)], path_components: vec![rc!(bespouri)],
imported_names: ImportedNames::All imported_names: ImportedNames::All
}) })