Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Constants #22

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lazy_static! {

terminal!(m, Eq, "=");
terminal!(m, Let, "let");
terminal!(m, Const, "const");
terminal!(m, Mut, "mut");
terminal!(m, Semicolon, ";");
terminal!(m, Plus, "+");
Expand Down
4 changes: 4 additions & 0 deletions src/lexer/token_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub enum TokenKind {
position: Position,
},
#[terminal]
Const {
position: Position,
},
#[terminal]
Mut {
position: Position,
},
Expand Down
1 change: 1 addition & 0 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum AstNode {
Num(Num),
Statement(Statement),
Initialization(Initialisation),
Constant(Constant),
Assignment(Assignment),
Function(Function),
Lambda(Lambda),
Expand Down
80 changes: 80 additions & 0 deletions src/parser/ast/statement/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{
lexer::{TokenKind, Tokens},
parser::{
ast::{AstNode, Expression, Id, TypeName},
combinators::Comb,
FromTokens, ParseError,
},
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Constant {
pub id: Id,
pub type_name: TypeName,
pub value: Expression,
}

impl FromTokens<TokenKind> for Constant {
fn parse(tokens: &mut Tokens<TokenKind>) -> Result<AstNode, ParseError>
where
Self: Sized,
{
Comb::CONST_KEYWORD.parse(tokens)?;

let matcher = Comb::ID >> Comb::COLON >> Comb::TYPE_NAME >> Comb::EQ >> Comb::EXPR;

let result = matcher.parse(tokens)?;

let Some(AstNode::Id(id)) = result.get(0) else {
unreachable!()
};

let Some(AstNode::TypeName(type_name)) = result.get(1).cloned() else {
unreachable!()
};

let Some(AstNode::Expression(value)) = result.get(2).cloned() else {
unreachable!()
};

Ok(Constant {
id: id.clone(),
value: value.clone(),
type_name,
}
.into())
}
}

impl From<Constant> for AstNode {
fn from(value: Constant) -> Self {
AstNode::Constant(value)
}
}

#[cfg(test)]
mod tests {
use crate::{lexer::Lexer, parser::ast::Num};

use super::*;

#[test]
fn test_simple_constant() {
let mut tokens = Lexer::new("const foo: i32 = 42")
.lex()
.expect("should work")
.into();

let result = Constant::parse(&mut tokens);

assert_eq!(
Ok(Constant {
id: Id("foo".into()),
type_name: TypeName::Literal("i32".into()),
value: Expression::Num(Num(42))
}
.into()),
result
)
}
}
34 changes: 33 additions & 1 deletion src/parser/ast/statement/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod assignment;
mod constant;
mod declaration;
mod initialisation;
mod while_loop;

pub use self::assignment::*;
pub use self::constant::*;
pub use self::declaration::*;
pub use self::initialisation::*;
pub use self::while_loop::*;
Expand All @@ -21,6 +23,7 @@ pub enum Statement {
If(If),
WhileLoop(WhileLoop),
Initialization(Initialisation),
Constant(Constant),
Assignment(Assignment),
Expression(Expression),
YieldingExpression(Expression),
Expand Down Expand Up @@ -75,6 +78,15 @@ impl FromTokens<TokenKind> for Statement {
};
Ok(Statement::Initialization(init.clone()).into())
}
TokenKind::Const { .. } => {
let matcher = Comb::CONSTANT >> Comb::SEMI;
let result = matcher.parse(tokens)?;

let [AstNode::Constant(constant)] = result.as_slice() else {
unreachable!()
};
Ok(Statement::Constant(constant.clone()).into())
}
TokenKind::ReturnKeyword { .. } => {
let matcher = Comb::RETURN_KEYWORD >> Comb::EXPR >> Comb::SEMI;
let result = matcher.parse(tokens)?;
Expand Down Expand Up @@ -164,11 +176,31 @@ impl From<Statement> for AstNode {
mod tests {
use crate::{
lexer::Lexer,
parser::ast::{Id, Num},
parser::ast::{Id, Num, TypeName},
};

use super::*;

#[test]
fn test_basic_constant() {
let mut tokens = Lexer::new("const foo: i32 = 42;")
.lex()
.expect("should work")
.into();

let result = Statement::parse(&mut tokens);

assert_eq!(
Ok(Statement::Constant(Constant {
id: Id("foo".into()),
type_name: TypeName::Literal("i32".into()),
value: Expression::Num(Num(42))
})
.into()),
result
)
}

#[test]
fn test_basic_return() {
let mut tokens = Lexer::new("return 42;").lex().expect("should work").into();
Expand Down
6 changes: 5 additions & 1 deletion src/parser/combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::lexer::{Terminal, TokenKind, Tokens};

use super::{
ast::{
Array, Assignment, AstNode, Block, Declaration, Expression, Function, Id, If,
Array, Assignment, AstNode, Block, Constant, Declaration, Expression, Function, Id, If,
Initialisation, Lambda, Num, Parameter, Statement, TypeName, WhileLoop,
},
FromTokens, ParseError,
Expand Down Expand Up @@ -147,6 +147,8 @@ macro_rules! node_comb {
impl<'a> Comb<'a, TokenKind, Terminal, AstNode> {
terminal_comb!(LET, Let);

terminal_comb!(CONST_KEYWORD, Const);

terminal_comb!(MUT, Mut);

terminal_comb!(EQ, Eq);
Expand Down Expand Up @@ -218,6 +220,8 @@ impl<'a> Comb<'a, TokenKind, Terminal, AstNode> {
node_comb!(TYPE_NAME, TypeName);

node_comb!(DECLARATION, Declaration);

node_comb!(CONSTANT, Constant);
}

impl<'a, Tok, Term, Node> Comb<'a, Tok, Term, Node>
Expand Down
Loading