Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Lucas de Jong committed Oct 30, 2024
1 parent 4926fa7 commit 05a05c0
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ impl<'de> Iterator for Lexer<'de> {
"true" => Ok((TokenKind::Boolean, TokenValue::Boolean(true))),
"false" => Ok((TokenKind::Boolean, TokenValue::Boolean(false))),
"let" => Ok((TokenKind::Let, TokenValue::None)),
"struct" => Ok((TokenKind::Struct, TokenValue::None)),
"enum" => Ok((TokenKind::Enum, TokenValue::None)),
"trait" => Ok((TokenKind::Trait, TokenValue::None)),
ident => Ok((
TokenKind::Identifier,
TokenValue::Identifier(ident.to_string().into()),
Expand Down
7 changes: 5 additions & 2 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ pub enum TokenKind {
Struct,
/// A enum keyword; `enum`.
Enum,
/// A trait keyword; `trait`.
Trait,
}

impl Display for TokenKind {
Expand Down Expand Up @@ -216,8 +218,9 @@ impl Display for TokenKind {
TokenKind::Question => write!(f, "`?`"),
TokenKind::Pipe => write!(f, "`|`"),
TokenKind::Caret => write!(f, "`^`"),
TokenKind::And => todo!("`&&`"),
TokenKind::Or => todo!("`||`"),
TokenKind::And => write!(f, "`&&`"),
TokenKind::Or => write!(f, "`||`"),
TokenKind::Trait => write!(f, "`trait`"),
}
}
}
40 changes: 25 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@ pub mod lexer;
pub mod parser;

fn main() {
let input = "let value = a == { let b = 1 if true else 2 };";
let input = "
let x = {1};
let a = \"hello\";
let b = 123;
let b = b if b <= 100 else 100;
struct Human: name, age;
enum Color: red, green, blue, orange;
fn add(a, b) { a + b }
fn sub(a, b) { a - b }
fn mul(a, b) { a * b }
fn div(a, b) { a / b }
trait Add:
fn add(a, b);
";

miette::set_hook(Box::new(|_| {
Box::new(
Expand Down Expand Up @@ -56,32 +72,26 @@ impl miette::highlighters::HighlighterState for SomHighlighterState {
let style: Style = match &token {
Ok(token) => match &token.kind {
// Comment / quote -> 92, 99, 112 + italic
TokenKind::If | TokenKind::Else | TokenKind::Let => {
Style::new().fg_rgb::<197, 120, 221>()
}
TokenKind::If
| TokenKind::Else
| TokenKind::Let
| TokenKind::Struct
| TokenKind::Enum
| TokenKind::Function | TokenKind::Trait => Style::new().fg_rgb::<197, 120, 221>(),
TokenKind::Identifier => Style::new().fg_rgb::<224, 108, 117>(),
TokenKind::String => Style::new().fg_rgb::<152, 195, 121>().italic(),
TokenKind::Integer | TokenKind::Decimal => {
Style::new().fg_rgb::<209, 154, 102>()
}
TokenKind::Boolean => Style::new().fg_rgb::<86, 156, 214>(),
TokenKind::CurlyOpen
| TokenKind::CurlyClose
| TokenKind::ParenOpen
| TokenKind::ParenClose
| TokenKind::SquareOpen
| TokenKind::SquareClose
| TokenKind::Equal

TokenKind::Equal
| TokenKind::LessThan
| TokenKind::GreaterThan
| TokenKind::LessThanOrEqual
| TokenKind::GreaterThanOrEqual
| TokenKind::Equality
| TokenKind::Inequality
| TokenKind::Plus
| TokenKind::Minus
| TokenKind::Star
| TokenKind::Slash
| TokenKind::Percent
| TokenKind::Not
| TokenKind::And
Expand Down
17 changes: 17 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ pub enum Statement<'de> {
Block(Vec<Statement<'de>>),
Expression(Expression<'de>),
Assignment(Cow<'de, str>, Expression<'de>),
Struct {
name: Cow<'de, str>,
fields: Vec<Cow<'de, str>>,
},
Enum {
name: Cow<'de, str>,
variants: Vec<Cow<'de, str>>,
},
Function {
name: Cow<'de, str>,
parameters: Vec<Cow<'de, str>>,
body: Expression<'de>,
},
Trait {
name: Cow<'de, str>,
functions: Vec<(Cow<'de, str>, Vec<Cow<'de, str>>)>,
},
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ impl<'de> Default for Lookup<'de> {
expression::binary::or,
)
.add_statement_handler(TokenKind::Let, statement::let_)
.add_statement_handler(TokenKind::Struct, statement::struct_)
.add_statement_handler(TokenKind::Enum, statement::enum_)
.add_statement_handler(TokenKind::Function, statement::function_)
.add_statement_handler(TokenKind::Trait, statement::trait_)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'de> Parser<'de> {
let mut statements = vec![];

while self.lexer.peek().is_some() {
statements.push(statement::parse(self, false).wrap_err("while parsing main block")?);
statements.push(statement::parse(self, false)?);
}

Ok(Symbol::Statement(Statement::Block(statements)))
Expand Down
Loading

0 comments on commit 05a05c0

Please sign in to comment.