From c0f40aa5124141cf12a26dc1f7a4563acfc08046 Mon Sep 17 00:00:00 2001 From: John Doty Date: Fri, 5 Jan 2024 14:59:48 -0800 Subject: [PATCH] [fine] Type checking bones --- fine/build.rs | 17 +- fine/src/lib.rs | 1 + fine/src/parser.rs | 70 +++- fine/src/semantics.rs | 517 ++++++++++++++++++++++++++++++ fine/tests/example_tests.rs | 10 + fine/tests/expression/number.fine | 1 + 6 files changed, 613 insertions(+), 3 deletions(-) create mode 100644 fine/src/semantics.rs diff --git a/fine/build.rs b/fine/build.rs index eb7876d8..0038fbb0 100644 --- a/fine/build.rs +++ b/fine/build.rs @@ -5,10 +5,12 @@ 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("//") { @@ -29,11 +31,23 @@ 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) } @@ -46,6 +60,7 @@ 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 4144a208..d2832c15 100644 --- a/fine/src/lib.rs +++ b/fine/src/lib.rs @@ -1,2 +1,3 @@ pub mod parser; +pub mod semantics; pub mod tokens; diff --git a/fine/src/parser.rs b/fine/src/parser.rs index e24cd788..56e13684 100644 --- a/fine/src/parser.rs +++ b/fine/src/parser.rs @@ -18,6 +18,10 @@ impl<'a> SyntaxTree<'a> { } } + pub fn root(&self) -> Option { + self.root + } + pub fn add_tree(&mut self, t: Tree<'a>) -> TreeRef { assert!(t.parent.is_none()); let tr = TreeRef::from_index(self.trees.len()); @@ -40,6 +44,14 @@ impl<'a> SyntaxTree<'a> { None => String::new(), } } + + pub fn start_position(&self, t: TreeRef) -> Option { + self[t].start_position(&self) + } + + pub fn end_position(&self, t: TreeRef) -> Option { + self[t].end_position(&self) + } } impl<'a> std::ops::Index for SyntaxTree<'a> { @@ -56,7 +68,7 @@ impl<'a> std::ops::IndexMut for SyntaxTree<'a> { } } -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum TreeKind { Error, File, @@ -86,7 +98,61 @@ pub struct Tree<'a> { pub children: Vec>, } -#[derive(Copy, Clone, Eq, PartialEq)] +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() + } + + pub fn start_position(&self, tree: &SyntaxTree<'a>) -> Option { + for child in &self.children { + let start = match child { + Child::Tree(tr) => tree.start_position(*tr), + Child::Token(tok) => Some(tok.start), + }; + + if let Some(start) = start { + return Some(start); + } + } + + // Fundamentally no tokens in this tree. This seems *broken*. + None + } + + pub fn end_position(&self, tree: &SyntaxTree<'a>) -> Option { + for child in self.children.iter().rev() { + let end = match child { + Child::Tree(tr) => tree.end_position(*tr), + Child::Token(tok) => Some(tok.start + tok.as_str().len()), + }; + + if let Some(start) = end { + return Some(start); + } + } + + // Fundamentally no tokens in this tree. This seems *broken*. + None + } +} + +#[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct TreeRef(NonZeroU32); impl TreeRef { diff --git a/fine/src/semantics.rs b/fine/src/semantics.rs new file mode 100644 index 00000000..93d074ee --- /dev/null +++ b/fine/src/semantics.rs @@ -0,0 +1,517 @@ +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.end.0, 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: SyntaxTree<'a>, + lines: Lines, + errors: RefCell>, + types: RefCell>, +} + +impl<'a> Semantics<'a> { + pub fn new(tree: SyntaxTree<'a>, lines: 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 syntax(&self) -> &SyntaxTree<'a> { + &self.syntax_tree + } + + pub fn 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, + { + let start = tree.start_position(&self.syntax_tree).unwrap(); + let end = tree.start_position(&self.syntax_tree).unwrap(); + self.report_error_span(start, end, error) + } + + fn report_error_tree_ref(&self, tree: TreeRef, error: T) + where + T: ToString, + { + let start = self.syntax_tree.start_position(tree).unwrap(); + let end = self.syntax_tree.end_position(tree).unwrap(); + self.report_error_span(start, end, 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::F64), + + // 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/tests/example_tests.rs b/fine/tests/example_tests.rs index d098e5da..36efaff4 100644 --- a/fine/tests/example_tests.rs +++ b/fine/tests/example_tests.rs @@ -1,4 +1,5 @@ use fine::parser::SyntaxTree; +use fine::tokens::Lines; use pretty_assertions::assert_eq; fn rebase_concrete(source_path: &str, dump: &str) { @@ -83,4 +84,13 @@ 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, +) { +} + include!(concat!(env!("OUT_DIR"), "/generated_tests.rs")); diff --git a/fine/tests/expression/number.fine b/fine/tests/expression/number.fine index b153d3f2..08274c06 100644 --- a/fine/tests/expression/number.fine +++ b/fine/tests/expression/number.fine @@ -5,4 +5,5 @@ // | Number:'"42"' // | Semicolon:'";"' // +// type: 138 Number 42;