Merge pull request #69 from AleoHQ/feature/values

Update ast and typed names
This commit is contained in:
Collin Chin 2020-06-20 16:34:35 -07:00 committed by GitHub
commit aa68df0f7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 16 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -16,7 +16,7 @@ graph LR
Pass2 -- statements --> Pass4(Synthesizer)
Pass4 -- constraints --> Pass5(Circuit)
Pass4 -- constraints --> Pass5(Program)
```
</details>

View File

@ -4,7 +4,7 @@ use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::circuit_definition))]
#[pest_ast(rule(Rule::circuit))]
pub struct Circuit<'ast> {
pub identifier: Identifier<'ast>,
pub members: Vec<CircuitMember<'ast>>,

View File

@ -4,7 +4,7 @@
assignee = { identifier ~ access_assignee* }
// Declared in common/file.rs
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test_function* ~ NEWLINE* ~ EOI }
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test_function* ~ NEWLINE* ~ EOI }
// Declared in common/identifier.rs
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
@ -177,7 +177,7 @@ access_static_member = { "::" ~ identifier }
/// Circuits
// Declared in circuits/circuit_definition.rs
circuit_definition = { "circuit" ~ identifier ~ "{" ~ NEWLINE* ~ circuit_member* ~ NEWLINE* ~ "}" ~ NEWLINE* }
circuit = { "circuit" ~ identifier ~ "{" ~ NEWLINE* ~ circuit_member* ~ NEWLINE* ~ "}" ~ NEWLINE* }
// Declared in circuits/circuit_field.rs
circuit_field = { identifier ~ ":" ~ expression }

View File

@ -519,7 +519,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}
if let Some(ConstrainedValue::CircuitDefinition(circuit_definition)) = self.get_mut(&program_identifier) {
let circuit_identifier = circuit_definition.identifier.clone();
let circuit_identifier = circuit_definition.circuit_name.clone();
let mut resolved_members = vec![];
for member in circuit_definition.members.clone().into_iter() {
match member {
@ -660,13 +660,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}
_ => {
return Err(ExpressionError::UndefinedStaticAccess(
circuit.identifier.to_string(),
circuit.circuit_name.to_string(),
circuit_member.to_string(),
));
}
};
Ok(ConstrainedValue::Function(Some(circuit.identifier), function))
Ok(ConstrainedValue::Function(Some(circuit.circuit_name), function))
}
fn enforce_function_call_expression<CS: ConstraintSystem<F>>(

View File

@ -3,6 +3,7 @@
use crate::{constraints::ConstrainedValue, GroupType};
use snarkos_models::curves::{Field, PrimeField};
use std::collections::HashMap;
pub struct ConstrainedProgram<F: Field + PrimeField, G: GroupType<F>> {

View File

@ -0,0 +1,12 @@
circuit Foo {
foo: u32
static function bar() -> u32 {
return 0
}
}
function main() -> u32 {
let f = Foo { foo: 1 };
return f.foo + Foo::bar()
}

View File

@ -98,6 +98,14 @@ fn test_member_field_fail() {
fail_undefined_member(program);
}
#[test]
fn test_member_field_and_function() {
let bytes = include_bytes!("member_field_and_function.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
}
#[test]
fn test_member_function() {
let bytes = include_bytes!("member_function.leo");

View File

@ -5,29 +5,26 @@ use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct Circuit {
pub identifier: Identifier,
pub circuit_name: Identifier,
pub members: Vec<CircuitMember>,
}
impl<'ast> From<AstCircuit<'ast>> for Circuit {
fn from(circuit: AstCircuit<'ast>) -> Self {
let variable = Identifier::from(circuit.identifier);
let circuit_name = Identifier::from(circuit.identifier);
let members = circuit
.members
.into_iter()
.map(|member| CircuitMember::from(member))
.collect();
Self {
identifier: variable,
members,
}
Self { circuit_name, members }
}
}
impl Circuit {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "circuit {} {{ \n", self.identifier)?;
write!(f, "circuit {} {{ \n", self.circuit_name)?;
for field in self.members.iter() {
write!(f, " {}\n", field)?;
}

View File

@ -161,14 +161,14 @@ impl<'ast> fmt::Display for Expression {
impl<'ast> From<CircuitInlineExpression<'ast>> for Expression {
fn from(expression: CircuitInlineExpression<'ast>) -> Self {
let variable = Identifier::from(expression.identifier);
let circuit_name = Identifier::from(expression.identifier);
let members = expression
.members
.into_iter()
.map(|member| CircuitFieldDefinition::from(member))
.collect::<Vec<CircuitFieldDefinition>>();
Expression::Circuit(variable, members)
Expression::Circuit(circuit_name, members)
}
}