mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-27 12:17:35 +03:00
Migrates circuits
This commit is contained in:
parent
624470a67f
commit
6f9f397310
@ -4,10 +4,9 @@ use crate::{
|
||||
constraints::{ConstrainedCircuitMember, ConstrainedProgram, ConstrainedValue},
|
||||
errors::ExpressionError,
|
||||
new_scope,
|
||||
types::CircuitMember,
|
||||
FieldType, GroupType,
|
||||
};
|
||||
use leo_types::{CircuitFieldDefinition, Expression, RangeOrExpression,
|
||||
use leo_types::{CircuitFieldDefinition,CircuitMember, Expression, RangeOrExpression,
|
||||
SpreadOrExpression, Identifier, Integer, IntegerType, Type};
|
||||
|
||||
use snarkos_models::{
|
||||
|
@ -2,10 +2,9 @@
|
||||
|
||||
use crate::{
|
||||
errors::ValueError,
|
||||
types::{Circuit},
|
||||
FieldType, GroupType,
|
||||
};
|
||||
use leo_types::{Function, Identifier, Integer, IntegerType, Type};
|
||||
use leo_types::{Circuit, Function, Identifier, Integer, IntegerType, Type};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -22,8 +22,5 @@ pub use self::imports::*;
|
||||
pub mod types;
|
||||
pub use self::types::*;
|
||||
|
||||
pub mod types_display;
|
||||
pub use self::types_display::*;
|
||||
|
||||
pub mod types_from;
|
||||
pub use self::types_from::*;
|
||||
|
@ -2,24 +2,10 @@
|
||||
//! Each defined type consists of typed statements and expressions.
|
||||
|
||||
use crate::Import;
|
||||
use leo_types::{Identifier, Function, TestFunction, Type};
|
||||
use leo_types::{Circuit, Identifier, Function, TestFunction};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Circuits
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum CircuitMember {
|
||||
CircuitField(Identifier, Type),
|
||||
CircuitFunction(bool, Function),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Circuit {
|
||||
pub identifier: Identifier,
|
||||
pub members: Vec<CircuitMember>,
|
||||
}
|
||||
|
||||
/// A simple program with statement expressions, program arguments and program returns.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Program {
|
||||
|
@ -1,45 +0,0 @@
|
||||
//! Format display functions for Leo types.
|
||||
|
||||
use crate::{
|
||||
Circuit, CircuitMember,
|
||||
};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
impl fmt::Display for CircuitMember {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
CircuitMember::CircuitField(ref identifier, ref _type) => {
|
||||
write!(f, "{}: {}", identifier, _type)
|
||||
}
|
||||
CircuitMember::CircuitFunction(ref _static, ref function) => {
|
||||
if *_static {
|
||||
write!(f, "static ")?;
|
||||
}
|
||||
write!(f, "{}", function)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Circuit {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "circuit {} {{ \n", self.identifier)?;
|
||||
for field in self.members.iter() {
|
||||
write!(f, " {}\n", field)?;
|
||||
}
|
||||
write!(f, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
// impl fmt::Display for Circuit {// uncomment when we no longer print out Program
|
||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// self.format(f)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl fmt::Debug for Circuit {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
@ -2,71 +2,16 @@
|
||||
|
||||
use crate::{types, Import, ImportSymbol};
|
||||
use leo_ast::{
|
||||
circuits::{
|
||||
Circuit,
|
||||
CircuitFieldDefinition as AstCircuitFieldDefinition,
|
||||
CircuitFunction,
|
||||
CircuitMember
|
||||
},
|
||||
files::File,
|
||||
imports::{
|
||||
Import as AstImport,
|
||||
ImportSymbol as AstImportSymbol,
|
||||
},
|
||||
};
|
||||
use leo_types::{Function, Identifier, TestFunction, Type};
|
||||
use leo_types::{Circuit, Function, Identifier, TestFunction};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// pest ast -> types::Circuit
|
||||
|
||||
impl<'ast> From<AstCircuitFieldDefinition<'ast>> for types::CircuitMember {
|
||||
fn from(circuit_value: AstCircuitFieldDefinition<'ast>) -> Self {
|
||||
types::CircuitMember::CircuitField(
|
||||
Identifier::from(circuit_value.identifier),
|
||||
Type::from(circuit_value._type),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<CircuitFunction<'ast>> for types::CircuitMember {
|
||||
fn from(circuit_function: CircuitFunction<'ast>) -> Self {
|
||||
types::CircuitMember::CircuitFunction(
|
||||
circuit_function._static.is_some(),
|
||||
Function::from(circuit_function.function),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<CircuitMember<'ast>> for types::CircuitMember {
|
||||
fn from(object: CircuitMember<'ast>) -> Self {
|
||||
match object {
|
||||
CircuitMember::CircuitFieldDefinition(circuit_value) => {
|
||||
types::CircuitMember::from(circuit_value)
|
||||
}
|
||||
CircuitMember::CircuitFunction(circuit_function) => {
|
||||
types::CircuitMember::from(circuit_function)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<Circuit<'ast>> for types::Circuit {
|
||||
fn from(circuit: Circuit<'ast>) -> Self {
|
||||
let variable = Identifier::from(circuit.identifier);
|
||||
let members = circuit
|
||||
.members
|
||||
.into_iter()
|
||||
.map(|member| types::CircuitMember::from(member))
|
||||
.collect();
|
||||
|
||||
types::Circuit {
|
||||
identifier: variable,
|
||||
members,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> Import
|
||||
|
||||
impl<'ast> From<AstImportSymbol<'ast>> for ImportSymbol {
|
||||
@ -110,7 +55,7 @@ impl<'ast> types::Program {
|
||||
file.circuits.into_iter().for_each(|circuit| {
|
||||
circuits.insert(
|
||||
Identifier::from(circuit.identifier.clone()),
|
||||
types::Circuit::from(circuit),
|
||||
Circuit::from(circuit),
|
||||
);
|
||||
});
|
||||
file.functions.into_iter().for_each(|function_def| {
|
||||
|
@ -1,6 +1,6 @@
|
||||
pub mod array;
|
||||
pub mod boolean;
|
||||
pub mod circuit;
|
||||
pub mod circuits;
|
||||
pub mod field;
|
||||
pub mod function;
|
||||
pub mod group;
|
||||
|
49
types/src/circuits/circuit.rs
Normal file
49
types/src/circuits/circuit.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use crate::{Identifier, CircuitMember};
|
||||
use leo_ast::circuits::Circuit as AstCircuit;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Circuit {
|
||||
pub identifier: 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 members = circuit
|
||||
.members
|
||||
.into_iter()
|
||||
.map(|member| CircuitMember::from(member))
|
||||
.collect();
|
||||
|
||||
Circuit {
|
||||
identifier: variable,
|
||||
members,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Circuit {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "circuit {} {{ \n", self.identifier)?;
|
||||
for field in self.members.iter() {
|
||||
write!(f, " {}\n", field)?;
|
||||
}
|
||||
write!(f, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO (Collin): Uncomment when we no longer print out Program
|
||||
// impl fmt::Display for Circuit {
|
||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// self.format(f)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl fmt::Debug for Circuit {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
58
types/src/circuits/circuit_member.rs
Normal file
58
types/src/circuits/circuit_member.rs
Normal file
@ -0,0 +1,58 @@
|
||||
use crate::{Identifier, Function, Type};
|
||||
use leo_ast::circuits::{CircuitFieldDefinition as AstCircuitFieldDefinition, CircuitMember as AstCircuitMember, CircuitFunction as AstCircuitFunction};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum CircuitMember {
|
||||
CircuitField(Identifier, Type),
|
||||
CircuitFunction(bool, Function),
|
||||
}
|
||||
|
||||
|
||||
impl<'ast> From<AstCircuitFieldDefinition<'ast>> for CircuitMember {
|
||||
fn from(circuit_value: AstCircuitFieldDefinition<'ast>) -> Self {
|
||||
CircuitMember::CircuitField(
|
||||
Identifier::from(circuit_value.identifier),
|
||||
Type::from(circuit_value._type),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstCircuitFunction<'ast>> for CircuitMember {
|
||||
fn from(circuit_function: AstCircuitFunction<'ast>) -> Self {
|
||||
CircuitMember::CircuitFunction(
|
||||
circuit_function._static.is_some(),
|
||||
Function::from(circuit_function.function),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstCircuitMember<'ast>> for CircuitMember {
|
||||
fn from(object: AstCircuitMember<'ast>) -> Self {
|
||||
match object {
|
||||
AstCircuitMember::CircuitFieldDefinition(circuit_value) => {
|
||||
CircuitMember::from(circuit_value)
|
||||
}
|
||||
AstCircuitMember::CircuitFunction(circuit_function) => {
|
||||
CircuitMember::from(circuit_function)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CircuitMember {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
CircuitMember::CircuitField(ref identifier, ref _type) => {
|
||||
write!(f, "{}: {}", identifier, _type)
|
||||
}
|
||||
CircuitMember::CircuitFunction(ref _static, ref function) => {
|
||||
if *_static {
|
||||
write!(f, "static ")?;
|
||||
}
|
||||
write!(f, "{}", function)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
types/src/circuits/mod.rs
Normal file
8
types/src/circuits/mod.rs
Normal file
@ -0,0 +1,8 @@
|
||||
pub mod circuit;
|
||||
pub use circuit::*;
|
||||
|
||||
pub mod circuit_field_definition;
|
||||
pub use circuit_field_definition::*;
|
||||
|
||||
pub mod circuit_member;
|
||||
pub use circuit_member::*;
|
@ -1,8 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod circuit_field_definition;
|
||||
pub use circuit_field_definition::*;
|
||||
pub mod circuits;
|
||||
pub use circuits::*;
|
||||
|
||||
pub mod common;
|
||||
pub use common::*;
|
||||
|
Loading…
Reference in New Issue
Block a user