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

Parser work #61

Merged
merged 5 commits into from
Jan 8, 2024
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
13 changes: 8 additions & 5 deletions crates/concrete_ast/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ use crate::{operations::Operation, statements::Statement};
#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
Match(MatchExpr),
If(IfExpr),
Operation(Operation),
// Block(Vec<Statement>),
}

pub struct BlockExpr {
pub statements: Vec<Statement>,
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -17,6 +13,13 @@ pub struct MatchExpr {
pub variants: Vec<MatchVariant>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct IfExpr {
pub value: Operation,
pub contents: Vec<Statement>,
pub r#else: Option<Vec<Statement>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct MatchVariant {
pub case: Operation,
Expand Down
10 changes: 2 additions & 8 deletions crates/concrete_ast/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ use crate::common::Ident;

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ImportStmt {
module: Vec<Ident>,
symbols: Vec<ImportedSymbol>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ImportedSymbol {
import_name: Ident,
rename_into: Option<Ident>,
pub module: Vec<Ident>,
pub symbols: Vec<Ident>,
}
14 changes: 4 additions & 10 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::Ident,
expressions::{Expression, MatchExpr},
operations::{Operation, PathOp},
expressions::{Expression, IfExpr, MatchExpr},
operations::{FnCallOp, Operation, PathOp},
types::TypeSpec,
};

Expand All @@ -10,10 +10,11 @@ pub enum Statement {
Assign(AssignStmt),
Match(MatchExpr),
For(ForStmt),
If(IfStmt),
If(IfExpr),
Let(LetStmt),
Return(ReturnStmt),
While(WhileStmt),
FnCall(FnCallOp),
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -55,13 +56,6 @@ pub struct ForStmt {
pub contents: Vec<Statement>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct IfStmt {
pub value: Operation,
pub contents: Vec<Statement>,
pub r#else: Option<Vec<Statement>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct WhileStmt {
pub value: Operation,
Expand Down
119 changes: 111 additions & 8 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern {
"match" => Token::KeywordMatch,
"mod" => Token::KeywordMod,
"pub" => Token::KeywordPub,
"mut" => Token::KeywordMut,
"import" => Token::KeywordImport,

// literals
"identifier" => Token::Identifier(<String>),
Expand All @@ -45,6 +47,8 @@ extern {
"," => Token::Coma,
"<" => Token::LessThanSign,
">" => Token::MoreThanSign,
">=" => Token::MoreThanEqSign,
"<=" => Token::LessThanEqSign,
"." => Token::Dot,

// operators
Expand All @@ -63,6 +67,16 @@ extern {

// lalrpop macros

Dot<T>: Vec<T> = {
<mut v:(<T> ".")*> <e:T?> => match e {
None => v,
Some(e) => {
v.push(e);
v
}
}
};

Comma<T>: Vec<T> = {
<mut v:(<T> ",")*> <e:T?> => match e {
None => v,
Expand Down Expand Up @@ -153,16 +167,35 @@ pub Program: ast::Program = {
// Modules

pub(crate) Module: ast::modules::Module = {
"mod" <name:Ident> "{" <contents:ModuleItems> "}" => {
"mod" <name:Ident> "{" <imports:ImportList?> <contents:ModuleItems> "}" => {
ast::modules::Module {
doc_string: None,
imports: vec![], // todo: add imports
imports: imports.unwrap_or_else(Vec::new),
name,
contents
}
}
}

pub(crate) ImportList: Vec<ast::imports::ImportStmt> = {
<ImportStmt> => vec![<>],
<mut s:ImportList> <n:ImportStmt> => {
s.push(n);
s
},
}


pub(crate) ImportStmt: ast::imports::ImportStmt = {
"import" <module:Dot<Ident>> "{" <symbols:Comma<Ident>> "}" ";" => {
ast::imports::ImportStmt {
module,
symbols,
}
}
}


pub(crate) ModuleItems: Vec<ast::modules::ModuleDefItem> = {
<ModuleDefItem> => vec![<>],
<mut s:ModuleItems> <n:ModuleDefItem> => {
Expand Down Expand Up @@ -210,7 +243,7 @@ pub(crate) Param: ast::functions::Param = {

pub(crate) FunctionDef: ast::functions::FunctionDef = {
<is_pub:"pub"?> "fn" <name:Ident> <generic_params:GenericParams?> "(" <params:Comma<Param>> ")" <ret_type:FunctionRetType?> "{"
<statements:SemiColonSeparated<Statement>>
<statements:StatementList>
"}" => {
ast::functions::FunctionDef {
decl: ast::functions::FunctionDecl {
Expand All @@ -229,9 +262,21 @@ pub(crate) FunctionDef: ast::functions::FunctionDef = {

pub(crate) Expression: ast::expressions::Expression = {
<MatchExpr> => ast::expressions::Expression::Match(<>),
<IfExpr> => ast::expressions::Expression::If(<>),
<Operation> => ast::expressions::Expression::Operation(<>),
}

pub(crate) IfExpr: ast::expressions::IfExpr = {
"if" <value:Operation> "{" <contents:StatementList> "}"
<else_stmts:("else" "{" <StatementList> "}")?> => {
ast::expressions::IfExpr {
value,
contents,
r#else: else_stmts,
}
}
}

pub(crate) MatchExpr: ast::expressions::MatchExpr = {
"match" <value:Operation> "{" <variants:Comma<MatchVariant>> "}" => {
ast::expressions::MatchExpr {
Expand All @@ -250,7 +295,7 @@ pub(crate) MatchVariant: ast::expressions::MatchVariant = {
}
},
// x -> { ... }
<case:Operation> "->" "{" <stmts:SemiColonSeparated<Statement>> "}" => {
<case:Operation> "->" "{" <stmts:StatementList> "}" => {
ast::expressions::MatchVariant {
case,
block: stmts
Expand All @@ -269,12 +314,15 @@ pub(crate) AtomicOp: ast::operations::AtomicOp = {
<"integer"> => ast::operations::AtomicOp::ConstInt(<>),
<"boolean"> => ast::operations::AtomicOp::ConstBool(<>),
<"string"> => ast::operations::AtomicOp::ConstStr(<>),
<PathOp> =>ast::operations::AtomicOp::Path(<>),
<FnCallOp> =>ast::operations::AtomicOp::FnCall(<>),
<PathOp> => ast::operations::AtomicOp::Path(<>),
<FnCallOp> => ast::operations::AtomicOp::FnCall(<>),
"(" <op:Operation> ")" => ast::operations::AtomicOp::Paren(Box::new(op)),
}

pub(crate) CompoundOp: ast::operations::CompoundOp = {
<ArithOp> => ast::operations::CompoundOp::Arith(<>),
<CmpOp> => ast::operations::CompoundOp::Compare(<>),
<LogicOp> => ast::operations::CompoundOp::Logic(<>),
}

pub(crate) ArithOp: ast::operations::ArithOp = {
Expand All @@ -289,6 +337,21 @@ pub(crate) ArithOp: ast::operations::ArithOp = {
"-" <value:AtomicOp> => ast::operations::ArithOp::Neg(value),
}

pub(crate) CmpOp: ast::operations::CmpOp = {
<lhs:AtomicOp> "==" <rhs:AtomicOp> => ast::operations::CmpOp::Eq(lhs, rhs),
<lhs:AtomicOp> "!=" <rhs:AtomicOp> => ast::operations::CmpOp::NotEq(lhs, rhs),
<lhs:AtomicOp> "<=" <rhs:AtomicOp> => ast::operations::CmpOp::LtEq(lhs, rhs),
<lhs:AtomicOp> ">=" <rhs:AtomicOp> => ast::operations::CmpOp::GtEq(lhs, rhs),
<lhs:AtomicOp> ">" <rhs:AtomicOp> => ast::operations::CmpOp::Lt(lhs, rhs),
<lhs:AtomicOp> "<" <rhs:AtomicOp> => ast::operations::CmpOp::Gt(lhs, rhs),
}

pub(crate) LogicOp: ast::operations::LogicOp = {
<lhs:AtomicOp> "&&" <rhs:AtomicOp> => ast::operations::LogicOp::And(lhs, rhs),
<lhs:AtomicOp> "||" <rhs:AtomicOp> => ast::operations::LogicOp::Or(lhs, rhs),
"!" <rhs:AtomicOp> => ast::operations::LogicOp::Not(rhs),
}

pub(crate) PathOp: ast::operations::PathOp = {
<first:Ident> <extra:PathSegments?> => ast::operations::PathOp {
first,
Expand Down Expand Up @@ -318,13 +381,53 @@ pub(crate) FnCallOp: ast::operations::FnCallOp = {

// -- Statements

pub StatementList: Vec<ast::statements::Statement> = {
<Statement> => vec![<>],
<mut s:StatementList> <n:Statement> => {
s.push(n);
s
},
}

pub(crate) Statement: ast::statements::Statement = {
<MatchExpr> ";" => ast::statements::Statement::Match(<>),
<ReturnStmt> => ast::statements::Statement::Return(<>),
<MatchExpr> ";"? => ast::statements::Statement::Match(<>),
<IfExpr> ";"? => ast::statements::Statement::If(<>),
<WhileStmt> ";"? => ast::statements::Statement::While(<>),
<LetStmt> ";" => ast::statements::Statement::Let(<>),
<AssignStmt> ";" => ast::statements::Statement::Assign(<>),
<FnCallOp> ";" => ast::statements::Statement::FnCall(<>),
<ReturnStmt> ";"? => ast::statements::Statement::Return(<>),
}

pub(crate) LetStmt: ast::statements::LetStmt = {
"let" <is_mutable:"mut"?> <name:Ident> ":" <target_type:TypeSpec> "=" <value:Expression> => ast::statements::LetStmt {
is_mutable: is_mutable.is_some(),
target: ast::statements::LetStmtTarget::Simple {
name,
r#type: target_type
},
value
},
}

pub(crate) AssignStmt: ast::statements::AssignStmt = {
<target:PathOp> "=" <value:Expression> => ast::statements::AssignStmt {
target,
value
},
}

pub(crate) ReturnStmt: ast::statements::ReturnStmt = {
"return" <value:Expression> => ast::statements::ReturnStmt {
value,
},
}

pub(crate) WhileStmt: ast::statements::WhileStmt = {
"while" <value:Operation> "{" <contents:StatementList> "}" => {
ast::statements::WhileStmt {
value,
contents,
}
}
}
47 changes: 45 additions & 2 deletions crates/concrete_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,58 @@ mod tests {
fn parse_simple_program() {
let source = r##"
mod ModuleName {
import Std.io.{print};

const MY_CONSTANT: u8 = 2;
const MY_CONSTANT2: bool = true;
const MY_CONSTANT3: string = "hello world!";

pub fn my_func(hello: u64) -> u64 {
let mut x: u64 = hello / 2;
x = x + 4;
x = x - 2;
x = x % 2;

match x {
0 -> return 2,
1 -> {
let y: u64 = x * 2;
return y * 10;
},
}

if x == 2 {
return 0;
}

let lol: u64 = if x == 3 {
return 4;
} else {
return 5;
};

print("hello world\nwith newlines and \" escapes ");
my_func((4 * 2) / 5);

while x > 0 {
x = x - 1;
}

return x;
}
}
"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
let mut ast = parser.parse(lexer).unwrap();
// dbg!(ast);
match parser.parse(lexer) {
Ok(ast) => {
dbg!(ast);
}
Err(e) => {
print_parser_error(source, e);
panic!()
}
}
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions crates/concrete_parser/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pub enum Token {
KeywordMod,
#[token("pub")]
KeywordPub,
#[token("mut")]
KeywordMut,
#[token("import")]
KeywordImport,

// Modern way of allowing identifiers, read: https://unicode.org/reports/tr31/
#[regex(r"[\p{XID_Start}_]\p{XID_Continue}*", |lex| lex.slice().to_string())]
Expand Down Expand Up @@ -89,6 +93,10 @@ pub enum Token {
LessThanSign,
#[token(">")]
MoreThanSign,
#[token(">=")]
MoreThanEqSign,
#[token("<=")]
LessThanEqSign,

#[token("+")]
OperatorAdd,
Expand Down
Loading