diff --git a/Cargo.lock b/Cargo.lock index cb158a19..140801f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "TSPL" -version = "0.0.12" +version = "0.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52dfd6238b1461b99635b26585a85b4e7b9c786cc0481b3c540ae5f590b6dfb6" +checksum = "fe639519d49b56c98fd4fde7a5a7be01b5563862341a783b9bc2eb58f5120d8b" dependencies = [ "highlight_error", ] @@ -163,7 +163,7 @@ checksum = "809e18805660d7b6b2e2b9f316a5099521b5998d5cba4dda11b5157a21aaef03" [[package]] name = "hvm" -version = "2.0.21" +version = "2.0.22" dependencies = [ "TSPL", "cc", diff --git a/Cargo.toml b/Cargo.toml index b08dea98..817e5da0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "hvm" description = "A massively parallel, optimal functional runtime in Rust." license = "Apache-2.0" -version = "2.0.21" +version = "2.0.22" edition = "2021" rust-version = "1.74" build = "build.rs" @@ -13,7 +13,7 @@ name = "hvm" path = "src/lib.rs" [dependencies] -TSPL = "0.0.12" +TSPL = "0.0.13" clap = "4.5.2" highlight_error = "0.1.1" num_cpus = "1.0" diff --git a/src/ast.rs b/src/ast.rs index 56db241c..c5b3a745 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,4 +1,4 @@ -use TSPL::{new_parser, Parser}; +use TSPL::{new_parser, Parser, ParseError}; use highlight_error::highlight_error; use crate::hvm; use std::fmt::{Debug, Display}; @@ -37,11 +37,13 @@ pub struct Book { // Parser // ------ +pub type ParseResult = std::result::Result; + new_parser!(CoreParser); impl<'i> CoreParser<'i> { - pub fn parse_numb_sym(&mut self) -> Result { + pub fn parse_numb_sym(&mut self) -> ParseResult { self.consume("[")?; // numeric casts @@ -97,33 +99,29 @@ impl<'i> CoreParser<'i> { return Ok(Numb(num.0)); } - pub fn parse_numb_lit(&mut self) -> Result { + pub fn parse_numb_lit(&mut self) -> ParseResult { let ini = self.index; let num = self.take_while(|x| x.is_alphanumeric() || x == '+' || x == '-' || x == '.'); + let mut num_parser = CoreParser::new(num); let end = self.index; Ok(Numb(if num.contains('.') || num.contains("inf") || num.contains("NaN") { - let val: f32 = num.parse().map_err(|err| format!("invalid number literal: {}\n{}", err, highlight_error(ini, end, self.input)))?; + let val: f32 = num.parse() + .map_err(|err| { + let msg = format!("invalid number literal: {}\n{}", err, highlight_error(ini, end, self.input)); + self.expected_and::("number literal", &msg).unwrap_err() + })?; hvm::Numb::new_f24(val) } else if num.starts_with('+') || num.starts_with('-') { - let val = Self::parse_int(&num[1..])? as i32; + *num_parser.index() += 1; + let val = num_parser.parse_u64()? as i32; hvm::Numb::new_i24(if num.starts_with('-') { -val } else { val }) } else { - let val = Self::parse_int(num)? as u32; + let val = num_parser.parse_u64()? as u32; hvm::Numb::new_u24(val) }.0)) } - fn parse_int(input: &str) -> Result { - if let Some(rest) = input.strip_prefix("0x") { - u64::from_str_radix(rest, 16).map_err(|err| format!("{err:?}")) - } else if let Some(rest) = input.strip_prefix("0b") { - u64::from_str_radix(rest, 2).map_err(|err| format!("{err:?}")) - } else { - input.parse::().map_err(|err| format!("{err:?}")) - } - } - - pub fn parse_numb(&mut self) -> Result { + pub fn parse_numb(&mut self) -> ParseResult { self.skip_trivia(); // Parses symbols (SYM) @@ -135,7 +133,7 @@ impl<'i> CoreParser<'i> { } } - pub fn parse_tree(&mut self) -> Result { + pub fn parse_tree(&mut self) -> ParseResult { self.skip_trivia(); //println!("aaa ||{}", &self.input[self.index..]); match self.peek_one() { @@ -194,7 +192,7 @@ impl<'i> CoreParser<'i> { } } - pub fn parse_net(&mut self) -> Result { + pub fn parse_net(&mut self) -> ParseResult { let root = self.parse_tree()?; let mut rbag = Vec::new(); self.skip_trivia(); @@ -210,7 +208,7 @@ impl<'i> CoreParser<'i> { Ok(Net { root, rbag }) } - pub fn parse_book(&mut self) -> Result { + pub fn parse_book(&mut self) -> ParseResult { let mut defs = BTreeMap::new(); while !self.is_eof() { self.consume("@")?; @@ -527,7 +525,7 @@ impl Net { } impl Book { - pub fn parse(code: &str) -> Result { + pub fn parse(code: &str) -> ParseResult { CoreParser::new(code).parse_book() } diff --git a/tests/snapshots/run__file@empty.hvm.snap b/tests/snapshots/run__file@empty.hvm.snap index 1460dd1e..54fc2b68 100644 --- a/tests/snapshots/run__file@empty.hvm.snap +++ b/tests/snapshots/run__file@empty.hvm.snap @@ -4,6 +4,6 @@ expression: rust_output input_file: tests/programs/empty.hvm --- exit status: 101 -thread 'main' panicked at src/ast.rs:547:41: +thread 'main' panicked at src/ast.rs:545:41: missing `@main` definition note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace