Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Protryon committed Jan 14, 2021
1 parent f56d869 commit ba2a7a6
Show file tree
Hide file tree
Showing 34 changed files with 437 additions and 252 deletions.
3 changes: 2 additions & 1 deletion 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 asg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leo-asg"
version = "1.0.0"
version = "1.0.7"
authors = [ "The Aleo Team <hello@aleo.org>" ]
description = "ASG of the Leo programming language"
homepage = "https://aleo.org"
Expand Down
23 changes: 13 additions & 10 deletions asg/src/expression/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
Some(Type::Circuit(circuit)) => circuit,
type_ => return Err(AsgConvertError::unexpected_type("circuit", type_.map(|x| x.to_string()).as_deref(), &span)),
};
let circuit_name = circuit.name.borrow().name.clone();
let member = circuit.members.borrow();
let member = member.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit.name.name, &name.name, &span))?;
let member = member.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?;
match member {
CircuitMember::Function(body) => {
if body.qualifier == FunctionQualifier::Static {
return Err(AsgConvertError::circuit_static_call_invalid(&circuit.name.name, &name.name, &span));
return Err(AsgConvertError::circuit_static_call_invalid(&circuit_name, &name.name, &span));
} else if body.qualifier == FunctionQualifier::MutSelfRef {
if !target.is_mut_ref() {
return Err(AsgConvertError::circuit_member_mut_call_invalid(&circuit.name.name, &name.name, &span));
return Err(AsgConvertError::circuit_member_mut_call_invalid(&circuit_name, &name.name, &span));
}
}
(Some(target), body.clone())
},
CircuitMember::Variable(_) => return Err(AsgConvertError::circuit_variable_call(&circuit.name.name, &name.name, &span))?,
CircuitMember::Variable(_) => return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span))?,
}
},
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression { circuit: ast_circuit, name, span }) => {
Expand All @@ -83,16 +84,18 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
} else {
return Err(AsgConvertError::unexpected_type("circuit", None, &span));
};
let circuit_name = circuit.name.borrow().name.clone();

let member = circuit.members.borrow();
let member = member.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit.name.name, &name.name, &span))?;
let member = member.get(&name.name).ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?;
match member {
CircuitMember::Function(body) => {
if body.qualifier != FunctionQualifier::Static {
return Err(AsgConvertError::circuit_member_call_invalid(&circuit.name.name, &name.name, &span));
return Err(AsgConvertError::circuit_member_call_invalid(&circuit_name, &name.name, &span));
}
(None, body.clone())
},
CircuitMember::Variable(_) => return Err(AsgConvertError::circuit_variable_call(&circuit.name.name, &name.name, &span))?,
CircuitMember::Variable(_) => return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span))?,
}
},
_ => return Err(AsgConvertError::illegal_ast_structure("non Identifier/CircuitMemberAccess/CircuitStaticFunctionAccess as call target")),
Expand Down Expand Up @@ -130,12 +133,12 @@ impl Into<leo_ast::CallExpression> for &CallExpression {
let circuit = self.function.circuit.borrow().as_ref().map(|x| x.upgrade()).flatten();
if let Some(circuit) = circuit {
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
circuit: Box::new(leo_ast::Expression::Identifier(circuit.name.clone())),
name: self.function.name.clone(),
circuit: Box::new(leo_ast::Expression::Identifier(circuit.name.borrow().clone())),
name: self.function.name.borrow().clone(),
span: self.span.clone().unwrap_or_default(),
})
} else {
leo_ast::Expression::Identifier(self.function.name.clone())
leo_ast::Expression::Identifier(self.function.name.borrow().clone())
}
};
leo_ast::CallExpression {
Expand Down
8 changes: 4 additions & 4 deletions asg/src/expression/circuit_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ impl FromAst<leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression
)
);
} else {
return Err(AsgConvertError::input_ref_needs_type(&circuit.name.name, &value.name.name, &value.span));
return Err(AsgConvertError::input_ref_needs_type(&circuit.name.borrow().name, &value.name.name, &value.span));
}
} else {
return Err(AsgConvertError::unresolved_circuit_member(&circuit.name.name, &value.name.name, &value.span));
return Err(AsgConvertError::unresolved_circuit_member(&circuit.name.borrow().name, &value.name.name, &value.span));
}

Ok(CircuitAccessExpression {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl FromAst<leo_ast::CircuitStaticFunctionAccessExpression> for CircuitAccessEx
if let Some(CircuitMember::Function(_)) = circuit.members.borrow().get(&value.name.name) {
// okay
} else {
return Err(AsgConvertError::unresolved_circuit_member(&circuit.name.name, &value.name.name, &value.span));
return Err(AsgConvertError::unresolved_circuit_member(&circuit.name.borrow().name, &value.name.name, &value.span));
}

Ok(CircuitAccessExpression {
Expand All @@ -150,7 +150,7 @@ impl Into<leo_ast::Expression> for &CircuitAccessExpression {
})
} else {
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
circuit: Box::new(leo_ast::Expression::Identifier(self.circuit.name.clone())),
circuit: Box::new(leo_ast::Expression::Identifier(self.circuit.name.borrow().clone())),
name: self.member.clone(),
span: self.span.clone().unwrap_or_default(),
})
Expand Down
8 changes: 4 additions & 4 deletions asg/src/expression/circuit_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
match expected_type {
Some(PartialType::Type(Type::Circuit(expected_circuit))) if expected_circuit == circuit => (),
None => (),
Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&circuit.name.name), &value.span)),
Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some(&circuit.name.borrow().name), &value.span)),
}
let members: IndexMap<&String, (&Identifier, &leo_ast::Expression)> = value.members.iter().map(|x| (&x.identifier.name, (&x.identifier, &x.expression))).collect();

