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 6a9f3bc commit e0ad300
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
fib(9999);
}
fn fib(n ~ number) ~ number {
fn fib(n ~ number) ~ [number] {
return if n < 2;
fib(n - 1) + fib(n - 2)
}
Expand Down
4 changes: 3 additions & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub enum UnaryOperator {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type<'de> {
Unit,
Symbol(Cow<'de, str>),
Array(Box<Type<'de>>),
Collection(Box<Type<'de>>),
Set(Box<Type<'de>>),
}
3 changes: 3 additions & 0 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ 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::SquareOpen, typing::collection)
.add_type_handler(TokenKind::CurlyOpen, typing::set)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/parser/statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::lexer::{self, TokenKind, TokenValue};

use super::{
ast::{
EnumMemberDeclaration, FunctionHeader, ParameterDeclaration, Statement,
Expand All @@ -9,6 +7,7 @@ use super::{
lookup::BindingPower,
typing, Parser,
};
use crate::lexer::{TokenKind, TokenValue};
use miette::{Context, Result};

pub fn parse<'de>(parser: &mut Parser<'de>, optional_semicolon: bool) -> Result<Statement<'de>> {
Expand Down
40 changes: 38 additions & 2 deletions src/parser/typing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::lexer::{TokenKind, TokenValue};

use super::{ast::Type, lookup::BindingPower, Parser};
use crate::lexer::{TokenKind, TokenValue};
use miette::Result;

pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Result<Type<'de>> {
Expand Down Expand Up @@ -61,6 +60,43 @@ pub fn parse<'de>(parser: &mut Parser<'de>, binding_power: BindingPower) -> Resu
Ok(lhs)
}

pub fn unit<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
parser
.lexer
.expect(TokenKind::ParenOpen, "expected an opening parenthesis")?;
parser
.lexer
.expect(TokenKind::ParenClose, "expected a closing parenthesis")?;

Ok(Type::Unit)
}

pub fn collection<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
parser
.lexer
.expect(TokenKind::SquareOpen, "expected an opening bracket")?;
let element = parse(parser, BindingPower::None)?;
parser
.lexer
.expect(TokenKind::SquareClose, "expected a closing bracket")?;

Ok(Type::Collection(Box::new(element)))
}

pub fn set<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
parser
.lexer
.expect(TokenKind::CurlyOpen, "expected an opening curly brace")?;

let element = parse(parser, BindingPower::None)?;

parser
.lexer
.expect(TokenKind::CurlyClose, "expected a closing curly brace")?;

Ok(Type::Set(Box::new(element)))
}

pub fn identifier<'de>(parser: &mut Parser<'de>) -> Result<Type<'de>> {
let token = parser
.lexer
Expand Down

0 comments on commit e0ad300

Please sign in to comment.