allow import definition at any point in a leo file

This commit is contained in:
collin 2020-08-07 16:35:55 -07:00
parent 424287eb7b
commit 13ca17634d
10 changed files with 14 additions and 35 deletions

View File

@ -2,6 +2,7 @@ use crate::{
ast::Rule,
circuits::Circuit,
functions::{Function, TestFunction},
imports::Import,
};
use pest_ast::FromPest;
@ -10,6 +11,7 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::definition))]
pub enum Definition<'ast> {
Import(Import<'ast>),
Circuit(Circuit<'ast>),
Function(Function<'ast>),
TestFunction(TestFunction<'ast>),

View File

@ -1,4 +1,4 @@
use crate::{ast::Rule, common::EOI, definitions::Definition, imports::Import, SpanDef};
use crate::{ast::Rule, common::EOI, definitions::Definition, SpanDef};
use pest::Span;
use pest_ast::FromPest;
@ -7,7 +7,6 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::file))]
pub struct File<'ast> {
pub imports: Vec<Import<'ast>>,
pub definitions: Vec<Definition<'ast>>,
pub eoi: EOI,
#[pest_ast(outer())]

View File

@ -4,11 +4,12 @@
assignee = { identifier ~ access_assignee* }
// Declared in files/file.rs
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ definition* ~ NEWLINE* ~ EOI }
file = { SOI ~ NEWLINE* ~ definition* ~ NEWLINE* ~ EOI }
// Declared in definitions/definition.rs
definition = {
circuit
import
| circuit
| function
| test_function
}

View File

@ -1,5 +1,4 @@
{
"imports": [],
"definitions": [
{
"Function": {

View File

@ -1,3 +0,0 @@
function main() {}
import test_import.foo;

View File

@ -1,19 +1,12 @@
use crate::{assert_satisfied, parse_program};
use crate::{assert_satisfied, import::set_local_dir, parse_program};
#[test]
fn test_out_of_order() {
set_local_dir();
let program_bytes = include_bytes!("out_of_order.leo");
let program = parse_program(program_bytes).unwrap();
assert_satisfied(program);
}
#[test]
fn test_import_fail() {
let program_bytes = include_bytes!("import_fail.leo");
let syntax_error = parse_program(program_bytes).is_err();
assert!(syntax_error);
}

View File

@ -2,4 +2,6 @@ test function fake_test() {}
function main() {}
import test_import.foo;
circuit Foo {}

View File

@ -1,8 +0,0 @@
circuit Point {
x: u32
y: u32
}
function foo() -> u32 {
return 1u32
}

View File

@ -6,7 +6,7 @@ static TEST_SOURCE_DIRECTORY: &str = "tests/import";
// Import tests rely on knowledge of local directories. They should be run locally only.
fn set_local_dir() {
pub fn set_local_dir() {
let mut local = current_dir().unwrap();
local.push(TEST_SOURCE_DIRECTORY);

View File

@ -23,14 +23,7 @@ const MAIN_FUNCTION_NAME: &str = "main";
impl<'ast> Program {
//! Logic to convert from an abstract syntax tree (ast) representation to a Leo program.
pub fn from(program_name: &str, program_ast: &File<'ast>) -> Self {
// Compiled ast -> aleo program representation
let imports = program_ast
.imports
.to_owned()
.into_iter()
.map(|import| Import::from(import))
.collect::<Vec<Import>>();
let mut imports = vec![];
let mut circuits = HashMap::new();
let mut functions = HashMap::new();
let mut tests = HashMap::new();
@ -41,6 +34,7 @@ impl<'ast> Program {
.to_owned()
.into_iter()
.for_each(|definition| match definition {
Definition::Import(import) => imports.push(Import::from(import)),
Definition::Circuit(circuit) => {
circuits.insert(Identifier::from(circuit.identifier.clone()), Circuit::from(circuit));
}