Expand All @@ -69,13 +69,13 @@ impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
let received = Arc::<Expression>::from_ast(scope, *receiver, Some(type_.partial()))?;
values.push(((*identifier).clone(), received));
} else {
return Err(AsgConvertError::missing_circuit_member(&circuit.name.name, name, &value.span));
return Err(AsgConvertError::missing_circuit_member(&circuit.name.borrow().name, name, &value.span));
}
}

for (name, (identifier, _expression)) in members.iter() {
if circuit_members.get(*name).is_none() {
return Err(AsgConvertError::extra_circuit_member(&circuit.name.name, *name, &identifier.span));
return Err(AsgConvertError::extra_circuit_member(&circuit.name.borrow().name, *name, &identifier.span));
}
}
}
Expand All @@ -92,7 +92,7 @@ impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
impl Into<leo_ast::CircuitInitExpression> for &CircuitInitExpression {
fn into(self) -> leo_ast::CircuitInitExpression {
leo_ast::CircuitInitExpression {
name: self.circuit.name.clone(),
name: self.circuit.name.borrow().clone(),
members: self.values.iter().map(|(name, value)| {
leo_ast::CircuitVariableDefinition {
identifier: name.clone(),
Expand Down
3 changes: 2 additions & 1 deletion asg/src/import.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use indexmap::IndexMap;
use crate::{ Program, AsgConvertError };
use std::sync::Arc;

pub trait ImportResolver {
fn resolve_package(&self, package_segments: &[&str]) -> Result<Option<Program>, AsgConvertError>;
Expand All @@ -18,7 +19,7 @@ pub struct CoreImportResolver<'a, T: ImportResolver + 'static>(pub &'a T);
impl<'a, T: ImportResolver + 'static> ImportResolver for CoreImportResolver<'a, T> {
fn resolve_package(&self, package_segments: &[&str]) -> Result<Option<Program>, AsgConvertError> {
if package_segments.len() > 0 && package_segments.get(0).unwrap() == &"core" {
crate::resolve_core_module(&*package_segments[1..].join("."))
Ok(crate::resolve_core_module(&*package_segments[1..].join("."))?)
} else {
self.0.resolve_package(package_segments)
}
Expand Down
12 changes: 6 additions & 6 deletions asg/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ impl Input {
pub fn new() -> Self {
let registers = Arc::new(Circuit {
id: uuid::Uuid::new_v4(),
name: Identifier::new(REGISTERS_PSUEDO_CIRCUIT.to_string()),
name: RefCell::new(Identifier::new(REGISTERS_PSUEDO_CIRCUIT.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()),
});
let record = Arc::new(Circuit {
id: uuid::Uuid::new_v4(),
name: Identifier::new(RECORD_PSUEDO_CIRCUIT.to_string()),
name: RefCell::new(Identifier::new(RECORD_PSUEDO_CIRCUIT.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()),
});
let state = Arc::new(Circuit {
id: uuid::Uuid::new_v4(),
name: Identifier::new(STATE_PSUEDO_CIRCUIT.to_string()),
name: RefCell::new(Identifier::new(STATE_PSUEDO_CIRCUIT.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()),
});
let state_leaf = Arc::new(Circuit {
id: uuid::Uuid::new_v4(),
name: Identifier::new(STATE_LEAF_PSUEDO_CIRCUIT.to_string()),
name: RefCell::new(Identifier::new(STATE_LEAF_PSUEDO_CIRCUIT.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()),
});
Expand All @@ -54,7 +54,7 @@ impl Input {

let container_circuit = Arc::new(Circuit {
id: uuid::Uuid::new_v4(),
name: Identifier::new(CONTAINER_PSUEDO_CIRCUIT.to_string()),
name: RefCell::new(Identifier::new(CONTAINER_PSUEDO_CIRCUIT.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(container_members),
});
Expand All @@ -80,7 +80,7 @@ impl Input {

impl Circuit {
pub fn is_input_psuedo_circuit(&self) -> bool {
match &*self.name.name {
match &*self.name.borrow().name {
REGISTERS_PSUEDO_CIRCUIT |
RECORD_PSUEDO_CIRCUIT |
STATE_PSUEDO_CIRCUIT |
Expand Down
4 changes: 2 additions & 2 deletions asg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ pub fn load_ast<T: AsRef<Path>, Y: AsRef<str>>(path: T, content: Y) -> Result<le
}

pub fn load_asg_from_ast<T: ImportResolver + 'static>(content: leo_ast::Program, resolver: &T) -> Result<Program, AsgConvertError> {
Program::new(&content, resolver)
InnerProgram::new(&content, resolver)
}

pub fn load_asg<T: ImportResolver + 'static>(content: &str, resolver: &T) -> Result<Program, AsgConvertError> {
Program::new(&load_ast("input.leo", content)?, resolver)
InnerProgram::new(&load_ast("input.leo", content)?, resolver)
}
15 changes: 12 additions & 3 deletions asg/src/program/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub enum CircuitMember {
Function(Arc<Function>),
}


pub struct Circuit {
pub id: Uuid,
pub name: Identifier,
pub name: RefCell<Identifier>,
pub body: RefCell<Weak<CircuitBody>>,
pub members: RefCell<IndexMap<String, CircuitMember>>,
}
Expand All @@ -30,6 +31,7 @@ impl PartialEq for Circuit {
self.id == other.id
}
}
impl Eq for Circuit {}

pub struct CircuitBody {
pub scope: Scope,
Expand All @@ -38,6 +40,13 @@ pub struct CircuitBody {
pub members: RefCell<IndexMap<String, CircuitMemberBody>>,
}

impl PartialEq for CircuitBody {
fn eq(&self, other: &CircuitBody) -> bool {
self.circuit == other.circuit
}
}
impl Eq for CircuitBody {}

impl Node for CircuitMemberBody {

fn span(&self) -> Option<&Span> {
Expand All @@ -51,7 +60,7 @@ impl Circuit {

let circuit = Arc::new(Circuit {
id: Uuid::new_v4(),
name: value.circuit_name.clone(),
name: RefCell::new(value.circuit_name.clone()),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()),
});
Expand Down Expand Up @@ -140,7 +149,7 @@ impl Into<leo_ast::Circuit> for &Circuit {
None => vec![],
};
leo_ast::Circuit {
circuit_name: self.name.clone(),
circuit_name: self.name.borrow().clone(),
members,
}
}
Expand Down
30 changes: 25 additions & 5 deletions asg/src/program/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{ Identifier, Type, WeakType, Statement, Span, AsgConvertError, Block
use std::sync::{ Arc, Weak };
use std::cell::RefCell;
use leo_ast::FunctionInput;
use uuid::Uuid;

#[derive(PartialEq)]
pub enum FunctionQualifier {
Expand All @@ -12,7 +13,8 @@ pub enum FunctionQualifier {
}

pub struct Function {
pub name: Identifier,
pub id: Uuid,
pub name: RefCell<Identifier>,
pub output: WeakType,
pub has_input: bool,
pub argument_types: Vec<WeakType>,
Expand All @@ -21,6 +23,16 @@ pub struct Function {
pub qualifier: FunctionQualifier,
}

impl PartialEq for Function {
fn eq(&self, other: &Function) -> bool {
if self.name.borrow().name != other.name.borrow().name {
return false;
}
self.id == other.id
}
}
impl Eq for Function {}

pub struct FunctionBody {
pub span: Option<Span>,
pub function: Arc<Function>,
Expand All @@ -29,6 +41,13 @@ pub struct FunctionBody {
pub scope: Scope,
}

impl PartialEq for FunctionBody {
fn eq(&self, other: &FunctionBody) -> bool {
self.function == other.function
}
}
impl Eq for FunctionBody {}

impl Function {
pub(crate) fn from_ast(scope: &Scope, value: &leo_ast::Function) -> Result<Function, AsgConvertError> {
let output: Type = value.output.as_ref().map(|t| scope.borrow().resolve_ast_type(t)).transpose()?
Expand Down Expand Up @@ -59,7 +78,8 @@ impl Function {
return Err(AsgConvertError::invalid_self_in_global(&value.span));
}
Ok(Function {
name: value.identifier.clone(),
id: Uuid::new_v4(),
name: RefCell::new(value.identifier.clone()),
output: output.into(),
has_input,
argument_types,
Expand Down Expand Up @@ -116,10 +136,10 @@ impl FunctionBody {
let main_block = BlockStatement::from_ast(&new_scope, &value.block, None)?;
let mut director = MonoidalDirector::new(ReturnPathReducer::new());
if !director.reduce_block(&main_block).0 && !function.output.is_unit() {
return Err(AsgConvertError::function_missing_return(&function.name.name, &value.span));
return Err(AsgConvertError::function_missing_return(&function.name.borrow().name, &value.span));
}
for (span, error) in director.reducer().errors {
return Err(AsgConvertError::function_return_validation(&function.name.name, &error, &span));
return Err(AsgConvertError::function_return_validation(&function.name.borrow().name, &error, &span));
}

Ok(FunctionBody {
Expand Down Expand Up @@ -166,7 +186,7 @@ impl Into<leo_ast::Function> for &Function {
};
let output: Type = self.output.clone().into();
leo_ast::Function {
identifier: self.name.clone(),
identifier: self.name.borrow().clone(),
input,
block: body,
output: Some((&output).into()),
Expand Down
Loading

0 comments on commit ba2a7a6

Please sign in to comment.