diff --git a/fine/build.rs b/fine/build.rs index d6b2c298..eb7876d8 100644 --- a/fine/build.rs +++ b/fine/build.rs @@ -5,17 +5,15 @@ use std::path::{Path, PathBuf}; fn generate_test_for_file(path: PathBuf) -> String { let contents = fs::read_to_string(&path).expect("Unable to read input"); - let display_path = path.display().to_string(); let mut concrete_stuff: Option = None; // Start iterating over lines and processing directives.... - let mut type_assertions = Vec::new(); let mut lines = contents.lines(); while let Some(line) = lines.next() { let line = match line.strip_prefix("//") { Some(line) => line, - None => continue, + None => break, }; let line = line.trim(); @@ -31,23 +29,11 @@ fn generate_test_for_file(path: PathBuf) -> String { concrete.push_str("\n"); } concrete_stuff = Some(concrete); - } else if let Some(line) = line.strip_prefix("type:") { - let (pos, expected) = line - .trim() - .split_once(' ') - .expect("Mal-formed type expectation"); - let pos: usize = pos - .trim() - .parse() - .expect(&format!("Unable to parse position '{pos}'")); - let expected = expected.trim(); - type_assertions.push(quote! { - crate::assert_type_at(&_tree, &_lines, #pos, #expected, #display_path); - }); } } let concrete_comparison = if let Some(concrete) = concrete_stuff { + let display_path = path.display().to_string(); quote! { crate::assert_concrete(&_tree, #concrete, #display_path) } @@ -60,7 +46,6 @@ fn generate_test_for_file(path: PathBuf) -> String { fn #name() { let (_tree, _lines) = fine::parser::parse(#contents); #concrete_comparison; - #(#type_assertions)* } }; diff --git a/fine/src/lib.rs b/fine/src/lib.rs index d2832c15..4144a208 100644 --- a/fine/src/lib.rs +++ b/fine/src/lib.rs @@ -1,3 +1,2 @@ pub mod parser; -pub mod semantics; pub mod tokens; diff --git a/fine/src/parser.rs b/fine/src/parser.rs index d2a8fc61..e24cd788 100644 --- a/fine/src/parser.rs +++ b/fine/src/parser.rs @@ -1,7 +1,6 @@ // NOTE: much of this parser structure derived from // https://matklad.github.io/2023/05/21/resilient-ll-parsing-tutorial.html use crate::tokens::{Lines, Token, TokenKind, Tokens}; -use std::fmt::Write as _; use std::{cell::Cell, num::NonZeroU32}; pub mod old; // Until I decide to delete it. @@ -19,26 +18,10 @@ impl<'a> SyntaxTree<'a> { } } - pub fn root(&self) -> Option { - self.root - } - - pub fn add_tree(&mut self, mut t: Tree<'a>) -> TreeRef { + pub fn add_tree(&mut self, t: Tree<'a>) -> TreeRef { assert!(t.parent.is_none()); let tr = TreeRef::from_index(self.trees.len()); - t.start_pos = t - .children - .first() - .map(|c| c.start_position(&self)) - .unwrap_or(0); - - t.end_pos = t - .children - .last() - .map(|c| c.end_position(&self)) - .unwrap_or(t.start_pos); - // NOTE: Because of the difficulty of holding multiple mutable // references it's this is our best chance to patch up parent // pointers. @@ -47,51 +30,14 @@ impl<'a> SyntaxTree<'a> { self[*ct].parent = Some(tr); } } - self.trees.push(t); tr } - pub fn dump(&self, with_positions: bool) -> String { - let mut output = String::new(); - if let Some(r) = self.root { - self[r].dump(self, with_positions, &mut output); - } - output - } - - pub fn start_position(&self, t: TreeRef) -> usize { - self[t].start_pos - } - - pub fn end_position(&self, t: TreeRef) -> usize { - self[t].end_pos - } - - pub fn find_tree_at(&self, pos: usize) -> Option { - let mut current = self.root?; - let mut tree = &self[current]; - if pos < tree.start_pos || pos >= tree.end_pos { - return None; - } - - loop { - let mut found = false; - for child in &tree.children { - if let Child::Tree(next) = child { - let next_tree = &self[*next]; - if pos >= next_tree.start_pos && pos < next_tree.end_pos { - found = true; - current = *next; - tree = next_tree; - break; - } - } - } - - if !found { - return Some(current); - } + pub fn dump(&self) -> String { + match self.root { + Some(r) => self[r].dump(self), + None => String::new(), } } } @@ -110,7 +56,7 @@ impl<'a> std::ops::IndexMut for SyntaxTree<'a> { } } -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug)] pub enum TreeKind { Error, File, @@ -137,34 +83,10 @@ pub enum TreeKind { pub struct Tree<'a> { pub kind: TreeKind, pub parent: Option, - pub start_pos: usize, - pub end_pos: usize, pub children: Vec>, } -impl<'a> Tree<'a> { - pub fn nth_token(&self, index: usize) -> Option<&Token<'a>> { - self.children - .get(index) - .map(|c| match c { - Child::Token(t) => Some(t), - _ => None, - }) - .flatten() - } - - pub fn nth_tree(&self, index: usize) -> Option { - self.children - .get(index) - .map(|c| match c { - Child::Tree(t) => Some(*t), - _ => None, - }) - .flatten() - } -} - -#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +#[derive(Copy, Clone, Eq, PartialEq)] pub struct TreeRef(NonZeroU32); impl TreeRef { @@ -180,15 +102,13 @@ impl TreeRef { } impl<'a> Tree<'a> { - pub fn dump(&self, tree: &SyntaxTree<'a>, with_positions: bool, output: &mut String) { - let _ = write!(output, "{:?}", self.kind); - if with_positions { - let _ = write!(output, " [{}, {})", self.start_pos, self.end_pos); - } - let _ = write!(output, "\n"); + pub fn dump(&self, tree: &SyntaxTree<'a>) -> String { + let mut output = String::new(); + output.push_str(&format!("{:?}\n", self.kind)); for child in self.children.iter() { - child.dump_rec(2, tree, with_positions, output); + child.dump_rec(2, tree, &mut output); } + output } } @@ -198,52 +118,21 @@ pub enum Child<'a> { } impl<'a> Child<'a> { - fn dump_rec( - &self, - indent: usize, - tree: &SyntaxTree<'a>, - with_positions: bool, - output: &mut String, - ) { + fn dump_rec(&self, indent: usize, tree: &SyntaxTree<'a>, output: &mut String) { for _ in 0..indent { - let _ = write!(output, " "); + output.push(' '); } match self { - Child::Token(t) => { - let _ = write!(output, "{:?}:'{:?}'", t.kind, t.as_str()); - if with_positions { - let _ = write!(output, " [{}, {})", t.start, t.start + t.as_str().len()); - } - let _ = write!(output, "\n"); - } + Child::Token(t) => output.push_str(&format!("{:?}:'{:?}'\n", t.kind, t.as_str())), Child::Tree(t) => { let t = &tree[*t]; - let _ = write!(output, "{:?}", t.kind); - if with_positions { - let _ = write!(output, " [{}, {})", t.start_pos, t.end_pos); - } - let _ = write!(output, "\n"); - + output.push_str(&format!("{:?}\n", t.kind)); for child in t.children.iter() { - child.dump_rec(indent + 2, tree, with_positions, output); + child.dump_rec(indent + 2, tree, output); } } } } - - pub fn start_position(&self, syntax_tree: &SyntaxTree) -> usize { - match &self { - Child::Token(t) => t.start, - Child::Tree(t) => syntax_tree[*t].start_pos, - } - } - - pub fn end_position(&self, syntax_tree: &SyntaxTree) -> usize { - match &self { - Child::Token(t) => t.start + t.as_str().len(), - Child::Tree(t) => syntax_tree[*t].end_pos, - } - } } enum ParseEvent<'a> { @@ -418,8 +307,6 @@ impl<'a> CParser<'a> { ParseEvent::Start { kind } => stack.push(Tree { kind, parent: None, - start_pos: 0, - end_pos: 0, children: Vec::new(), }), @@ -773,10 +660,6 @@ mod tests { fn tree_ref_size() { // What's the point of doing all that work if the tree ref isn't nice // and "small"? - // - // TODO: This is a dumb optimization because tokens are - // huge so Child is huge no matter what we do. If we retain - // tokens out of line then we can re-visit this optimization. assert_eq!(4, std::mem::size_of::>()); } } diff --git a/fine/src/semantics.rs b/fine/src/semantics.rs deleted file mode 100644 index 3f0ced21..00000000 --- a/fine/src/semantics.rs +++ /dev/null @@ -1,510 +0,0 @@ -use crate::{ - parser::{Child, SyntaxTree, Tree, TreeKind, TreeRef}, - tokens::{Lines, TokenKind}, -}; -use std::{cell::RefCell, collections::HashMap, fmt}; - -// TODO: An error should have: -// -// - a start -// - an end -// - a focus -// - descriptive messages -// -// that will have to wait for now -#[derive(Clone, PartialEq, Eq)] -pub struct Error { - pub start: (usize, usize), - pub end: (usize, usize), - pub message: String, -} - -impl Error { - pub fn new(line: usize, column: usize, message: T) -> Self - where - T: ToString, - { - Error { - start: (line, column), - end: (line, column), - message: message.to_string(), - } - } - - pub fn new_spanned(start: (usize, usize), end: (usize, usize), message: T) -> Self - where - T: ToString, - { - Error { - start, - end, - message: message.to_string(), - } - } -} - -impl fmt::Debug for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{self}") - } -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}:{}: {}", self.start.0, self.start.1, self.message) - } -} - -#[derive(Copy, Clone)] -pub enum Type { - // Signals a type error. If you receive this then you know that an error - // has already been reported; if you produce this be sure to also note - // the error in the errors collection. - Error, - - // Signals that the expression has a control-flow side-effect and that no - // value will ever result from this expression. Usually this means - // everything's fine. - Unreachable, - - Nothing, - // TODO: Numeric literals should be implicitly convertable, unlike other - // types. Maybe just "numeric literal" type? - F64, - String, - Bool, -} - -impl Type { - pub fn is_error(&self) -> bool { - match self { - Type::Error => true, - _ => false, - } - } - - pub fn compatible_with(&self, other: &Type) -> bool { - // TODO: This is wrong; we because of numeric literals etc. - match (self, other) { - (Type::F64, Type::F64) => true, - (Type::String, Type::String) => true, - (Type::Bool, Type::Bool) => true, - (Type::Unreachable, Type::Unreachable) => true, - - // Avoid introducing more errors - (Type::Error, _) => true, - (_, Type::Error) => true, - - (_, _) => false, - } - } -} - -impl fmt::Debug for Type { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{self}") - } -} - -impl fmt::Display for Type { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use Type::*; - match self { - Error => write!(f, "<< INTERNAL ERROR >>"), - Unreachable => write!(f, "<< UNREACHABLE >>"), - Nothing => write!(f, "()"), - F64 => write!(f, "f64"), - String => write!(f, "string"), - Bool => write!(f, "bool"), - } - } -} - -pub struct Semantics<'a> { - // TODO: Do I really want my own copy here? Should we standardize on Arc - // or Rc or some other nice sharing mechanism? - syntax_tree: &'a SyntaxTree<'a>, - lines: &'a Lines, - errors: RefCell>, - types: RefCell>, -} - -impl<'a> Semantics<'a> { - pub fn new(tree: &'a SyntaxTree<'a>, lines: &'a Lines) -> Self { - let mut semantics = Semantics { - syntax_tree: tree, - lines, - errors: RefCell::new(vec![]), - types: RefCell::new(HashMap::new()), - }; - - // NOTE: We ensure all the known errors are reported before we move - // on to answering any other questions. We're going to work as - // hard as we can from a partial tree. - if let Some(tr) = semantics.syntax_tree.root() { - semantics.gather_errors(tr); - } - - semantics - } - - pub fn snapshot_errors(&self) -> Vec { - (*self.errors.borrow()).clone() - } - - fn report_error(&self, position: usize, error: T) - where - T: ToString, - { - let (line, col) = self.lines.position(position); - self.errors - .borrow_mut() - .push(Error::new(line, col, error.to_string())); - } - - fn report_error_span(&self, start: usize, end: usize, error: T) - where - T: ToString, - { - let start = self.lines.position(start); - let end = self.lines.position(end); - self.errors - .borrow_mut() - .push(Error::new_spanned(start, end, error.to_string())); - } - - fn report_error_tree(&self, tree: &Tree<'a>, error: T) - where - T: ToString, - { - self.report_error_span(tree.start_pos, tree.end_pos, error) - } - - fn report_error_tree_ref(&self, tree: TreeRef, error: T) - where - T: ToString, - { - let tree = &self.syntax_tree[tree]; - self.report_error_span(tree.start_pos, tree.end_pos, error) - } - - fn gather_errors(&mut self, tree: TreeRef) { - let mut stack = vec![tree]; - while let Some(tr) = stack.pop() { - let tree = &self.syntax_tree[tr]; - for child in &tree.children { - match child { - Child::Token(t) => { - if t.kind == TokenKind::Error { - self.report_error(t.start, t.as_str()); - } - } - Child::Tree(t) => stack.push(*t), - } - } - } - } - - pub fn type_of(&self, t: TreeRef, value_required: bool) -> Option { - if let Some(existing) = self.types.borrow().get(&t) { - return Some(existing.clone()); - } - - let tree = &self.syntax_tree[t]; - let result = match tree.kind { - TreeKind::Error => Some(Type::Error), - TreeKind::UnaryExpression => self.type_of_unary(tree, value_required), - TreeKind::BinaryExpression => self.type_of_binary(tree, value_required), - TreeKind::TypeExpression => self.type_of_type_expr(tree, value_required), - TreeKind::Block => self.type_of_block(tree, value_required), - TreeKind::LiteralExpression => self.type_of_literal(tree), - TreeKind::GroupingExpression => self.type_of_grouping(tree, value_required), - TreeKind::ConditionalExpression => self.type_of_conditional(tree, value_required), - TreeKind::CallExpression => self.type_of_call(tree), - TreeKind::Argument => self.type_of_argument(tree), - - TreeKind::LetStatement => Some(Type::Nothing), - TreeKind::ReturnStatement => Some(Type::Unreachable), - TreeKind::ExpressionStatement => { - self.type_of_expression_statement(tree, value_required) - } - TreeKind::Identifier => self.type_of_identifier(tree), - _ => return None, - }; - - // NOTE: These return `None` if they encounter some problem. - let result = result.unwrap_or(Type::Error); - - self.types.borrow_mut().insert(t, result.clone()); - Some(result) - } - - fn type_of_unary(&self, tree: &Tree, value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::UnaryExpression); - - let op = tree.nth_token(0)?; - let expr = tree.nth_tree(1)?; - - let argument_type = self - .type_of(expr, value_required) - .expect("Our argument should be an expression"); - - match (op.kind, argument_type) { - (TokenKind::Plus, Type::F64) => Some(Type::F64), - (TokenKind::Minus, Type::F64) => Some(Type::F64), - (TokenKind::Bang, Type::Bool) => Some(Type::Bool), - - // This is dumb and should be punished, probably. - (_, Type::Unreachable) => { - self.report_error( - op.start, - "cannot apply a unary operator to something that doesn't yield a value", - ); - Some(Type::Error) - } - - // Propagate existing errors without additional complaint. - (_, Type::Error) => Some(Type::Error), - - (_, arg_type) => { - self.report_error( - op.start, - format!( - "cannot apply unary operator '{}' to value of type {}", - op.as_str(), - arg_type - ), - ); - Some(Type::Error) - } - } - } - - fn type_of_binary(&self, tree: &Tree, value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::BinaryExpression); - let lhs = self - .type_of(tree.nth_tree(0)?, value_required) - .expect("must be an expression"); - let op = tree.nth_token(1)?; - let rhs = self - .type_of(tree.nth_tree(2)?, value_required) - .expect("must be an expression"); - - match (op.kind, lhs, rhs) { - ( - TokenKind::Plus | TokenKind::Minus | TokenKind::Star | TokenKind::Slash, - Type::F64, - Type::F64, - ) => Some(Type::F64), - - (TokenKind::Plus, Type::String, Type::String) => Some(Type::String), - - (TokenKind::And | TokenKind::Or, Type::Bool, Type::Bool) => Some(Type::Bool), - - // This is dumb and should be punished, probably. - (_, _, Type::Unreachable) => { - self.report_error( - op.start, - format!("cannot apply '{op}' to an argument that doesn't yield a value (on the right)"), - ); - Some(Type::Error) - } - (_, Type::Unreachable, _) => { - self.report_error( - op.start, - format!("cannot apply '{op}' to an argument that doesn't yield a value (on the left)"), - ); - Some(Type::Error) - } - - // Propagate existing errors without additional complaint. - (_, Type::Error, _) => Some(Type::Error), - (_, _, Type::Error) => Some(Type::Error), - - // Missed the whole table, it must be an error. - (_, left_type, right_type) => { - self.report_error( - op.start, - format!("cannot apply binary operator '{op}' to expressions of type '{left_type}' (on the left) and '{right_type}' (on the right)"), - ); - Some(Type::Error) - } - } - } - - fn type_of_type_expr(&self, tree: &Tree, _value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::TypeExpression); - Some(Type::Error) - } - - fn type_of_block(&self, tree: &Tree, value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::Block); - - if tree.children.len() < 2 { - return None; - } - - // if tree.children.len() == 2 { - // // Empty blocks generate Nothing. - // return Some(Type::Nothing); - // } - - // The type of the block is the type of the last expression. - // (But the last child is the closing brace probably?) - let last_is_brace = tree.nth_token(tree.children.len() - 1).is_some(); - let last_index = tree.children.len() - if last_is_brace { 2 } else { 1 }; - - let mut is_unreachable = false; - for i in 1..last_index { - is_unreachable = self - .type_of(tree.nth_tree(i)?, false) - .map(|t| matches!(t, Type::Unreachable)) - .unwrap_or(false) - || is_unreachable; - } - - // NOTE: If for some reason the last statement is unsuitable for a - // type then we consider the type of the block to be Nothing. - // (And explicitly not Error, which is what returning None - // would yield.) - let last_type = self - .type_of(tree.nth_tree(last_index)?, value_required) - .unwrap_or(Type::Nothing); - - // If anything in this block generated an "Unreachable" then the - // whole type of the block is "unreachable" no matter what. - Some(if is_unreachable { - Type::Unreachable - } else { - last_type - }) - } - - fn type_of_literal(&self, tree: &Tree) -> Option { - assert_eq!(tree.kind, TreeKind::LiteralExpression); - - let tok = tree.nth_token(0)?; - let pig = match tok.kind { - TokenKind::Number => Type::F64, - TokenKind::String => Type::String, - TokenKind::True | TokenKind::False => Type::Bool, - _ => panic!("the token {tok} doesn't have a type!"), - }; - Some(pig) - } - - fn type_of_grouping(&self, tree: &Tree, value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::GroupingExpression); - - let expr = tree.nth_tree(1)?; - Some( - self.type_of(expr, value_required) - .expect("the thing in the parenthesis must have some type"), - ) - } - - fn type_of_conditional(&self, tree: &Tree, value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::ConditionalExpression); - - let cond_tree = tree.nth_tree(1)?; - let cond_type = self.type_of(cond_tree, true).expect("must be expression"); - let then_type = self - .type_of(tree.nth_tree(2)?, value_required) - .expect("must be expression"); - - let has_else = tree - .nth_token(3) - .map(|t| t.kind == TokenKind::Else) - .unwrap_or(false); - let else_type = if has_else { - Some( - self.type_of(tree.nth_tree(4)?, value_required) - .expect("must be expression"), - ) - } else { - None - }; - - if !cond_type.compatible_with(&Type::Bool) { - if !cond_type.is_error() { - self.report_error_tree_ref(cond_tree, "conditions must yield a boolean"); - } - Some(Type::Error) - } else { - match (then_type, else_type) { - (Type::Error, _) => Some(Type::Error), - (_, Some(Type::Error)) => Some(Type::Error), - (_, None) if value_required => { - self.report_error_tree( - tree, - "this conditional expression needs an else arm to produce a value", - ); - Some(Type::Error) - } - (then_type, Some(else_type)) if value_required => { - if else_type.compatible_with(&Type::Unreachable) { - // Doesn't matter if the value is required; the else branch - // will never generate a value for us so let's ignore it. - Some(then_type) - } else if then_type.compatible_with(&Type::Unreachable) { - // Or the then branch is unreachable, same thing with else - // then. - Some(else_type) - } else if !then_type.compatible_with(&else_type) { - self.report_error_tree( - tree, - format!("the type of the `then` branch ({then_type}) must match the type of the `else` branch ({else_type})"), - ); - Some(Type::Error) - } else { - Some(then_type) - } - } - (_, _) => { - assert!(!value_required); - Some(Type::Unreachable) - } - } - } - } - - fn type_of_call(&self, tree: &Tree) -> Option { - assert_eq!(tree.kind, TreeKind::CallExpression); - Some(Type::Error) - } - - fn type_of_argument(&self, tree: &Tree) -> Option { - assert_eq!(tree.kind, TreeKind::Argument); - Some(Type::Error) - } - - fn type_of_expression_statement(&self, tree: &Tree, value_required: bool) -> Option { - assert_eq!(tree.kind, TreeKind::ExpressionStatement); - let last_is_semicolon = tree - .nth_token(tree.children.len() - 1) - .map(|t| t.kind == TokenKind::Semicolon) - .unwrap_or(false); - - let expression_type = self - .type_of(tree.nth_tree(0)?, value_required && !last_is_semicolon) - .expect("must be expression"); - Some(match expression_type { - Type::Error => Type::Error, - Type::Unreachable => Type::Unreachable, - _ => { - // A semicolon at the end of an expression statement discards - // the value, leaving us with nothing. - if last_is_semicolon { - Type::Nothing - } else { - expression_type - } - } - }) - } - - fn type_of_identifier(&self, tree: &Tree) -> Option { - assert_eq!(tree.kind, TreeKind::Identifier); - todo!() - } -} diff --git a/fine/src/tokens.rs b/fine/src/tokens.rs index c634d7c3..dedbf864 100644 --- a/fine/src/tokens.rs +++ b/fine/src/tokens.rs @@ -429,11 +429,7 @@ impl<'a> Tokens<'a> { }; match c { - ' ' | '\t' | '\r' => self.whitespace(pos), - '\n' => { - self.lines.add_line(pos); - self.whitespace(pos) - } + ' ' | '\t' | '\r' | '\n' => self.whitespace(pos), '{' => self.token(pos, TokenKind::LeftBrace), '}' => self.token(pos, TokenKind::RightBrace), '[' => self.token(pos, TokenKind::LeftBracket), diff --git a/fine/tests/example_tests.rs b/fine/tests/example_tests.rs index a80b057b..d098e5da 100644 --- a/fine/tests/example_tests.rs +++ b/fine/tests/example_tests.rs @@ -1,6 +1,4 @@ use fine::parser::SyntaxTree; -use fine::semantics::{Semantics, Type}; -use fine::tokens::Lines; use pretty_assertions::assert_eq; fn rebase_concrete(source_path: &str, dump: &str) { @@ -71,7 +69,7 @@ fn rebase_concrete(source_path: &str, dump: &str) { } fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) { - let dump = tree.dump(false); + let dump = tree.dump(); let rebase = std::env::var("FINE_TEST_REBASE") .unwrap_or(String::new()) .to_lowercase(); @@ -85,53 +83,4 @@ fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) { } } -fn assert_type_at( - tree: &SyntaxTree, - lines: &Lines, - pos: usize, - expected: &str, - _source_path: &str, -) { - let tree_ref = match tree.find_tree_at(pos) { - Some(t) => t, - None => { - println!("Unable to find the subtee at position {pos}! Parsed the tree as:"); - println!("\n{}", tree.dump(true)); - panic!("Cannot find tree at position {pos}"); - } - }; - - let semantics = Semantics::new(tree, lines); - let tree_type = semantics.type_of(tree_ref, true); - - let actual = format!("{}", tree_type.unwrap_or(Type::Error)); - if actual != expected { - println!( - "The type of the {:?} tree at position {pos} had the wrong type! Parsed the tree as:", - tree[tree_ref].kind - ); - println!("\n{}", tree.dump(true)); - - let errors = semantics.snapshot_errors(); - if errors.len() == 0 { - println!("There were no errors reported during type checking.\n"); - } else { - println!( - "{} error{} reported during type checking:", - errors.len(), - if errors.len() == 1 { "" } else { "s" } - ); - for error in &errors { - println!(" Error: {error}"); - } - println!(); - } - - assert_eq!( - expected, actual, - "The type of the tree at position {pos} was incorrect" - ); - } -} - include!(concat!(env!("OUT_DIR"), "/generated_tests.rs")); diff --git a/fine/tests/expression/arithmetic.fine b/fine/tests/expression/arithmetic.fine index f5ac71e3..fe61663a 100644 --- a/fine/tests/expression/arithmetic.fine +++ b/fine/tests/expression/arithmetic.fine @@ -20,5 +20,3 @@ // | Semicolon:'";"' // 1 * 2 + -3 * 4; - -// type: 532 f64 \ No newline at end of file diff --git a/fine/tests/expression/boolean.fine b/fine/tests/expression/boolean.fine index 01f570f1..340b9e65 100644 --- a/fine/tests/expression/boolean.fine +++ b/fine/tests/expression/boolean.fine @@ -20,5 +20,3 @@ // | Semicolon:'";"' // true and false or false and !true; - -// type: 549 bool diff --git a/fine/tests/expression/conditional.fine b/fine/tests/expression/conditional.fine index a8c03cd7..a3224eb1 100644 --- a/fine/tests/expression/conditional.fine +++ b/fine/tests/expression/conditional.fine @@ -24,21 +24,3 @@ // | RightBrace:'"}"' // if true { "discarded"; 23 } else { 45 } - -// Here come some type probes! -// (type of the condition) -// type: 667 bool -// -// (the discarded expression) -// type: 674 string -// -// (the "then" clause) -// type: 686 f64 -// type: 689 f64 -// -// (the "else" clause) -// type: 696 f64 -// type: 699 f64 -// -// (the overall expression) -// type: 664 f64 \ No newline at end of file diff --git a/fine/tests/expression/number.fine b/fine/tests/expression/number.fine index ae01ea54..b153d3f2 100644 --- a/fine/tests/expression/number.fine +++ b/fine/tests/expression/number.fine @@ -4,7 +4,5 @@ // | LiteralExpression // | Number:'"42"' // | Semicolon:'";"' - +// 42; - -// type: 129 f64 diff --git a/fine/tests/expression/strings.fine b/fine/tests/expression/strings.fine index 808ff8ff..eab2d5ea 100644 --- a/fine/tests/expression/strings.fine +++ b/fine/tests/expression/strings.fine @@ -10,5 +10,3 @@ // | Semicolon:'";"' // "Hello " + 'world!'; - -// type: 261 string \ No newline at end of file