Compare commits
2 commits
5cc9ecc398
...
618e0028d3
| Author | SHA1 | Date | |
|---|---|---|---|
| 618e0028d3 | |||
| c0f40aa512 |
11 changed files with 747 additions and 23 deletions
|
|
@ -5,15 +5,17 @@ use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
fn generate_test_for_file(path: PathBuf) -> String {
|
fn generate_test_for_file(path: PathBuf) -> String {
|
||||||
let contents = fs::read_to_string(&path).expect("Unable to read input");
|
let contents = fs::read_to_string(&path).expect("Unable to read input");
|
||||||
|
let display_path = path.display().to_string();
|
||||||
|
|
||||||
let mut concrete_stuff: Option<String> = None;
|
let mut concrete_stuff: Option<String> = None;
|
||||||
|
|
||||||
// Start iterating over lines and processing directives....
|
// Start iterating over lines and processing directives....
|
||||||
|
let mut type_assertions = Vec::new();
|
||||||
let mut lines = contents.lines();
|
let mut lines = contents.lines();
|
||||||
while let Some(line) = lines.next() {
|
while let Some(line) = lines.next() {
|
||||||
let line = match line.strip_prefix("//") {
|
let line = match line.strip_prefix("//") {
|
||||||
Some(line) => line,
|
Some(line) => line,
|
||||||
None => break,
|
None => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
let line = line.trim();
|
let line = line.trim();
|
||||||
|
|
@ -29,11 +31,23 @@ fn generate_test_for_file(path: PathBuf) -> String {
|
||||||
concrete.push_str("\n");
|
concrete.push_str("\n");
|
||||||
}
|
}
|
||||||
concrete_stuff = Some(concrete);
|
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 concrete_comparison = if let Some(concrete) = concrete_stuff {
|
||||||
let display_path = path.display().to_string();
|
|
||||||
quote! {
|
quote! {
|
||||||
crate::assert_concrete(&_tree, #concrete, #display_path)
|
crate::assert_concrete(&_tree, #concrete, #display_path)
|
||||||
}
|
}
|
||||||
|
|
@ -46,6 +60,7 @@ fn generate_test_for_file(path: PathBuf) -> String {
|
||||||
fn #name() {
|
fn #name() {
|
||||||
let (_tree, _lines) = fine::parser::parse(#contents);
|
let (_tree, _lines) = fine::parser::parse(#contents);
|
||||||
#concrete_comparison;
|
#concrete_comparison;
|
||||||
|
#(#type_assertions)*
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
|
pub mod semantics;
|
||||||
pub mod tokens;
|
pub mod tokens;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// NOTE: much of this parser structure derived from
|
// NOTE: much of this parser structure derived from
|
||||||
// https://matklad.github.io/2023/05/21/resilient-ll-parsing-tutorial.html
|
// https://matklad.github.io/2023/05/21/resilient-ll-parsing-tutorial.html
|
||||||
use crate::tokens::{Lines, Token, TokenKind, Tokens};
|
use crate::tokens::{Lines, Token, TokenKind, Tokens};
|
||||||
|
use std::fmt::Write as _;
|
||||||
use std::{cell::Cell, num::NonZeroU32};
|
use std::{cell::Cell, num::NonZeroU32};
|
||||||
|
|
||||||
pub mod old; // Until I decide to delete it.
|
pub mod old; // Until I decide to delete it.
|
||||||
|
|
@ -18,10 +19,26 @@ impl<'a> SyntaxTree<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_tree(&mut self, t: Tree<'a>) -> TreeRef {
|
pub fn root(&self) -> Option<TreeRef> {
|
||||||
|
self.root
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_tree(&mut self, mut t: Tree<'a>) -> TreeRef {
|
||||||
assert!(t.parent.is_none());
|
assert!(t.parent.is_none());
|
||||||
let tr = TreeRef::from_index(self.trees.len());
|
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
|
// NOTE: Because of the difficulty of holding multiple mutable
|
||||||
// references it's this is our best chance to patch up parent
|
// references it's this is our best chance to patch up parent
|
||||||
// pointers.
|
// pointers.
|
||||||
|
|
@ -30,14 +47,51 @@ impl<'a> SyntaxTree<'a> {
|
||||||
self[*ct].parent = Some(tr);
|
self[*ct].parent = Some(tr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.trees.push(t);
|
self.trees.push(t);
|
||||||
tr
|
tr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dump(&self) -> String {
|
pub fn dump(&self, with_positions: bool) -> String {
|
||||||
match self.root {
|
let mut output = String::new();
|
||||||
Some(r) => self[r].dump(self),
|
if let Some(r) = self.root {
|
||||||
None => String::new(),
|
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<TreeRef> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -56,7 +110,7 @@ impl<'a> std::ops::IndexMut<TreeRef> for SyntaxTree<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum TreeKind {
|
pub enum TreeKind {
|
||||||
Error,
|
Error,
|
||||||
File,
|
File,
|
||||||
|
|
@ -83,10 +137,34 @@ pub enum TreeKind {
|
||||||
pub struct Tree<'a> {
|
pub struct Tree<'a> {
|
||||||
pub kind: TreeKind,
|
pub kind: TreeKind,
|
||||||
pub parent: Option<TreeRef>,
|
pub parent: Option<TreeRef>,
|
||||||
|
pub start_pos: usize,
|
||||||
|
pub end_pos: usize,
|
||||||
pub children: Vec<Child<'a>>,
|
pub children: Vec<Child<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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<TreeRef> {
|
||||||
|
self.children
|
||||||
|
.get(index)
|
||||||
|
.map(|c| match c {
|
||||||
|
Child::Tree(t) => Some(*t),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
||||||
pub struct TreeRef(NonZeroU32);
|
pub struct TreeRef(NonZeroU32);
|
||||||
|
|
||||||
impl TreeRef {
|
impl TreeRef {
|
||||||
|
|
@ -102,13 +180,15 @@ impl TreeRef {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Tree<'a> {
|
impl<'a> Tree<'a> {
|
||||||
pub fn dump(&self, tree: &SyntaxTree<'a>) -> String {
|
pub fn dump(&self, tree: &SyntaxTree<'a>, with_positions: bool, output: &mut String) {
|
||||||
let mut output = String::new();
|
let _ = write!(output, "{:?}", self.kind);
|
||||||
output.push_str(&format!("{:?}\n", self.kind));
|
if with_positions {
|
||||||
for child in self.children.iter() {
|
let _ = write!(output, " [{}, {})", self.start_pos, self.end_pos);
|
||||||
child.dump_rec(2, tree, &mut output);
|
}
|
||||||
|
let _ = write!(output, "\n");
|
||||||
|
for child in self.children.iter() {
|
||||||
|
child.dump_rec(2, tree, with_positions, output);
|
||||||
}
|
}
|
||||||
output
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,21 +198,52 @@ pub enum Child<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Child<'a> {
|
impl<'a> Child<'a> {
|
||||||
fn dump_rec(&self, indent: usize, tree: &SyntaxTree<'a>, output: &mut String) {
|
fn dump_rec(
|
||||||
|
&self,
|
||||||
|
indent: usize,
|
||||||
|
tree: &SyntaxTree<'a>,
|
||||||
|
with_positions: bool,
|
||||||
|
output: &mut String,
|
||||||
|
) {
|
||||||
for _ in 0..indent {
|
for _ in 0..indent {
|
||||||
output.push(' ');
|
let _ = write!(output, " ");
|
||||||
}
|
}
|
||||||
match self {
|
match self {
|
||||||
Child::Token(t) => output.push_str(&format!("{:?}:'{:?}'\n", t.kind, t.as_str())),
|
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::Tree(t) => {
|
Child::Tree(t) => {
|
||||||
let t = &tree[*t];
|
let t = &tree[*t];
|
||||||
output.push_str(&format!("{:?}\n", t.kind));
|
let _ = write!(output, "{:?}", t.kind);
|
||||||
|
if with_positions {
|
||||||
|
let _ = write!(output, " [{}, {})", t.start_pos, t.end_pos);
|
||||||
|
}
|
||||||
|
let _ = write!(output, "\n");
|
||||||
|
|
||||||
for child in t.children.iter() {
|
for child in t.children.iter() {
|
||||||
child.dump_rec(indent + 2, tree, output);
|
child.dump_rec(indent + 2, tree, with_positions, 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> {
|
enum ParseEvent<'a> {
|
||||||
|
|
@ -307,6 +418,8 @@ impl<'a> CParser<'a> {
|
||||||
ParseEvent::Start { kind } => stack.push(Tree {
|
ParseEvent::Start { kind } => stack.push(Tree {
|
||||||
kind,
|
kind,
|
||||||
parent: None,
|
parent: None,
|
||||||
|
start_pos: 0,
|
||||||
|
end_pos: 0,
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
@ -660,6 +773,10 @@ mod tests {
|
||||||
fn tree_ref_size() {
|
fn tree_ref_size() {
|
||||||
// What's the point of doing all that work if the tree ref isn't nice
|
// What's the point of doing all that work if the tree ref isn't nice
|
||||||
// and "small"?
|
// 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::<Option<TreeRef>>());
|
assert_eq!(4, std::mem::size_of::<Option<TreeRef>>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
510
fine/src/semantics.rs
Normal file
510
fine/src/semantics.rs
Normal file
|
|
@ -0,0 +1,510 @@
|
||||||
|
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<T>(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<T>(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<Vec<Error>>,
|
||||||
|
types: RefCell<HashMap<TreeRef, Type>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Error> {
|
||||||
|
(*self.errors.borrow()).clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn report_error<T>(&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<T>(&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<T>(&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<T>(&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<Type> {
|
||||||
|
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<Type> {
|
||||||
|
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<Type> {
|
||||||
|
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<Type> {
|
||||||
|
assert_eq!(tree.kind, TreeKind::TypeExpression);
|
||||||
|
Some(Type::Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_of_block(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
||||||
|
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<Type> {
|
||||||
|
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<Type> {
|
||||||
|
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<Type> {
|
||||||
|
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<Type> {
|
||||||
|
assert_eq!(tree.kind, TreeKind::CallExpression);
|
||||||
|
Some(Type::Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_of_argument(&self, tree: &Tree) -> Option<Type> {
|
||||||
|
assert_eq!(tree.kind, TreeKind::Argument);
|
||||||
|
Some(Type::Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_of_expression_statement(&self, tree: &Tree, value_required: bool) -> Option<Type> {
|
||||||
|
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<Type> {
|
||||||
|
assert_eq!(tree.kind, TreeKind::Identifier);
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -429,7 +429,11 @@ impl<'a> Tokens<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match c {
|
match c {
|
||||||
' ' | '\t' | '\r' | '\n' => self.whitespace(pos),
|
' ' | '\t' | '\r' => self.whitespace(pos),
|
||||||
|
'\n' => {
|
||||||
|
self.lines.add_line(pos);
|
||||||
|
self.whitespace(pos)
|
||||||
|
}
|
||||||
'{' => self.token(pos, TokenKind::LeftBrace),
|
'{' => self.token(pos, TokenKind::LeftBrace),
|
||||||
'}' => self.token(pos, TokenKind::RightBrace),
|
'}' => self.token(pos, TokenKind::RightBrace),
|
||||||
'[' => self.token(pos, TokenKind::LeftBracket),
|
'[' => self.token(pos, TokenKind::LeftBracket),
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
use fine::parser::SyntaxTree;
|
use fine::parser::SyntaxTree;
|
||||||
|
use fine::semantics::{Semantics, Type};
|
||||||
|
use fine::tokens::Lines;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
fn rebase_concrete(source_path: &str, dump: &str) {
|
fn rebase_concrete(source_path: &str, dump: &str) {
|
||||||
|
|
@ -69,7 +71,7 @@ fn rebase_concrete(source_path: &str, dump: &str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) {
|
fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) {
|
||||||
let dump = tree.dump();
|
let dump = tree.dump(false);
|
||||||
let rebase = std::env::var("FINE_TEST_REBASE")
|
let rebase = std::env::var("FINE_TEST_REBASE")
|
||||||
.unwrap_or(String::new())
|
.unwrap_or(String::new())
|
||||||
.to_lowercase();
|
.to_lowercase();
|
||||||
|
|
@ -83,4 +85,53 @@ 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"));
|
include!(concat!(env!("OUT_DIR"), "/generated_tests.rs"));
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,5 @@
|
||||||
// | Semicolon:'";"'
|
// | Semicolon:'";"'
|
||||||
//
|
//
|
||||||
1 * 2 + -3 * 4;
|
1 * 2 + -3 * 4;
|
||||||
|
|
||||||
|
// type: 532 f64
|
||||||
|
|
@ -20,3 +20,5 @@
|
||||||
// | Semicolon:'";"'
|
// | Semicolon:'";"'
|
||||||
//
|
//
|
||||||
true and false or false and !true;
|
true and false or false and !true;
|
||||||
|
|
||||||
|
// type: 549 bool
|
||||||
|
|
|
||||||
|
|
@ -24,3 +24,21 @@
|
||||||
// | RightBrace:'"}"'
|
// | RightBrace:'"}"'
|
||||||
//
|
//
|
||||||
if true { "discarded"; 23 } else { 45 }
|
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
|
||||||
|
|
@ -4,5 +4,7 @@
|
||||||
// | LiteralExpression
|
// | LiteralExpression
|
||||||
// | Number:'"42"'
|
// | Number:'"42"'
|
||||||
// | Semicolon:'";"'
|
// | Semicolon:'";"'
|
||||||
//
|
|
||||||
42;
|
42;
|
||||||
|
|
||||||
|
// type: 129 f64
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,5 @@
|
||||||
// | Semicolon:'";"'
|
// | Semicolon:'";"'
|
||||||
//
|
//
|
||||||
"Hello " + 'world!';
|
"Hello " + 'world!';
|
||||||
|
|
||||||
|
// type: 261 string
|
||||||
Loading…
Add table
Add a link
Reference in a new issue