Skip to content

Commit

Permalink
improve parser for expressions, removing a previous limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Jan 9, 2024
1 parent 529acb8 commit 9d83e20
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 156 deletions.
95 changes: 90 additions & 5 deletions crates/concrete_ast/src/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,112 @@
use crate::{operations::Operation, statements::Statement};
use crate::{common::Ident, statements::Statement, types::TypeSpec};

#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
Simple(SimpleExpr),
FnCall(FnCallOp),
Match(MatchExpr),
If(IfExpr),
Operation(Operation),
UnaryOp(UnaryOp, Box<Self>),
BinaryOp(Box<Self>, BinaryOp, Box<Self>),
}

// needed for match variants and array accesses
#[derive(Clone, Debug, PartialEq)]
pub enum SimpleExpr {
ConstBool(bool),
ConstChar(char),
ConstInt(u64),
ConstFloat(f64),
ConstStr(String),
Path(PathOp),
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum UnaryOp {
ArithNeg,
LogicalNot,
BitwiseNot,
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum BinaryOp {
Arith(ArithOp),
Logic(LogicOp),
Compare(CmpOp),
Bitwise(BitwiseOp),
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum ArithOp {
Add,
Sub,
Mul,
Div,
Mod,
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum LogicOp {
And,
Or,
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum CmpOp {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
}

#[derive(Clone, Debug, PartialEq, Copy)]
pub enum BitwiseOp {
And,
Or,
Xor,
}

#[derive(Clone, Debug, PartialEq)]
pub struct MatchExpr {
pub value: Operation,
pub value: Box<Expression>,
pub variants: Vec<MatchVariant>,
}

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

#[derive(Clone, Debug, PartialEq)]
pub struct MatchVariant {
pub case: Operation,
pub case: SimpleExpr,
pub block: Vec<Statement>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum PathSegment {
FieldAccess(Ident),
ArrayIndex(SimpleExpr),
}

#[derive(Clone, Debug, PartialEq)]
pub struct PathOp {
pub first: Ident,
pub extra: Vec<PathSegment>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct CastOp {
pub value: Expression,
pub r#type: TypeSpec,
}

#[derive(Clone, Debug, PartialEq)]
pub struct FnCallOp {
pub target: Ident,
pub args: Vec<Expression>,
}
1 change: 0 additions & 1 deletion crates/concrete_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod expressions;
pub mod functions;
pub mod imports;
pub mod modules;
pub mod operations;
pub mod statements;
pub mod structs;
pub mod types;
Expand Down
78 changes: 0 additions & 78 deletions crates/concrete_ast/src/operations.rs

This file was deleted.

9 changes: 4 additions & 5 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
common::Ident,
expressions::{Expression, IfExpr, MatchExpr},
operations::{FnCallOp, Operation, PathOp},
expressions::{Expression, FnCallOp, IfExpr, MatchExpr, PathOp},
types::TypeSpec,
};

Expand Down Expand Up @@ -51,13 +50,13 @@ pub struct Binding {
#[derive(Clone, Debug, PartialEq)]
pub struct ForStmt {
pub name: Ident,
pub from: Operation,
pub to: Operation,
pub from: Expression,
pub to: Expression,
pub contents: Vec<Statement>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct WhileStmt {
pub value: Operation,
pub value: Expression,
pub contents: Vec<Statement>,
}
Loading

0 comments on commit 9d83e20

Please sign in to comment.