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

Dyro PoC: AST, ANF conversion and Interpreter #2

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[workspace]
resolver = "2"
members = ["crates/*"]
members = ["crates/*"]

[workspace.dependencies]
anyhow = "1.0.86"
im = "15.1.0"
expect-test = "1.5.0"
14 changes: 14 additions & 0 deletions crates/dyro-poc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "dyro-poc"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow.workspace = true
im.workspace = true

[dev-dependencies]
expect-test.workspace = true

[features]
debug-ast-to-anf = []
99 changes: 99 additions & 0 deletions crates/dyro-poc/src/anf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#![allow(dead_code)]

use crate::ast;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ANFVar(pub u32);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ANFType(pub ast::ASTType);

impl ANFType {
pub fn size(&self) -> usize {
use ast::ASTType::*;

match self.0 {
// Don't want to consider ZST now
Unit => 1,
Int => 4,
Bool => 1,
Tuple(ref types) => types.iter().map(|t| ANFType(t.clone()).size()).sum(),

// [heap/stack enum, size, value]
MutPtr(_) => 9,

// We probably can't store these types in memory so 0
String => 0,
Function(_, _) => 0,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ANFUnaryOp(pub ast::ASTUnaryOp);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ANFBinaryOp(pub ast::ASTBinaryOp);

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ANFNode {
Let {
binding: ANFVar,
value: ANFSimpleExpression,
body: Box<ANFNode>,
},
LetFn {
name: ANFVar,
args: Vec<(ANFVar, ANFType)>,
return_type: ANFType,
value: Box<ANFNode>,
body: Box<ANFNode>,
},
Simple(ANFSimpleExpression),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ANFSimpleExpression {
Unit,
Int(i32),
Bool(bool),
Var(ANFVar),
String(String),
SpecialFunction(crate::special::SpecialFunction),
If {
condition: ANFVar,
// We need to use ANFSimpleExpression here to support function call (it could be a continuation here?)
then: Box<ANFSimpleExpression>,
r#else: Box<ANFSimpleExpression>,
},
Call {
function: ANFVar,
type_arguments: Vec<ANFType>,
arguments: Vec<ANFVar>,
},
TupleAccess {
tuple: ANFVar,
index: usize,
},
Tuple {
elements: Vec<ANFVar>,
},
ArrayRead {
array: ANFVar,
index: ANFVar,
},
ArraySet {
array: ANFVar,
index: ANFVar,
value: ANFVar,
},
UnaryOp {
op: ANFUnaryOp,
operand: ANFVar,
},
BinaryOp {
op: ANFBinaryOp,
left: ANFVar,
right: ANFVar,
},
}
108 changes: 108 additions & 0 deletions crates/dyro-poc/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#![allow(dead_code)]

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ASTVar(pub String);

impl From<&str> for ASTVar {
fn from(s: &str) -> Self {
Self(s.to_owned())
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ASTType {
Unit,
Int,
Bool,
String,
MutPtr(Box<ASTType>),
Tuple(Vec<ASTType>),
Function(Vec<ASTType>, Box<ASTType>),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ASTUnaryOp {
Neg,
Not,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ASTBinaryOp {
Add,
Sub,
Mul,
Div,
Eq,
Lt,
Gt,
Le,
Ge,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ASTNode {
Let {
binding: ASTVar,
value: Box<ASTNode>,
body: Box<ASTNode>,
},
LetFn {
name: ASTVar,
args: Vec<(ASTVar, ASTType)>,
return_type: ASTType,
value: Box<ASTNode>,
body: Box<ASTNode>,
},
Unit,
Int(i32),
Bool(bool),
Var(ASTVar),
/// For panic call only
String(String),
SpecialFunction(crate::special::SpecialFunction),
If {
condition: Box<ASTNode>,
then: Box<ASTNode>,
r#else: Box<ASTNode>,
r#type: ASTType,
},
/// Partial evaluation is not supported
Call {
function: Box<ASTNode>,
type_arguments: Vec<ASTType>,
arguments: Vec<ASTNode>,
},
TupleAccess {
tuple: Box<ASTNode>,
index: usize,
},
Tuple {
elements: Vec<ASTNode>,
},
/// array[index]
ArrayRead {
array: Box<ASTNode>,
index: Box<ASTNode>,
},
/// array[index] = value
ArraySet {
array: Box<ASTNode>,
index: Box<ASTNode>,
value: Box<ASTNode>,
},
/// (first; second) equivalent to let _ = first in second
/// removed in ANF
Sequence {
first: Box<ASTNode>,
second: Box<ASTNode>,
},
UnaryOp {
op: ASTUnaryOp,
operand: Box<ASTNode>,
},
BinaryOp {
op: ASTBinaryOp,
left: Box<ASTNode>,
right: Box<ASTNode>,
},
}
Loading