Item Id store

This commit is contained in:
greg 2019-09-18 09:56:11 -07:00
parent b517bc2366
commit 8dc8833eb3
3 changed files with 32 additions and 8 deletions

View File

@ -15,11 +15,33 @@ pub struct ItemId {
} }
impl ItemId { impl ItemId {
pub fn new(n: u32) -> ItemId { fn new(n: u32) -> ItemId {
ItemId { idx: n } ItemId { idx: n }
} }
} }
pub struct ItemIdStore {
last_idx: u32
}
impl ItemIdStore {
pub fn new() -> ItemIdStore {
ItemIdStore { last_idx: 0 }
}
/// Always returns an ItemId with internal value zero
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 {
let idx = self.last_idx;
self.last_idx += 1;
ItemId::new(idx)
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Meta<T> { pub struct Meta<T> {
pub n: T, pub n: T,

View File

@ -183,6 +183,7 @@ pub struct Parser {
parse_record: Vec<ParseRecord>, parse_record: Vec<ParseRecord>,
parse_level: u32, parse_level: u32,
restrictions: ParserRestrictions, restrictions: ParserRestrictions,
id_store: ItemIdStore,
} }
struct ParserRestrictions { struct ParserRestrictions {
@ -231,7 +232,8 @@ impl Parser {
token_handler: TokenHandler::new(initial_input), token_handler: TokenHandler::new(initial_input),
parse_record: vec![], parse_record: vec![],
parse_level: 0, parse_level: 0,
restrictions: ParserRestrictions { no_struct_literal: false } restrictions: ParserRestrictions { no_struct_literal: false },
id_store: ItemIdStore::new(),
} }
} }
@ -334,7 +336,7 @@ impl Parser {
), ),
} }
} }
Ok(AST { id: ItemId::new(0), statements }) Ok(AST { id: self.id_store.fresh(), statements })
} }
/// `statement := expression | declaration` /// `statement := expression | declaration`

View File

@ -4,7 +4,7 @@ use std::str::FromStr;
use super::tokenize; use super::tokenize;
use super::ParseResult; use super::ParseResult;
use crate::ast::{ItemId, AST, Meta, Expression, Statement, StatementKind, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument, FormalParam, PrefixOp, BinOp, QualifiedName}; use crate::ast::{ItemIdStore, AST, Meta, Expression, Statement, StatementKind, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument, FormalParam, PrefixOp, BinOp, QualifiedName};
use super::Declaration::*; use super::Declaration::*;
use super::Signature; use super::Signature;
use super::TypeIdentifier::*; use super::TypeIdentifier::*;
@ -20,12 +20,12 @@ fn parse(input: &str) -> ParseResult<AST> {
} }
macro_rules! parse_test { macro_rules! parse_test {
($string:expr, $correct:expr) => { ($string:expr, $correct:expr) => {
assert_eq!(parse($string).unwrap(), $correct) assert_eq!(parse($string).unwrap(), $correct)
}; };
} }
macro_rules! parse_test_wrap_ast { macro_rules! parse_test_wrap_ast {
($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemId::new(4), statements: vec![$correct] }) } ($string:expr, $correct:expr) => { parse_test!($string, AST { id: ItemIdStore::new_id(), 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()) }
@ -99,7 +99,7 @@ fn parsing_number_literals_and_binexps() {
parse_test! {"3; 4; 4.3", parse_test! {"3; 4; 4.3",
AST { AST {
id: ItemId::new(0), id: ItemIdStore::new_id(),
statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)), statements: vec![exst!(NatLiteral(3)), exst!(NatLiteral(4)),
exst!(FloatLiteral(4.3))] exst!(FloatLiteral(4.3))]
} }
@ -550,7 +550,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: ItemId::new(0), id: ItemIdStore::new_id(),
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! {