From 087d5f7bfcf58b5d57366b2613607bcf8ccd21a0 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 5 Jan 2025 21:20:31 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20`'de`=20to=20`'as?= =?UTF-8?q?t`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit semver: chore --- src/lexer/mod.rs | 32 ++++---- src/lexer/token.rs | 12 +-- src/main.rs | 5 -- src/parser/ast/mod.rs | 48 ++++++------ src/parser/ast/untyped.rs | 118 ++++++++++++++--------------- src/parser/expression/binary.rs | 112 +++++++++++++-------------- src/parser/expression/mod.rs | 16 ++-- src/parser/expression/primitive.rs | 12 +-- src/parser/expression/unary.rs | 4 +- src/parser/lookup.rs | 48 ++++++------ src/parser/mod.rs | 12 +-- src/parser/statement.rs | 20 ++--- src/parser/typechecker/mod.rs | 19 +---- src/parser/typing.rs | 22 +++--- 14 files changed, 231 insertions(+), 249 deletions(-) diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index a77d390..01d1f22 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -3,15 +3,15 @@ pub use token::*; use miette::{LabeledSpan, Result, SourceSpan}; -pub struct Lexer<'de> { - whole: &'de str, - remainder: &'de str, +pub struct Lexer<'ast> { + whole: &'ast str, + remainder: &'ast str, byte_offset: usize, - peeked: Option, miette::Error>>, + peeked: Option, miette::Error>>, } -impl<'de> Lexer<'de> { - pub fn new(input: &'de str) -> Self { +impl<'ast> Lexer<'ast> { + pub fn new(input: &'ast str) -> Self { Self { whole: input, remainder: input, @@ -24,7 +24,7 @@ impl<'de> Lexer<'de> { &mut self, expected: TokenKind, unexpected: &str, - ) -> Result, miette::Error> { + ) -> Result, miette::Error> { match self.next() { Some(Ok(token)) if expected == token.kind => Ok(token), Some(Ok(token)) => Err(miette::miette! { @@ -50,9 +50,9 @@ impl<'de> Lexer<'de> { pub fn expect_where( &mut self, - mut check: impl FnMut(&Token<'de>) -> bool, + mut check: impl FnMut(&Token<'ast>) -> bool, unexpected: &str, - ) -> Result, miette::Error> { + ) -> Result, miette::Error> { match self.next() { Some(Ok(token)) if check(&token) => Ok(token), Some(Ok(token)) => Err(miette::miette! { @@ -73,11 +73,11 @@ impl<'de> Lexer<'de> { } } - pub fn expect_any(&mut self, unexpected: &str) -> Result, miette::Error> { + pub fn expect_any(&mut self, unexpected: &str) -> Result, miette::Error> { self.expect_where(|_| true, unexpected) } - pub fn peek(&mut self) -> Option<&Result, miette::Error>> { + pub fn peek(&mut self) -> Option<&Result, miette::Error>> { if self.peeked.is_some() { return self.peeked.as_ref(); } @@ -89,7 +89,7 @@ impl<'de> Lexer<'de> { pub fn peek_expect( &mut self, expected: TokenKind, - ) -> Option<&Result, miette::Error>> { + ) -> Option<&Result, miette::Error>> { match self.peek() { Some(Ok(token::Token { kind, .. })) => { if *kind == expected { @@ -107,7 +107,7 @@ impl<'de> Lexer<'de> { single: TokenKind, compound: TokenKind, expected_char: char, - ) -> Result<(TokenKind, TokenValue<'de>)> { + ) -> Result<(TokenKind, TokenValue<'ast>)> { if let Some(c) = self.remainder.chars().next() { if c == expected_char { self.remainder = &self.remainder[c.len_utf8()..]; @@ -122,8 +122,8 @@ impl<'de> Lexer<'de> { } } -impl<'de> Iterator for Lexer<'de> { - type Item = Result>; +impl<'ast> Iterator for Lexer<'ast> { + type Item = Result>; fn next(&mut self) -> Option { if let Some(next) = self.peeked.take() { @@ -138,7 +138,7 @@ impl<'de> Iterator for Lexer<'de> { self.remainder = chars.as_str(); self.byte_offset += c.len_utf8(); - let kind: Result<(TokenKind, TokenValue<'de>)> = match c { + let kind: Result<(TokenKind, TokenValue<'ast>)> = match c { '(' => Ok((TokenKind::ParenOpen, TokenValue::None)), ')' => Ok((TokenKind::ParenClose, TokenValue::None)), '{' => Ok((TokenKind::CurlyOpen, TokenValue::None)), diff --git a/src/lexer/token.rs b/src/lexer/token.rs index f621dac..e01190e 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -1,10 +1,10 @@ use std::{borrow::Cow, fmt::Display, hash::Hash}; #[derive(Debug, Clone, PartialEq)] -pub struct Token<'de> { +pub struct Token<'ast> { pub kind: TokenKind, - pub value: TokenValue<'de>, - pub original: &'de str, + pub value: TokenValue<'ast>, + pub original: &'ast str, pub span: miette::SourceSpan, } @@ -24,14 +24,14 @@ impl Display for Token<'_> { } #[derive(Debug, Clone, PartialEq)] -pub enum TokenValue<'de> { +pub enum TokenValue<'ast> { None, Boolean(bool), Integer(i64), Decimal(f64), - String(Cow<'de, str>), + String(Cow<'ast, str>), Character(char), - Identifier(Cow<'de, str>), + Identifier(Cow<'ast, str>), } impl Display for TokenValue<'_> { diff --git a/src/main.rs b/src/main.rs index d96a93a..7d21573 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,12 +14,7 @@ type HexCode = int; fn main() { let a = |a ~ HexCode| { 1 + 1; a }; - - let b = a; - - let c = b(2); } - "; fn main() { diff --git a/src/parser/ast/mod.rs b/src/parser/ast/mod.rs index 4000529..da6f66e 100644 --- a/src/parser/ast/mod.rs +++ b/src/parser/ast/mod.rs @@ -6,13 +6,13 @@ pub mod typed; pub mod untyped; #[derive(Debug, Clone)] -pub struct Type<'de> { - pub value: TypeValue<'de>, +pub struct Type<'ast> { + pub value: TypeValue<'ast>, pub span: SourceSpan, pub original_span: Option, } -impl<'de> Type<'de> { +impl<'ast> Type<'ast> { pub fn label(&self, text: impl Into) -> Vec { let labels = vec![miette::LabeledSpan::at(self.span, text.into())]; @@ -74,7 +74,7 @@ impl<'de> Type<'de> { } } - pub fn symbol(span: SourceSpan, name: Cow<'de, str>) -> Self { + pub fn symbol(span: SourceSpan, name: Cow<'ast, str>) -> Self { Self { value: TypeValue::Symbol(name), span, @@ -82,7 +82,7 @@ impl<'de> Type<'de> { } } - pub fn collection(span: SourceSpan, element: Type<'de>) -> Self { + pub fn collection(span: SourceSpan, element: Type<'ast>) -> Self { Self { value: TypeValue::Collection(Box::new(element)), span, @@ -90,7 +90,7 @@ impl<'de> Type<'de> { } } - pub fn set(span: SourceSpan, element: Type<'de>) -> Self { + pub fn set(span: SourceSpan, element: Type<'ast>) -> Self { Self { value: TypeValue::Set(Box::new(element)), span, @@ -98,7 +98,11 @@ impl<'de> Type<'de> { } } - pub fn function(span: SourceSpan, parameters: Vec>, return_type: Type<'de>) -> Self { + pub fn function( + span: SourceSpan, + parameters: Vec>, + return_type: Type<'ast>, + ) -> Self { Self { value: TypeValue::Function { parameters, @@ -109,7 +113,7 @@ impl<'de> Type<'de> { } } - pub fn alias(span: SourceSpan, name: Cow<'de, str>, alias: Type<'de>) -> Self { + pub fn alias(span: SourceSpan, name: Cow<'ast, str>, alias: Type<'ast>) -> Self { Self { value: TypeValue::Alias(name, Box::new(alias)), span, @@ -142,20 +146,20 @@ impl PartialEq for Type<'_> { } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum TypeValue<'de> { +pub enum TypeValue<'ast> { Unit, Boolean, Integer, Decimal, Character, String, - Alias(Cow<'de, str>, Box>), - Symbol(Cow<'de, str>), - Collection(Box>), - Set(Box>), + Alias(Cow<'ast, str>, Box>), + Symbol(Cow<'ast, str>), + Collection(Box>), + Set(Box>), Function { - parameters: Vec>, - return_type: Box>, + parameters: Vec>, + return_type: Box>, }, } @@ -202,7 +206,7 @@ impl Display for TypeValue<'_> { } } -pub trait Spannable<'de>: Sized { +pub trait Spannable<'ast>: Sized { type Value; fn at(span: miette::SourceSpan, value: Self::Value) -> Self; @@ -248,24 +252,24 @@ pub trait CombineSpan { impl CombineSpan for SourceSpan {} -impl<'de> Spannable<'de> for untyped::Expression<'de> { - type Value = untyped::ExpressionValue<'de>; +impl<'ast> Spannable<'ast> for untyped::Expression<'ast> { + type Value = untyped::ExpressionValue<'ast>; fn at(span: miette::SourceSpan, value: Self::Value) -> Self { Self { value, span } } } -impl<'de> Spannable<'de> for untyped::Statement<'de> { - type Value = untyped::StatementValue<'de>; +impl<'ast> Spannable<'ast> for untyped::Statement<'ast> { + type Value = untyped::StatementValue<'ast>; fn at(span: miette::SourceSpan, value: Self::Value) -> Self { Self { value, span } } } -impl<'de> Spannable<'de> for Type<'de> { - type Value = TypeValue<'de>; +impl<'ast> Spannable<'ast> for Type<'ast> { + type Value = TypeValue<'ast>; fn at(span: miette::SourceSpan, value: Self::Value) -> Self { Self { diff --git a/src/parser/ast/untyped.rs b/src/parser/ast/untyped.rs index d6a4d1a..c4c49d4 100644 --- a/src/parser/ast/untyped.rs +++ b/src/parser/ast/untyped.rs @@ -2,50 +2,50 @@ use super::Type; use std::{borrow::Cow, fmt::Display}; #[derive(Debug, Clone)] -pub enum Symbol<'de> { - Statement(Statement<'de>), - Expression(Expression<'de>), +pub enum Symbol<'ast> { + Statement(Statement<'ast>), + Expression(Expression<'ast>), } #[derive(Debug, Clone)] -pub struct Statement<'de> { - pub value: StatementValue<'de>, +pub struct Statement<'ast> { + pub value: StatementValue<'ast>, pub span: miette::SourceSpan, } #[derive(Debug, Clone)] -pub enum StatementValue<'de> { - Block(Vec>), - Expression(Expression<'de>), +pub enum StatementValue<'ast> { + Block(Vec>), + Expression(Expression<'ast>), Assignment { - name: Cow<'de, str>, - value: Expression<'de>, + name: Cow<'ast, str>, + value: Expression<'ast>, }, Struct { - name: Cow<'de, str>, - fields: Vec>, + name: Cow<'ast, str>, + fields: Vec>, }, Enum { - name: Cow<'de, str>, - variants: Vec>, + name: Cow<'ast, str>, + variants: Vec>, }, Function { - header: FunctionHeader<'de>, - body: Expression<'de>, + header: FunctionHeader<'ast>, + body: Expression<'ast>, }, Trait { - name: Cow<'de, str>, - functions: Vec>, + name: Cow<'ast, str>, + functions: Vec>, }, - Return(Expression<'de>), + Return(Expression<'ast>), Conditional { - condition: Box>, - truthy: Box>, - falsy: Option>>, + condition: Box>, + truthy: Box>, + falsy: Option>>, }, TypeAlias { - name: std::borrow::Cow<'de, str>, - explicit_type: crate::parser::ast::Type<'de>, + name: std::borrow::Cow<'ast, str>, + explicit_type: crate::parser::ast::Type<'ast>, }, } @@ -165,8 +165,8 @@ impl Display for UnaryOperator { } #[derive(Debug, Clone)] -pub struct Expression<'de> { - pub value: ExpressionValue<'de>, +pub struct Expression<'ast> { + pub value: ExpressionValue<'ast>, pub span: miette::SourceSpan, } @@ -183,75 +183,75 @@ impl Statement<'_> { } #[derive(Debug, Clone)] -pub enum ExpressionValue<'de> { - Primitive(Primitive<'de>), +pub enum ExpressionValue<'ast> { + Primitive(Primitive<'ast>), Binary { operator: BinaryOperator, - left: Box>, - right: Box>, + left: Box>, + right: Box>, }, Unary { operator: UnaryOperator, - operand: Box>, + operand: Box>, }, - Group(Box>), + Group(Box>), Block { - statements: Vec>, - return_value: Box>, + statements: Vec>, + return_value: Box>, }, Conditional { - condition: Box>, - truthy: Box>, - falsy: Box>, + condition: Box>, + truthy: Box>, + falsy: Box>, }, Call { - callee: Box>, - arguments: Vec>, + callee: Box>, + arguments: Vec>, }, - Lambda(Lambda<'de>), + Lambda(Lambda<'ast>), } #[derive(Debug, Clone)] -pub struct FunctionHeader<'de> { - pub name: Cow<'de, str>, - pub parameters: Vec>, - pub explicit_return_type: Option>, +pub struct FunctionHeader<'ast> { + pub name: Cow<'ast, str>, + pub parameters: Vec>, + pub explicit_return_type: Option>, pub span: miette::SourceSpan, } #[derive(Debug, Clone)] -pub struct Lambda<'de> { - pub parameters: Vec>, - pub body: Box>, +pub struct Lambda<'ast> { + pub parameters: Vec>, + pub body: Box>, } #[derive(Debug, Clone)] -pub struct ParameterDeclaration<'de> { - pub name: Cow<'de, str>, - pub explicit_type: Type<'de>, +pub struct ParameterDeclaration<'ast> { + pub name: Cow<'ast, str>, + pub explicit_type: Type<'ast>, pub span: miette::SourceSpan, } #[derive(Debug, Clone)] -pub struct StructMemberDeclaration<'de> { - pub name: Cow<'de, str>, - pub explicit_type: Type<'de>, +pub struct StructMemberDeclaration<'ast> { + pub name: Cow<'ast, str>, + pub explicit_type: Type<'ast>, pub span: miette::SourceSpan, } #[derive(Debug, Clone)] -pub struct EnumMemberDeclaration<'de> { - pub name: Cow<'de, str>, - pub value_type: Option>, +pub struct EnumMemberDeclaration<'ast> { + pub name: Cow<'ast, str>, + pub value_type: Option>, pub span: miette::SourceSpan, } #[derive(Debug, Clone)] -pub enum Primitive<'de> { +pub enum Primitive<'ast> { Integer(i64), Decimal(f64), - String(Cow<'de, str>), - Identifier(Cow<'de, str>), + String(Cow<'ast, str>), + Identifier(Cow<'ast, str>), Character(char), Boolean(bool), Unit, diff --git a/src/parser/expression/binary.rs b/src/parser/expression/binary.rs index 59a1ce0..1cd3779 100644 --- a/src/parser/expression/binary.rs +++ b/src/parser/expression/binary.rs @@ -8,12 +8,12 @@ use crate::parser::{ }; use miette::Result; -pub fn parse_binary_expression<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn parse_binary_expression<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, operator: BinaryOperator, -) -> Result> { +) -> Result> { let rhs = crate::parser::expression::parse(parser, bp)?; Ok(Expression::at_multiple( @@ -26,106 +26,106 @@ pub fn parse_binary_expression<'de>( )) } -pub fn addition<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn addition<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Add) } -pub fn multiplication<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn multiplication<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Multiply) } -pub fn subtraction<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn subtraction<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Subtract) } -pub fn division<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn division<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Divide) } -pub fn modulo<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn modulo<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Modulo) } -pub fn equal<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn equal<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Equality) } -pub fn not_equal<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn not_equal<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Inequality) } -pub fn less_than<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn less_than<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::LessThan) } -pub fn less_than_or_equal<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn less_than_or_equal<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::LessThanOrEqual) } -pub fn greater_than<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn greater_than<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::GreaterThan) } -pub fn greater_than_or_equal<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn greater_than_or_equal<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::GreaterThanOrEqual) } -pub fn and<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn and<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::And) } -pub fn or<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn or<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, bp: BindingPower, -) -> Result> { +) -> Result> { parse_binary_expression(parser, lhs, bp, BinaryOperator::Or) } diff --git a/src/parser/expression/mod.rs b/src/parser/expression/mod.rs index 5dd29a2..14781ea 100644 --- a/src/parser/expression/mod.rs +++ b/src/parser/expression/mod.rs @@ -16,10 +16,10 @@ pub mod binary; pub mod primitive; pub mod unary; -pub fn parse<'de>( - parser: &mut Parser<'de>, +pub fn parse<'ast>( + parser: &mut Parser<'ast>, binding_power: BindingPower, -) -> Result> { +) -> Result> { let token = match parser.lexer.peek().as_ref() { Some(Ok(token)) => token, Some(Err(err)) => return Err(miette::miette!(err.to_string())), // FIXME: better error handling @@ -77,11 +77,11 @@ pub fn parse<'de>( Ok(lhs) } -pub fn call<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +pub fn call<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, _binding_power: BindingPower, -) -> Result> { +) -> Result> { let mut arguments = Vec::new(); while parser.lexer.peek().is_some_and(|token| { @@ -112,7 +112,7 @@ pub fn call<'de>( )) } -pub fn lambda<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn lambda<'ast>(parser: &mut Parser<'ast>) -> Result> { parser .lexer .expect(TokenKind::Pipe, "expected a pipe before lambda arguments")?; diff --git a/src/parser/expression/primitive.rs b/src/parser/expression/primitive.rs index 38146da..8d5bb86 100644 --- a/src/parser/expression/primitive.rs +++ b/src/parser/expression/primitive.rs @@ -10,7 +10,7 @@ use crate::{ }; use miette::Result; -pub fn integer<'de>(parser: &mut Parser) -> Result> { +pub fn integer<'ast>(parser: &mut Parser) -> Result> { let token = parser .lexer .expect(TokenKind::Integer, "expected an integer")?; @@ -26,7 +26,7 @@ pub fn integer<'de>(parser: &mut Parser) -> Result> { )) } -pub fn decimal<'de>(parser: &mut Parser) -> Result> { +pub fn decimal<'ast>(parser: &mut Parser) -> Result> { let token = parser .lexer .expect(TokenKind::Decimal, "expected a decimal")?; @@ -42,7 +42,7 @@ pub fn decimal<'de>(parser: &mut Parser) -> Result> { )) } -pub fn boolean<'de>(parser: &mut Parser) -> Result> { +pub fn boolean<'ast>(parser: &mut Parser) -> Result> { let token = parser .lexer .expect(TokenKind::Boolean, "expected a boolean")?; @@ -58,7 +58,7 @@ pub fn boolean<'de>(parser: &mut Parser) -> Result> { )) } -pub fn character<'de>(parser: &mut Parser) -> Result> { +pub fn character<'ast>(parser: &mut Parser) -> Result> { let token = parser .lexer .expect(TokenKind::Character, "expected a character")?; @@ -74,7 +74,7 @@ pub fn character<'de>(parser: &mut Parser) -> Result> { )) } -pub fn string<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn string<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::String, "expected a string")?; @@ -90,7 +90,7 @@ pub fn string<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn identifier<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn identifier<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Identifier, "expected an identifier")?; diff --git a/src/parser/expression/unary.rs b/src/parser/expression/unary.rs index 1cfbc1b..471fa34 100644 --- a/src/parser/expression/unary.rs +++ b/src/parser/expression/unary.rs @@ -11,7 +11,7 @@ use crate::{ }; use miette::Result; -pub fn negate<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn negate<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Not, "expected a negate operator")?; @@ -26,7 +26,7 @@ pub fn negate<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn negative<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn negative<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Minus, "expected a negative operator")?; diff --git a/src/parser/lookup.rs b/src/parser/lookup.rs index c624a80..ba9bed4 100644 --- a/src/parser/lookup.rs +++ b/src/parser/lookup.rs @@ -24,27 +24,27 @@ pub enum BindingPower { Primary = 10, } -pub type TypeHandler<'de> = fn(&mut Parser<'de>) -> Result>; -pub type LeftTypeHandler<'de> = fn(&mut Parser<'de>, Type, BindingPower) -> Result>; -pub type StatementHandler<'de> = fn(&mut Parser<'de>) -> Result>; -pub type ExpressionHandler<'de> = fn(&mut Parser<'de>) -> Result>; -pub type LeftExpressionHandler<'de> = - fn(&mut Parser<'de>, Expression<'de>, BindingPower) -> Result>; +pub type TypeHandler<'ast> = fn(&mut Parser<'ast>) -> Result>; +pub type LeftTypeHandler<'ast> = fn(&mut Parser<'ast>, Type, BindingPower) -> Result>; +pub type StatementHandler<'ast> = fn(&mut Parser<'ast>) -> Result>; +pub type ExpressionHandler<'ast> = fn(&mut Parser<'ast>) -> Result>; +pub type LeftExpressionHandler<'ast> = + fn(&mut Parser<'ast>, Expression<'ast>, BindingPower) -> Result>; -pub struct Lookup<'de> { - pub statement_lookup: HashMap>, - pub expression_lookup: HashMap>, - pub left_expression_lookup: HashMap>, - pub type_lookup: HashMap>, - pub left_type_lookup: HashMap>, +pub struct Lookup<'ast> { + pub statement_lookup: HashMap>, + pub expression_lookup: HashMap>, + pub left_expression_lookup: HashMap>, + pub type_lookup: HashMap>, + pub left_type_lookup: HashMap>, pub binding_power_lookup: HashMap, } -impl<'de> Lookup<'de> { +impl<'ast> Lookup<'ast> { pub(crate) fn add_statement_handler( mut self, token: TokenKind, - handler: StatementHandler<'de>, + handler: StatementHandler<'ast>, ) -> Self { if self.statement_lookup.contains_key(&token) { panic!("Token already has a statement handler"); @@ -57,7 +57,7 @@ impl<'de> Lookup<'de> { pub(crate) fn add_expression_handler( mut self, token: TokenKind, - handler: ExpressionHandler<'de>, + handler: ExpressionHandler<'ast>, ) -> Self { if self.expression_lookup.contains_key(&token) { panic!("Token already has an expression handler"); @@ -71,7 +71,7 @@ impl<'de> Lookup<'de> { mut self, token: TokenKind, binding_power: BindingPower, - handler: LeftExpressionHandler<'de>, + handler: LeftExpressionHandler<'ast>, ) -> Self { if self.binding_power_lookup.contains_key(&token) { panic!("Token already has a binding power"); @@ -82,7 +82,7 @@ impl<'de> Lookup<'de> { self } - pub(crate) fn add_type_handler(mut self, token: TokenKind, handler: TypeHandler<'de>) -> Self { + pub(crate) fn add_type_handler(mut self, token: TokenKind, handler: TypeHandler<'ast>) -> Self { if self.type_lookup.contains_key(&token) { panic!("Token already has a type handler"); } @@ -95,7 +95,7 @@ impl<'de> Lookup<'de> { pub(crate) fn add_left_type_handler( mut self, token: TokenKind, - handler: LeftTypeHandler<'de>, + handler: LeftTypeHandler<'ast>, ) -> Self { if self.left_type_lookup.contains_key(&token) { panic!("Token already has a left type handler"); @@ -215,11 +215,11 @@ impl Default for Lookup<'_> { } } -fn conditional<'de>( - parser: &mut Parser<'de>, - lhs: Expression<'de>, +fn conditional<'ast>( + parser: &mut Parser<'ast>, + lhs: Expression<'ast>, binding_power: BindingPower, -) -> Result> { +) -> Result> { let condition = expression::parse(parser, binding_power.clone())?; let token = parser @@ -238,7 +238,7 @@ fn conditional<'de>( )) } -fn group<'de>(parser: &mut Parser<'de>) -> Result> { +fn group<'ast>(parser: &mut Parser<'ast>) -> Result> { let open = parser .lexer .expect(TokenKind::ParenOpen, "expected a left parenthesis")?; @@ -253,7 +253,7 @@ fn group<'de>(parser: &mut Parser<'de>) -> Result> { )) } -fn block<'de>(parser: &mut Parser<'de>) -> Result> { +fn block<'ast>(parser: &mut Parser<'ast>) -> Result> { let open = parser .lexer .expect(TokenKind::CurlyOpen, "expected a left curly brace")?; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 895c52b..f7f01ea 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -13,20 +13,20 @@ pub mod statement; pub mod typechecker; pub mod typing; -pub struct Parser<'de> { - lexer: Lexer<'de>, - lookup: Lookup<'de>, +pub struct Parser<'ast> { + lexer: Lexer<'ast>, + lookup: Lookup<'ast>, } -impl<'de> Parser<'de> { - pub fn new(lexer: Lexer<'de>) -> Self { +impl<'ast> Parser<'ast> { + pub fn new(lexer: Lexer<'ast>) -> Self { Self { lexer, lookup: Lookup::default(), } } - pub fn parse(&mut self) -> Result> { + pub fn parse(&mut self) -> Result> { let mut statements = vec![]; while self.lexer.peek().is_some() { diff --git a/src/parser/statement.rs b/src/parser/statement.rs index 40012b0..e96f42d 100644 --- a/src/parser/statement.rs +++ b/src/parser/statement.rs @@ -14,7 +14,7 @@ use crate::lexer::{TokenKind, TokenValue}; use crate::parser::ast::CombineSpan; use miette::{Context, Result, SourceSpan}; -pub fn parse<'de>(parser: &mut Parser<'de>, optional_semicolon: bool) -> Result> { +pub fn parse<'ast>(parser: &mut Parser<'ast>, optional_semicolon: bool) -> Result> { let token = match parser.lexer.peek().as_ref() { Some(Ok(token)) => token, Some(Err(err)) => return Err(miette::miette!(err.to_string())), // FIXME: better error handling @@ -58,7 +58,7 @@ pub fn parse<'de>(parser: &mut Parser<'de>, optional_semicolon: bool) -> Result< Ok(statement) } -pub fn let_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn let_<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Let, "expected a let keyword")?; @@ -83,7 +83,7 @@ pub fn let_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn struct_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn struct_<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Struct, "expected a struct keyword")?; @@ -142,7 +142,7 @@ pub fn struct_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn enum_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn enum_<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Enum, "expected an enum keyword")?; @@ -197,7 +197,7 @@ pub fn enum_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn function_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn function_<'ast>(parser: &mut Parser<'ast>) -> Result> { let header = parse_function_header(parser)?; let body = expression::parse(parser, BindingPower::None)?; @@ -207,7 +207,7 @@ pub fn function_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn trait_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn trait_<'ast>(parser: &mut Parser<'ast>) -> Result> { parser .lexer .expect(TokenKind::Trait, "expected a trait keyword")?; @@ -249,7 +249,7 @@ pub fn trait_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn return_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn return_<'ast>(parser: &mut Parser<'ast>) -> Result> { parser .lexer .expect(TokenKind::Return, "expected a return keyword")?; @@ -262,7 +262,7 @@ pub fn return_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn if_<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn if_<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::If, "expected an if keyword")?; @@ -292,7 +292,7 @@ pub fn if_<'de>(parser: &mut Parser<'de>) -> Result> { )) } -fn parse_function_header<'de>(parser: &mut Parser<'de>) -> Result> { +fn parse_function_header<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Function, "expected a function keyword")?; @@ -368,7 +368,7 @@ fn parse_function_header<'de>(parser: &mut Parser<'de>) -> Result(parser: &mut Parser<'de>) -> Result> { +pub fn type_<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Type, "expected a type keyword")?; diff --git a/src/parser/typechecker/mod.rs b/src/parser/typechecker/mod.rs index 1f7e7e7..646bbf3 100644 --- a/src/parser/typechecker/mod.rs +++ b/src/parser/typechecker/mod.rs @@ -292,24 +292,7 @@ impl<'ast> TypeChecker<'ast> { let mut environment = Environment::new(Some(environment)); for parameter in &lambda.parameters { - // If it's a symbol, it's a type alias - let explicit_type = match parameter.explicit_type.clone() { - Type::Symbol(name) => environment - .get(&name) - .cloned() - .ok_or_else(|| { - vec![MietteDiagnostic { - code: None, - severity: None, - url: None, - labels: Some(vec![parameter.label("undeclared type")]), - help: Some(format!("{} is not declared", name)), - message: "undeclared type".to_owned(), - }] - })?, - ty => ty, - }; - + println!("{:?}", parameter); environment.set(parameter.name.clone(), parameter.explicit_type.clone()); } diff --git a/src/parser/typing.rs b/src/parser/typing.rs index a9accbf..1a4c96f 100644 --- a/src/parser/typing.rs +++ b/src/parser/typing.rs @@ -8,7 +8,7 @@ use super::{ use crate::lexer::{TokenKind, TokenValue}; use miette::Result; -pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Result> { +pub fn parse<'ast>(parser: &mut Parser<'ast>, binding_power: BindingPower) -> Result> { let token = match parser.lexer.peek().as_ref() { Some(Ok(token)) => token, Some(Err(err)) => return Err(miette::miette!(err.to_string())), // FIXME: better error handling @@ -66,7 +66,7 @@ pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Resu Ok(lhs) } -pub fn unit<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn unit<'ast>(parser: &mut Parser<'ast>) -> Result> { let open = parser .lexer .expect(TokenKind::ParenOpen, "expected an opening parenthesis")?; @@ -81,7 +81,7 @@ pub fn unit<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn boolean<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn boolean<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::BooleanType, "expected a boolean type")?; @@ -89,7 +89,7 @@ pub fn boolean<'de>(parser: &mut Parser<'de>) -> Result> { Ok(Type::at(token.span, TypeValue::Boolean)) } -pub fn integer<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn integer<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::IntegerType, "expected an integer type")?; @@ -97,7 +97,7 @@ pub fn integer<'de>(parser: &mut Parser<'de>) -> Result> { Ok(Type::at(token.span, TypeValue::Integer)) } -pub fn decimal<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn decimal<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::DecimalType, "expected a decimal type")?; @@ -105,7 +105,7 @@ pub fn decimal<'de>(parser: &mut Parser<'de>) -> Result> { Ok(Type::at(token.span, TypeValue::Decimal)) } -pub fn string<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn string<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::StringType, "expected a string type")?; @@ -113,7 +113,7 @@ pub fn string<'de>(parser: &mut Parser<'de>) -> Result> { Ok(Type::string(token.span)) } -pub fn character<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn character<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::CharacterType, "expected a character type")?; @@ -121,7 +121,7 @@ pub fn character<'de>(parser: &mut Parser<'de>) -> Result> { Ok(Type::character(token.span)) } -pub fn collection<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn collection<'ast>(parser: &mut Parser<'ast>) -> Result> { let open = parser .lexer .expect(TokenKind::SquareOpen, "expected an opening bracket")?; @@ -136,7 +136,7 @@ pub fn collection<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn set<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn set<'ast>(parser: &mut Parser<'ast>) -> Result> { let open = parser .lexer .expect(TokenKind::CurlyOpen, "expected an opening curly brace")?; @@ -153,7 +153,7 @@ pub fn set<'de>(parser: &mut Parser<'de>) -> Result> { )) } -pub fn identifier<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn identifier<'ast>(parser: &mut Parser<'ast>) -> Result> { let token = parser .lexer .expect(TokenKind::Identifier, "expected an identifier")?; @@ -166,7 +166,7 @@ pub fn identifier<'de>(parser: &mut Parser<'de>) -> Result> { Ok(Type::symbol(token.span, name)) } -pub fn function<'de>(parser: &mut Parser<'de>) -> Result> { +pub fn function<'ast>(parser: &mut Parser<'ast>) -> Result> { let function = parser .lexer .expect(TokenKind::Function, "expected a function type")?;