mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-29 12:34:20 +03:00
Merge pull request #69 from AleoHQ/feature/values
Update ast and typed names
This commit is contained in:
commit
aa68df0f7d
Binary file not shown.
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
@ -16,7 +16,7 @@ graph LR
|
||||
|
||||
Pass2 -- statements --> Pass4(Synthesizer)
|
||||
|
||||
Pass4 -- constraints --> Pass5(Circuit)
|
||||
Pass4 -- constraints --> Pass5(Program)
|
||||
```
|
||||
|
||||
</details>
|
||||
|
@ -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>>,
|
||||
|
@ -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 }
|
||||
|
@ -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>>(
|
||||
|
@ -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>> {
|
||||
|
12
compiler/tests/circuits/member_field_and_function.leo
Normal file
12
compiler/tests/circuits/member_field_and_function.leo
Normal 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()
|
||||
}
|
@ -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");
|
||||
|
@ -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)?;
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user