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

initial ast and parser structure skeleton #50

Merged
merged 13 commits into from
Jan 5, 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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"

members = [ "crates/concrete","crates/concrete_ast", "crates/concrete_codegen_mlir", "crates/concrete_driver", "crates/concrete_parser"]
members = [ "crates/concrete","crates/concrete_ast", "crates/concrete_codegen_mlir", "crates/concrete_driver", "crates/concrete_parser", "crates/concrete_type_checker"]
unbalancedparentheses marked this conversation as resolved.
Show resolved Hide resolved

[profile.release]
lto = true
Expand Down
28 changes: 28 additions & 0 deletions crates/concrete_ast/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span {
pub from: usize,
pub to: usize,
}

impl Span {
pub fn new(from: usize, to: usize) -> Self {
Self { from, to }
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct DocString {
contents: String,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Ident {
pub name: String,
pub span: Span,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct GenericParam {
pub name: Ident,
pub params: Vec<Ident>,
}
18 changes: 18 additions & 0 deletions crates/concrete_ast/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{
common::{DocString, Ident},
expressions::Expression,
types::TypeSpec,
};

#[derive(Clone, Debug, PartialEq)]
pub struct ConstantDecl {
pub doc_string: Option<DocString>,
pub name: Ident,
pub r#type: TypeSpec,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ConstantDef {
pub decl: ConstantDecl,
pub value: Expression,
}
24 changes: 24 additions & 0 deletions crates/concrete_ast/src/expressions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{operations::Operation, statements::Statement};

#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
Match(MatchExpr),
Operation(Operation),
// Block(Vec<Statement>),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be commented

}

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

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

#[derive(Clone, Debug, PartialEq)]
pub struct MatchVariant {
pub case: Operation,
pub block: Vec<Statement>,
}
26 changes: 26 additions & 0 deletions crates/concrete_ast/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{
common::{DocString, GenericParam, Ident},
statements::Statement,
types::TypeSpec,
};

#[derive(Clone, Debug, PartialEq)]
pub struct FunctionDecl {
pub doc_string: Option<DocString>,
pub generic_params: Vec<GenericParam>,
pub name: Ident,
pub params: Vec<Param>,
pub ret_type: Option<TypeSpec>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct FunctionDef {
pub decl: FunctionDecl,
pub body: Vec<Statement>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Param {
pub name: Ident,
pub r#type: TypeSpec,
}
13 changes: 13 additions & 0 deletions crates/concrete_ast/src/imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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>,
}
24 changes: 10 additions & 14 deletions crates/concrete_ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
pub mod common;
pub mod constants;
pub mod expressions;
pub mod functions;
pub mod imports;
pub mod modules;
pub mod operations;
pub mod statements;
pub mod structs;
pub mod types;
24 changes: 24 additions & 0 deletions crates/concrete_ast/src/modules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{
common::{DocString, Ident},
constants::ConstantDef,
functions::FunctionDef,
imports::ImportStmt,
structs::StructDecl,
types::TypeDecl,
};

#[derive(Clone, Debug, PartialEq)]
pub struct ModuleBase {
pub doc_string: Option<DocString>,
pub imports: Vec<ImportStmt>,
pub name: Ident,
pub contents: Vec<ModuleDefItem>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ModuleDefItem {
Constant(ConstantDef),
Function(FunctionDef),
Record(StructDecl),
Type(TypeDecl),
}
78 changes: 78 additions & 0 deletions crates/concrete_ast/src/operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::{common::Ident, types::TypeSpec};

#[derive(Clone, Debug, PartialEq)]
pub enum PathSegment {
FieldAccess(Ident),
ArrayIndex(Box<Operation>),
}

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

#[derive(Clone, Debug, PartialEq)]
pub enum Operation {
Compound(CompoundOp),
Atomic(AtomicOp),
}

#[derive(Clone, Debug, PartialEq)]
pub enum CompoundOp {
Compare(CmpOp),
Logic(LogicOp),
Arith(ArithOp),
Cast(CastOp),
}

#[derive(Clone, Debug, PartialEq)]
pub enum AtomicOp {
ConstBool(bool),
ConstChar(char),
ConstInt(u64),
ConstFloat(f64),
ConstStr(String),
FnCall(FnCallOp),
Path(PathOp),
Paren(Box<Operation>),
}

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

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

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

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

#[derive(Clone, Debug, PartialEq)]
pub struct FnCallOp {
pub target: Ident,
pub args: Vec<Operation>,
}
69 changes: 69 additions & 0 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{
common::Ident,
expressions::{Expression, MatchExpr},
operations::{Operation, PathOp},
types::TypeSpec,
};

#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Assign(AssignStmt),
Match(MatchExpr),
For(ForStmt),
If(IfStmt),
Let(LetStmt),
Return(ReturnStmt),
While(WhileStmt),
}

#[derive(Clone, Debug, PartialEq)]
pub enum LetStmtTarget {
Simple { name: Ident, r#type: TypeSpec },
Destructure(Vec<Binding>),
}

#[derive(Clone, Debug, PartialEq)]
pub struct LetStmt {
pub is_mutable: bool,
pub target: LetStmtTarget,
pub value: Expression,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ReturnStmt {
pub value: Expression,
}

#[derive(Clone, Debug, PartialEq)]
pub struct AssignStmt {
pub target: PathOp,
pub value: Expression,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Binding {
pub name: Ident,
pub rename: Option<Ident>,
pub r#type: TypeSpec,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ForStmt {
pub name: Ident,
pub from: Operation,
pub to: Operation,
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,
pub contents: Vec<Statement>,
}
19 changes: 19 additions & 0 deletions crates/concrete_ast/src/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{
common::{DocString, GenericParam, Ident},
types::TypeSpec,
};

#[derive(Clone, Debug, PartialEq)]
pub struct StructDecl {
doc_string: Option<DocString>,
name: Ident,
type_params: Vec<GenericParam>,
fields: Vec<Field>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Field {
doc_string: Option<DocString>,
name: Ident,
r#type: TypeSpec,
}
18 changes: 18 additions & 0 deletions crates/concrete_ast/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::common::{DocString, Ident};

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum TypeSpec {
Simple {
name: Ident,
},
Generic {
name: Ident,
type_params: Vec<TypeSpec>,
},
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct TypeDecl {
pub doc_string: Option<DocString>,
pub name: Ident,
}
Loading