Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Somfic committed Oct 30, 2024
1 parent e0ad300 commit 8f2aa93
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
19 changes: 19 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"),
}
}
}
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
";

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type<'de>>),
Set(Box<Type<'de>>),
Expand Down
7 changes: 6 additions & 1 deletion src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
43 changes: 40 additions & 3 deletions src/parser/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type<'de>> {
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<Type<'de>> {
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<Type<'de>> {
parser
.lexer
.expect(TokenKind::IntegerType, "expected an integer type")?;

Ok(Type::Integer)
}

pub fn decimal<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
parser
.lexer
.expect(TokenKind::DecimalType, "expected a decimal type")?;

Ok(Type::Decimal)
}

pub fn string<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
parser
.lexer
.expect(TokenKind::StringType, "expected a string type")?;

Ok(Type::String)
}

pub fn character<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
parser
.lexer
.expect(TokenKind::CharacterType, "expected a character type")?;

Ok(Type::Character)
}

pub fn collection<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
Expand Down

0 comments on commit 8f2aa93

Please sign in to comment.