Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 8, 2024
1 parent 8a28687 commit f438e47
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
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
11 changes: 2 additions & 9 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
common::Ident,
expressions::{Expression, MatchExpr},
expressions::{Expression, MatchExpr, IfExpr},
operations::{Operation, PathOp},
types::TypeSpec,
};
Expand All @@ -10,7 +10,7 @@ pub enum Statement {
Assign(AssignStmt),
Match(MatchExpr),
For(ForStmt),
If(IfStmt),
If(IfExpr),
Let(LetStmt),
Return(ReturnStmt),
While(WhileStmt),
Expand Down Expand Up @@ -55,13 +55,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
32 changes: 32 additions & 0 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ extern {
"," => Token::Coma,
"<" => Token::LessThanSign,
">" => Token::MoreThanSign,
">=" => Token::MoreThanEqSign,
"<=" => Token::LessThanEqSign,
"." => Token::Dot,

// operators
Expand Down Expand Up @@ -230,9 +232,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:SemiColonSeparated<Statement>> "}"
<else_stmts:("else" "{" <SemiColonSeparated<Statement>> "}")?> => {
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 Down Expand Up @@ -276,6 +290,8 @@ pub(crate) AtomicOp: ast::operations::AtomicOp = {

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 @@ -290,6 +306,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 @@ -321,6 +352,7 @@ pub(crate) FnCallOp: ast::operations::FnCallOp = {

pub(crate) Statement: ast::statements::Statement = {
<MatchExpr> => ast::statements::Statement::Match(<>),
<IfExpr> => ast::statements::Statement::If(<>),
<LetStmt> => ast::statements::Statement::Let(<>),
<AssignStmt> => ast::statements::Statement::Assign(<>),
<ReturnStmt> => ast::statements::Statement::Return(<>),
Expand Down
6 changes: 6 additions & 0 deletions crates/concrete_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ mod ModuleName {
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;
}
return x;
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/concrete_parser/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub enum Token {
LessThanSign,
#[token(">")]
MoreThanSign,
#[token(">=")]
MoreThanEqSign,
#[token("<=")]
LessThanEqSign,

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

0 comments on commit f438e47

Please sign in to comment.