diff --git a/Cargo.lock b/Cargo.lock index 32873c0162..c8f951ffbc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -497,6 +497,17 @@ dependencies = [ "toml", ] +[[package]] +name = "leo-ast" +version = "0.1.0" +dependencies = [ + "from-pest", + "lazy_static", + "pest", + "pest-ast", + "pest_derive", +] + [[package]] name = "leo-compiler" version = "0.1.0" @@ -504,6 +515,7 @@ dependencies = [ "from-pest", "hex", "lazy_static", + "leo-ast", "log", "pest", "pest-ast", diff --git a/Cargo.toml b/Cargo.toml index aa774ab144..a23e2a05d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ name = "leo" path = "leo/main.rs" [workspace] -members = [ "compiler" ] +members = [ "ast", "compiler" ] [dependencies] leo-compiler = { path = "compiler", version = "0.1.0" } diff --git a/ast/Cargo.toml b/ast/Cargo.toml new file mode 100644 index 0000000000..56f82f68d2 --- /dev/null +++ b/ast/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "leo-ast" +version = "0.1.0" +authors = ["howardwu "] +edition = "2018" + +[dependencies] +from-pest = { version = "0.3.1" } +lazy_static = { version = "1.3.0" } +pest = { version = "2.0" } +pest-ast = { version = "0.3.3" } +pest_derive = { version = "2.0" } diff --git a/ast/README.md b/ast/README.md new file mode 100644 index 0000000000..99f60d0df8 --- /dev/null +++ b/ast/README.md @@ -0,0 +1 @@ +# leo-ast diff --git a/compiler/src/ast.rs b/ast/src/ast.rs similarity index 100% rename from compiler/src/ast.rs rename to ast/src/ast.rs diff --git a/ast/src/ast_for_statement.rs b/ast/src/ast_for_statement.rs new file mode 100644 index 0000000000..0e443bbdfb --- /dev/null +++ b/ast/src/ast_for_statement.rs @@ -0,0 +1,10 @@ +#[derive(Clone, Debug, FromPest, PartialEq)] +#[pest_ast(rule(Rule::statement_for))] +pub struct ForStatement<'ast> { + pub index: Identifier<'ast>, + pub start: Expression<'ast>, + pub stop: Expression<'ast>, + pub statements: Vec>, + #[pest_ast(outer())] + pub span: Span<'ast>, +} \ No newline at end of file diff --git a/compiler/src/leo.pest b/ast/src/leo.pest similarity index 100% rename from compiler/src/leo.pest rename to ast/src/leo.pest diff --git a/ast/src/lib.rs b/ast/src/lib.rs new file mode 100644 index 0000000000..57e14bf364 --- /dev/null +++ b/ast/src/lib.rs @@ -0,0 +1,11 @@ +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate pest_derive; + +extern crate from_pest; +extern crate pest; +extern crate pest_ast; + +pub mod ast; +pub use ast::*; diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index da2de4ae2d..791351d157 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -5,6 +5,8 @@ authors = ["The Aleo Team "] edition = "2018" [dependencies] +leo-ast = { path = "../ast", version = "0.1.0" } + snarkos-algorithms = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" } snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" } snarkos-errors = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" } diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index db574f6db9..92742c3e09 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -1,11 +1,11 @@ //! Compiles a Leo program from a file path. use crate::{ - ast, constraints::{generate_constraints, generate_test_constraints, ConstrainedValue}, errors::CompilerError, GroupType, InputValue, Program, }; +use leo_ast::ast; use snarkos_errors::gadgets::SynthesisError; use snarkos_models::{ diff --git a/compiler/src/constraints/import.rs b/compiler/src/constraints/import.rs index 0b032aacee..78541b94a2 100644 --- a/compiler/src/constraints/import.rs +++ b/compiler/src/constraints/import.rs @@ -1,11 +1,11 @@ use crate::{ - ast, constraints::{ConstrainedProgram, ConstrainedValue}, errors::constraints::ImportError, new_scope, types::Program, GroupType, Import, }; +use leo_ast::ast; use from_pest::FromPest; use snarkos_models::{ diff --git a/compiler/src/errors/ast/mod.rs b/compiler/src/errors/ast/mod.rs index cf3c7c30d1..6ec6770f95 100644 --- a/compiler/src/errors/ast/mod.rs +++ b/compiler/src/errors/ast/mod.rs @@ -1,4 +1,4 @@ -use crate::ast::Rule; +use leo_ast::ast::Rule; use pest::error::Error; diff --git a/compiler/src/errors/compiler.rs b/compiler/src/errors/compiler.rs index 114e9edb97..f099bbf374 100644 --- a/compiler/src/errors/compiler.rs +++ b/compiler/src/errors/compiler.rs @@ -1,5 +1,5 @@ -use crate::ast::Rule; use crate::errors::{FunctionError, ImportError, IntegerError, SyntaxError}; +use leo_ast::ast::Rule; use pest::error::Error; use std::{io, path::PathBuf}; diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 9b0af8355f..bf7a6aaaaa 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -14,8 +14,6 @@ extern crate pest_ast; #[macro_use] extern crate pest_derive; -pub mod ast; - pub mod compiler; pub mod constraints; diff --git a/compiler/src/types_from.rs b/compiler/src/types_from.rs index 33a937b533..c619aa3967 100644 --- a/compiler/src/types_from.rs +++ b/compiler/src/types_from.rs @@ -1,6 +1,7 @@ //! Logic to convert from an abstract syntax tree (ast) representation to a Leo program. -use crate::{ast, types, Import, ImportSymbol}; +use crate::{types, Import, ImportSymbol}; +use leo_ast::ast; use snarkos_models::gadgets::utilities::{ boolean::Boolean, diff --git a/examples/fibonacci/.gitignore b/examples/fibonacci/.gitignore new file mode 100644 index 0000000000..ee3b8ff567 --- /dev/null +++ b/examples/fibonacci/.gitignore @@ -0,0 +1,2 @@ +/output +/.leo diff --git a/examples/fibonacci/Leo.toml b/examples/fibonacci/Leo.toml new file mode 100644 index 0000000000..9e35608c56 --- /dev/null +++ b/examples/fibonacci/Leo.toml @@ -0,0 +1,3 @@ +[package] +name = "fibonacci" +version = "0.1.0" diff --git a/examples/fibonacci/src/main.leo b/examples/fibonacci/src/main.leo new file mode 100644 index 0000000000..b52c0b09ce --- /dev/null +++ b/examples/fibonacci/src/main.leo @@ -0,0 +1,14 @@ +function fibonacci(i: u32) -> u32 { + if i == 0 { + return 0 + } else if i == 1 { + return 1 + } else { + return fibonacci(i - 1) + fibonacci(i - 2) + } +} + +// The 'fibonacci' main function. +function main() -> u32 { + return fibonacci(1) +} diff --git a/examples/pedersen_hash/src/main.leo b/examples/pedersen_hash/src/main.leo index 91eee630c0..ce7b714716 100644 --- a/examples/pedersen_hash/src/main.leo +++ b/examples/pedersen_hash/src/main.leo @@ -1,5 +1,22 @@ +circuit PedersenHash { + parameters: u32[512] + static function new(parameters: u32[512]) -> Self { + return Self { parameters: parameters } + } + function hash(bits: bool[512]) -> u32 { + let mut digest: u32 = 0; + for i in 0..512 { + let base = if bits[i] ? parameters[i] : 0u32; + digest += base; + } + return digest + } +} + // The 'pedersen_hash' main function. function main() -> u32 { - let a = 1 / 0; - return a + let parameters = [0u32; 512]; + let pedersen = PedersenHash::new(parameters); + let input: bool[512] = [true; 512]; + return pedersen.hash(input) }