From 13ca17634d8c8c20b55767becf192e4fd0ad3a9a Mon Sep 17 00:00:00 2001 From: collin Date: Fri, 7 Aug 2020 16:35:55 -0700 Subject: [PATCH] allow import definition at any point in a leo file --- ast/src/definitions/definition.rs | 2 ++ ast/src/files/file.rs | 3 +-- ast/src/leo.pest | 5 +++-- ast/tests/serialization/expected_ast.json | 1 - compiler/tests/definition/import_fail.leo | 3 --- compiler/tests/definition/mod.rs | 13 +++---------- compiler/tests/definition/out_of_order.leo | 2 ++ compiler/tests/definition/src/test_import.leo | 8 -------- compiler/tests/import/mod.rs | 2 +- typed/src/program.rs | 10 ++-------- 10 files changed, 14 insertions(+), 35 deletions(-) delete mode 100644 compiler/tests/definition/import_fail.leo delete mode 100644 compiler/tests/definition/src/test_import.leo diff --git a/ast/src/definitions/definition.rs b/ast/src/definitions/definition.rs index 71031f7865..5f643f3cc0 100644 --- a/ast/src/definitions/definition.rs +++ b/ast/src/definitions/definition.rs @@ -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>), diff --git a/ast/src/files/file.rs b/ast/src/files/file.rs index ad4d300b5e..58dc4d3c51 100644 --- a/ast/src/files/file.rs +++ b/ast/src/files/file.rs @@ -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>, pub definitions: Vec>, pub eoi: EOI, #[pest_ast(outer())] diff --git a/ast/src/leo.pest b/ast/src/leo.pest index 94373d32c3..2c2d575cfa 100644 --- a/ast/src/leo.pest +++ b/ast/src/leo.pest @@ -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 } diff --git a/ast/tests/serialization/expected_ast.json b/ast/tests/serialization/expected_ast.json index 94665d51bd..6477a3d94b 100644 --- a/ast/tests/serialization/expected_ast.json +++ b/ast/tests/serialization/expected_ast.json @@ -1,5 +1,4 @@ { - "imports": [], "definitions": [ { "Function": { diff --git a/compiler/tests/definition/import_fail.leo b/compiler/tests/definition/import_fail.leo deleted file mode 100644 index 6a92f3e376..0000000000 --- a/compiler/tests/definition/import_fail.leo +++ /dev/null @@ -1,3 +0,0 @@ -function main() {} - -import test_import.foo; \ No newline at end of file diff --git a/compiler/tests/definition/mod.rs b/compiler/tests/definition/mod.rs index e0282c491a..4897900e61 100644 --- a/compiler/tests/definition/mod.rs +++ b/compiler/tests/definition/mod.rs @@ -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); -} diff --git a/compiler/tests/definition/out_of_order.leo b/compiler/tests/definition/out_of_order.leo index bbb9b5a1dc..04e5ddf12b 100644 --- a/compiler/tests/definition/out_of_order.leo +++ b/compiler/tests/definition/out_of_order.leo @@ -2,4 +2,6 @@ test function fake_test() {} function main() {} +import test_import.foo; + circuit Foo {} \ No newline at end of file diff --git a/compiler/tests/definition/src/test_import.leo b/compiler/tests/definition/src/test_import.leo deleted file mode 100644 index 6dd3e2c88a..0000000000 --- a/compiler/tests/definition/src/test_import.leo +++ /dev/null @@ -1,8 +0,0 @@ -circuit Point { - x: u32 - y: u32 -} - -function foo() -> u32 { - return 1u32 -} \ No newline at end of file diff --git a/compiler/tests/import/mod.rs b/compiler/tests/import/mod.rs index d3c1cd3c5d..b3b3d480c3 100644 --- a/compiler/tests/import/mod.rs +++ b/compiler/tests/import/mod.rs @@ -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); diff --git a/typed/src/program.rs b/typed/src/program.rs index 0a246f1506..c4964ffd10 100644 --- a/typed/src/program.rs +++ b/typed/src/program.rs @@ -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::>(); - + 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)); }