diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 7c05481..88b5c9c 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -195,6 +195,12 @@ impl<'de> Iterator for Lexer<'de> { "struct" => Ok((TokenKind::Struct, TokenValue::None)), "enum" => Ok((TokenKind::Enum, TokenValue::None)), "trait" => Ok((TokenKind::Trait, TokenValue::None)), + "bool" => Ok((TokenKind::BooleanType, TokenValue::None)), + "int" => Ok((TokenKind::IntegerType, TokenValue::None)), + "dec" => Ok((TokenKind::DecimalType, TokenValue::None)), + "str" => Ok((TokenKind::StringType, TokenValue::None)), + "char" => Ok((TokenKind::CharacterType, TokenValue::None)), + "()" => Ok((TokenKind::UnitType, TokenValue::None)), ident => Ok(( TokenKind::Identifier, TokenValue::Identifier(ident.to_string().into()), diff --git a/src/lexer/token.rs b/src/lexer/token.rs index 581994e..970ccca 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -165,6 +165,19 @@ pub enum TokenKind { Enum, /// A trait keyword; `trait`. Trait, + + /// The boolean type; `bool`. + BooleanType, + /// The integer type; `int`. + IntegerType, + /// The decimal type; `dec`. + DecimalType, + /// The string type; `str`. + StringType, + /// The character type; `char`. + CharacterType, + /// The unit type; `()`. + UnitType, } impl Display for TokenKind { @@ -221,6 +234,12 @@ impl Display for TokenKind { TokenKind::And => write!(f, "`&&`"), TokenKind::Or => write!(f, "`||`"), TokenKind::Trait => write!(f, "`trait`"), + TokenKind::BooleanType => write!(f, "a boolean type"), + TokenKind::IntegerType => write!(f, "an integer type"), + TokenKind::DecimalType => write!(f, "a decimal type"), + TokenKind::StringType => write!(f, "a string type"), + TokenKind::CharacterType => write!(f, "a character type"), + TokenKind::UnitType => write!(f, "a unit type"), } } } diff --git a/src/main.rs b/src/main.rs index 4da81c5..c36b56e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,9 @@ fn main() { fib(9999); } - fn fib(n ~ number) ~ [number] { + fn fib(n ~ int) ~ int { return if n < 2; - fib(n - 1) + fib(n - 2) + fib(n - 1) + fib(n - 20) } "; @@ -76,7 +76,12 @@ impl miette::highlighters::HighlighterState for SomHighlighterState { Style::new().fg_rgb::<209, 154, 102>() } TokenKind::Boolean => Style::new().fg_rgb::<86, 156, 214>(), - + TokenKind::IntegerType + | TokenKind::DecimalType + | TokenKind::BooleanType + | TokenKind::StringType + | TokenKind::CharacterType + | TokenKind::UnitType => Style::new().fg_rgb::<86, 156, 214>().italic(), TokenKind::Equal | TokenKind::LessThan | TokenKind::GreaterThan diff --git a/src/parser/ast.rs b/src/parser/ast.rs index c2712e7..c59bac6 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -122,6 +122,11 @@ pub enum UnaryOperator { #[derive(Debug, Clone, PartialEq, Eq)] pub enum Type<'de> { Unit, + Boolean, + Integer, + Decimal, + Character, + String, Symbol(Cow<'de, str>), Collection(Box>), Set(Box>), diff --git a/src/parser/lookup.rs b/src/parser/lookup.rs index e2af3c0..e214595 100644 --- a/src/parser/lookup.rs +++ b/src/parser/lookup.rs @@ -196,7 +196,12 @@ impl Default for Lookup<'_> { .add_statement_handler(TokenKind::Function, statement::function_) .add_statement_handler(TokenKind::Trait, statement::trait_) .add_type_handler(TokenKind::Identifier, typing::identifier) - .add_type_handler(TokenKind::ParenOpen, typing::unit) + .add_type_handler(TokenKind::UnitType, typing::unit) + .add_type_handler(TokenKind::CharacterType, typing::character) + .add_type_handler(TokenKind::BooleanType, typing::boolean) + .add_type_handler(TokenKind::IntegerType, typing::integer) + .add_type_handler(TokenKind::DecimalType, typing::decimal) + .add_type_handler(TokenKind::StringType, typing::string) .add_type_handler(TokenKind::SquareOpen, typing::collection) .add_type_handler(TokenKind::CurlyOpen, typing::set) } diff --git a/src/parser/typing.rs b/src/parser/typing.rs index 363031a..10e673b 100644 --- a/src/parser/typing.rs +++ b/src/parser/typing.rs @@ -63,12 +63,49 @@ pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Resu pub fn unit<'de>(parser: &mut Parser<'de>) -> Result> { parser .lexer - .expect(TokenKind::ParenOpen, "expected an opening parenthesis")?; + .expect(TokenKind::UnitType, "expected an unit type")?; + + Ok(Type::Unit) +} + +pub fn boolean<'de>(parser: &mut Parser<'de>) -> Result> { parser .lexer - .expect(TokenKind::ParenClose, "expected a closing parenthesis")?; + .expect(TokenKind::BooleanType, "expected a boolean type")?; - Ok(Type::Unit) + Ok(Type::Boolean) +} + +pub fn integer<'de>(parser: &mut Parser<'de>) -> Result> { + parser + .lexer + .expect(TokenKind::IntegerType, "expected an integer type")?; + + Ok(Type::Integer) +} + +pub fn decimal<'de>(parser: &mut Parser<'de>) -> Result> { + parser + .lexer + .expect(TokenKind::DecimalType, "expected a decimal type")?; + + Ok(Type::Decimal) +} + +pub fn string<'de>(parser: &mut Parser<'de>) -> Result> { + parser + .lexer + .expect(TokenKind::StringType, "expected a string type")?; + + Ok(Type::String) +} + +pub fn character<'de>(parser: &mut Parser<'de>) -> Result> { + parser + .lexer + .expect(TokenKind::CharacterType, "expected a character type")?; + + Ok(Type::Character) } pub fn collection<'de>(parser: &mut Parser<'de>) -> Result> {