-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from lambdaclass/ast_edgl
initial ast and parser structure skeleton
- Loading branch information
Showing
21 changed files
with
828 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
} | ||
|
||
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.