Merge branch 'master' of https://github.com/AleoHQ/language into fix/namespace

This commit is contained in:
collin 2020-06-22 18:35:10 -07:00
commit 0a990bdb4e
11 changed files with 37 additions and 16 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -1,5 +1,8 @@
# The Leo Programming Language
[![Build Status](https://travis-ci.com/AleoHQ/leo.svg?token=Xy7ht9JdPvr4xSgbPruF&branch=master)](https://travis-ci.com/AleoHQ/leo)
[![codecov](https://codecov.io/gh/AleoHQ/leo/branch/master/graph/badge.svg?token=S6MWO60SYL)](https://codecov.io/gh/AleoHQ/leo)
## Compiler Architecture
<!-- generated by mermaid compile action - START -->
@ -16,7 +19,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

@ -616,7 +616,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 {
@ -764,14 +764,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}
_ => {
return Err(ExpressionError::undefined_member_access(
circuit.identifier.to_string(),
circuit.circuit_name.to_string(),
circuit_member.to_string(),
span,
));
}
};
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

@ -114,6 +114,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

@ -198,14 +198,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, Span::from(expression.span))
Expression::Circuit(circuit_name, members, Span::from(expression.span))
}
}