[fine] Get rid of "value required", it's not useful
This commit is contained in:
parent
b205ebcb4c
commit
758aef4db9
4 changed files with 43 additions and 66 deletions
|
|
@ -18,7 +18,7 @@ pub fn process_file(file: &str) {
|
||||||
|
|
||||||
// This is... probably wrong, I don't know, what am I doing?
|
// This is... probably wrong, I don't know, what am I doing?
|
||||||
for t in tree.trees() {
|
for t in tree.trees() {
|
||||||
let _ = semantics.type_of(t, false);
|
let _ = semantics.type_of(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
// OK now there might be errors.
|
// OK now there might be errors.
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ impl Type {
|
||||||
(Type::String, Type::String) => true,
|
(Type::String, Type::String) => true,
|
||||||
(Type::Bool, Type::Bool) => true,
|
(Type::Bool, Type::Bool) => true,
|
||||||
(Type::Unreachable, Type::Unreachable) => true,
|
(Type::Unreachable, Type::Unreachable) => true,
|
||||||
|
(Type::Nothing, Type::Nothing) => true,
|
||||||
|
|
||||||
// Avoid introducing more errors
|
// Avoid introducing more errors
|
||||||
(Type::Error, _) => true,
|
(Type::Error, _) => true,
|
||||||
|
|
@ -126,7 +127,7 @@ pub struct Semantics<'a> {
|
||||||
syntax_tree: &'a SyntaxTree<'a>,
|
syntax_tree: &'a SyntaxTree<'a>,
|
||||||
lines: &'a Lines,
|
lines: &'a Lines,
|
||||||
errors: RefCell<Vec<Error>>,
|
errors: RefCell<Vec<Error>>,
|
||||||
types: RefCell<HashMap<(TreeRef, bool), Type>>,
|
types: RefCell<HashMap<TreeRef, Type>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Semantics<'a> {
|
impl<'a> Semantics<'a> {
|
||||||
|
|
@ -209,29 +210,27 @@ impl<'a> Semantics<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn type_of(&self, t: TreeRef, value_required: bool) -> Option<Type> {
|
pub fn type_of(&self, t: TreeRef) -> Option<Type> {
|
||||||
if let Some(existing) = self.types.borrow().get(&(t, value_required)) {
|
if let Some(existing) = self.types.borrow().get(&t) {
|
||||||
return Some(existing.clone());
|
return Some(existing.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
let tree = &self.syntax_tree[t];
|
let tree = &self.syntax_tree[t];
|
||||||
let result = match tree.kind {
|
let result = match tree.kind {
|
||||||
TreeKind::Error => Some(Type::Error),
|
TreeKind::Error => Some(Type::Error),
|
||||||
TreeKind::UnaryExpression => self.type_of_unary(tree, value_required),
|
TreeKind::UnaryExpression => self.type_of_unary(tree),
|
||||||
TreeKind::BinaryExpression => self.type_of_binary(tree, value_required),
|
TreeKind::BinaryExpression => self.type_of_binary(tree),
|
||||||
TreeKind::TypeExpression => self.type_of_type_expr(tree, value_required),
|
TreeKind::TypeExpression => self.type_of_type_expr(tree),
|
||||||
TreeKind::Block => self.type_of_block(tree, value_required),
|
TreeKind::Block => self.type_of_block(tree),
|
||||||
TreeKind::LiteralExpression => self.type_of_literal(tree),
|
TreeKind::LiteralExpression => self.type_of_literal(tree),
|
||||||
TreeKind::GroupingExpression => self.type_of_grouping(tree, value_required),
|
TreeKind::GroupingExpression => self.type_of_grouping(tree),
|
||||||
TreeKind::ConditionalExpression => self.type_of_conditional(tree, value_required),
|
TreeKind::ConditionalExpression => self.type_of_conditional(tree),
|
||||||
TreeKind::CallExpression => self.type_of_call(tree),
|
TreeKind::CallExpression => self.type_of_call(tree),
|
||||||
TreeKind::Argument => self.type_of_argument(tree),
|
TreeKind::Argument => self.type_of_argument(tree),
|
||||||
|
|
||||||
TreeKind::LetStatement => Some(Type::Nothing),
|
TreeKind::LetStatement => Some(Type::Nothing),
|
||||||
TreeKind::ReturnStatement => Some(Type::Unreachable),
|
TreeKind::ReturnStatement => Some(Type::Unreachable),
|
||||||
TreeKind::ExpressionStatement => {
|
TreeKind::ExpressionStatement => self.type_of_expression_statement(tree),
|
||||||
self.type_of_expression_statement(tree, value_required)
|
|
||||||
}
|
|
||||||
TreeKind::Identifier => self.type_of_identifier(tree),
|
TreeKind::Identifier => self.type_of_identifier(tree),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
@ -239,20 +238,18 @@ impl<'a> Semantics<'a> {
|
||||||
// NOTE: These return `None` if they encounter some problem.
|
// NOTE: These return `None` if they encounter some problem.
|
||||||
let result = result.unwrap_or(Type::Error);
|
let result = result.unwrap_or(Type::Error);
|
||||||
|
|
||||||
self.types
|
self.types.borrow_mut().insert(t, result.clone());
|
||||||
.borrow_mut()
|
|
||||||
.insert((t, value_required), result.clone());
|
|
||||||
Some(result)
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_unary(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
fn type_of_unary(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::UnaryExpression);
|
assert_eq!(tree.kind, TreeKind::UnaryExpression);
|
||||||
|
|
||||||
let op = tree.nth_token(0)?;
|
let op = tree.nth_token(0)?;
|
||||||
let expr = tree.nth_tree(1)?;
|
let expr = tree.nth_tree(1)?;
|
||||||
|
|
||||||
let argument_type = self
|
let argument_type = self
|
||||||
.type_of(expr, value_required)
|
.type_of(expr)
|
||||||
.expect("Our argument should be an expression");
|
.expect("Our argument should be an expression");
|
||||||
|
|
||||||
match (op.kind, argument_type) {
|
match (op.kind, argument_type) {
|
||||||
|
|
@ -286,14 +283,14 @@ impl<'a> Semantics<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_binary(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
fn type_of_binary(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::BinaryExpression);
|
assert_eq!(tree.kind, TreeKind::BinaryExpression);
|
||||||
let lhs = self
|
let lhs = self
|
||||||
.type_of(tree.nth_tree(0)?, value_required)
|
.type_of(tree.nth_tree(0)?)
|
||||||
.expect("must be an expression");
|
.expect("must be an expression");
|
||||||
let op = tree.nth_token(1)?;
|
let op = tree.nth_token(1)?;
|
||||||
let rhs = self
|
let rhs = self
|
||||||
.type_of(tree.nth_tree(2)?, value_required)
|
.type_of(tree.nth_tree(2)?)
|
||||||
.expect("must be an expression");
|
.expect("must be an expression");
|
||||||
|
|
||||||
match (op.kind, lhs, rhs) {
|
match (op.kind, lhs, rhs) {
|
||||||
|
|
@ -338,12 +335,12 @@ impl<'a> Semantics<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_type_expr(&self, tree: &Tree, _value_required: bool) -> Option<Type> {
|
fn type_of_type_expr(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::TypeExpression);
|
assert_eq!(tree.kind, TreeKind::TypeExpression);
|
||||||
Some(Type::Error)
|
Some(Type::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_block(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
fn type_of_block(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::Block);
|
assert_eq!(tree.kind, TreeKind::Block);
|
||||||
|
|
||||||
if tree.children.len() < 2 {
|
if tree.children.len() < 2 {
|
||||||
|
|
@ -363,7 +360,7 @@ impl<'a> Semantics<'a> {
|
||||||
let mut is_unreachable = false;
|
let mut is_unreachable = false;
|
||||||
for i in 1..last_index {
|
for i in 1..last_index {
|
||||||
is_unreachable = self
|
is_unreachable = self
|
||||||
.type_of(tree.nth_tree(i)?, false)
|
.type_of(tree.nth_tree(i)?)
|
||||||
.map(|t| matches!(t, Type::Unreachable))
|
.map(|t| matches!(t, Type::Unreachable))
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
|| is_unreachable;
|
|| is_unreachable;
|
||||||
|
|
@ -374,7 +371,7 @@ impl<'a> Semantics<'a> {
|
||||||
// (And explicitly not Error, which is what returning None
|
// (And explicitly not Error, which is what returning None
|
||||||
// would yield.)
|
// would yield.)
|
||||||
let last_type = self
|
let last_type = self
|
||||||
.type_of(tree.nth_tree(last_index)?, value_required)
|
.type_of(tree.nth_tree(last_index)?)
|
||||||
.unwrap_or(Type::Nothing);
|
.unwrap_or(Type::Nothing);
|
||||||
|
|
||||||
// If anything in this block generated an "Unreachable" then the
|
// If anything in this block generated an "Unreachable" then the
|
||||||
|
|
@ -399,34 +396,29 @@ impl<'a> Semantics<'a> {
|
||||||
Some(pig)
|
Some(pig)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_grouping(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
fn type_of_grouping(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::GroupingExpression);
|
assert_eq!(tree.kind, TreeKind::GroupingExpression);
|
||||||
|
|
||||||
let expr = tree.nth_tree(1)?;
|
let expr = tree.nth_tree(1)?;
|
||||||
Some(
|
Some(
|
||||||
self.type_of(expr, value_required)
|
self.type_of(expr)
|
||||||
.expect("the thing in the parenthesis must have some type"),
|
.expect("the thing in the parenthesis must have some type"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_conditional(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
fn type_of_conditional(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::ConditionalExpression);
|
assert_eq!(tree.kind, TreeKind::ConditionalExpression);
|
||||||
|
|
||||||
let cond_tree = tree.nth_tree(1)?;
|
let cond_tree = tree.nth_tree(1)?;
|
||||||
let cond_type = self.type_of(cond_tree, true).expect("must be expression");
|
let cond_type = self.type_of(cond_tree).expect("must be expression");
|
||||||
let then_type = self
|
let then_type = self.type_of(tree.nth_tree(2)?).expect("must be expression");
|
||||||
.type_of(tree.nth_tree(2)?, value_required)
|
|
||||||
.expect("must be expression");
|
|
||||||
|
|
||||||
let has_else = tree
|
let has_else = tree
|
||||||
.nth_token(3)
|
.nth_token(3)
|
||||||
.map(|t| t.kind == TokenKind::Else)
|
.map(|t| t.kind == TokenKind::Else)
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
let else_type = if has_else {
|
let else_type = if has_else {
|
||||||
Some(
|
Some(self.type_of(tree.nth_tree(4)?).expect("must be expression"))
|
||||||
self.type_of(tree.nth_tree(4)?, value_required)
|
|
||||||
.expect("must be expression"),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
@ -440,23 +432,14 @@ impl<'a> Semantics<'a> {
|
||||||
match (then_type, else_type) {
|
match (then_type, else_type) {
|
||||||
(Type::Error, _) => Some(Type::Error),
|
(Type::Error, _) => Some(Type::Error),
|
||||||
(_, Some(Type::Error)) => Some(Type::Error),
|
(_, Some(Type::Error)) => Some(Type::Error),
|
||||||
(_, None) if value_required => {
|
|
||||||
self.report_error_tree(
|
(Type::Unreachable, None) => Some(Type::Unreachable),
|
||||||
tree,
|
(Type::Unreachable, Some(t)) => Some(t),
|
||||||
"this conditional expression needs an else arm to produce a value",
|
(t, Some(Type::Unreachable)) => Some(t),
|
||||||
);
|
|
||||||
Some(Type::Error)
|
(then_type, else_type) => {
|
||||||
}
|
let else_type = else_type.unwrap_or(Type::Nothing);
|
||||||
(then_type, Some(else_type)) if value_required => {
|
if !then_type.compatible_with(&else_type) {
|
||||||
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(
|
self.report_error_tree(
|
||||||
tree,
|
tree,
|
||||||
format!("the type of the `then` branch ({then_type}) must match the type of the `else` branch ({else_type})"),
|
format!("the type of the `then` branch ({then_type}) must match the type of the `else` branch ({else_type})"),
|
||||||
|
|
@ -466,10 +449,6 @@ impl<'a> Semantics<'a> {
|
||||||
Some(then_type)
|
Some(then_type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(_, _) => {
|
|
||||||
assert!(!value_required);
|
|
||||||
Some(Type::Unreachable)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -484,22 +463,20 @@ impl<'a> Semantics<'a> {
|
||||||
Some(Type::Error)
|
Some(Type::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_of_expression_statement(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
fn type_of_expression_statement(&self, tree: &Tree) -> Option<Type> {
|
||||||
assert_eq!(tree.kind, TreeKind::ExpressionStatement);
|
assert_eq!(tree.kind, TreeKind::ExpressionStatement);
|
||||||
let last_is_semicolon = tree
|
let last_is_semicolon = tree
|
||||||
.nth_token(tree.children.len() - 1)
|
.nth_token(tree.children.len() - 1)
|
||||||
.map(|t| t.kind == TokenKind::Semicolon)
|
.map(|t| t.kind == TokenKind::Semicolon)
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
let expression_type = self
|
let expression_type = self.type_of(tree.nth_tree(0)?).expect("must be expression");
|
||||||
.type_of(tree.nth_tree(0)?, value_required && !last_is_semicolon)
|
|
||||||
.expect("must be expression");
|
|
||||||
Some(match expression_type {
|
Some(match expression_type {
|
||||||
Type::Error => Type::Error,
|
|
||||||
Type::Unreachable => Type::Unreachable,
|
Type::Unreachable => Type::Unreachable,
|
||||||
_ => {
|
_ => {
|
||||||
// A semicolon at the end of an expression statement discards
|
// A semicolon at the end of an expression statement discards
|
||||||
// the value, leaving us with nothing.
|
// the value, leaving us with nothing. (Even if the
|
||||||
|
// expression otherwise generated a type error!)
|
||||||
if last_is_semicolon {
|
if last_is_semicolon {
|
||||||
Type::Nothing
|
Type::Nothing
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -150,7 +150,7 @@ fn assert_type_at(
|
||||||
None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"),
|
None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let tree_type = semantics.type_of(tree_ref, true);
|
let tree_type = semantics.type_of(tree_ref);
|
||||||
let actual = format!("{}", tree_type.unwrap_or(Type::Error));
|
let actual = format!("{}", tree_type.unwrap_or(Type::Error));
|
||||||
semantic_assert_eq!(
|
semantic_assert_eq!(
|
||||||
&semantics,
|
&semantics,
|
||||||
|
|
@ -173,7 +173,7 @@ fn assert_type_error_at(
|
||||||
None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"),
|
None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let tree_type = semantics.type_of(tree_ref, true);
|
let tree_type = semantics.type_of(tree_ref);
|
||||||
semantic_assert!(
|
semantic_assert!(
|
||||||
&semantics,
|
&semantics,
|
||||||
matches!(tree_type, Some(Type::Error)),
|
matches!(tree_type, Some(Type::Error)),
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
if (if false { true }) { 32 } else { 23 }
|
if (if false { true }) { 32 } else { 23 }
|
||||||
// type-error: 4 this conditional expression needs an else arm to produce a value
|
// type-error: 4 the type of the `then` branch (bool) must match the type of the `else` branch (())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue