mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
Migrate functions
This commit is contained in:
parent
e08ec6f9e9
commit
a380d83c75
@ -1,12 +1,6 @@
|
||||
//! Abstract syntax tree (ast) representation from leo.pest.
|
||||
use crate::{
|
||||
access::AssigneeAccess,
|
||||
circuits::Circuit,
|
||||
common::{
|
||||
Identifier,
|
||||
Mutable,
|
||||
Visibility
|
||||
},
|
||||
common::Identifier,
|
||||
expressions::{
|
||||
ArrayInlineExpression,
|
||||
ArrayInitializerExpression,
|
||||
@ -16,13 +10,10 @@ use crate::{
|
||||
NotExpression,
|
||||
PostfixExpression
|
||||
},
|
||||
imports::Import,
|
||||
operations::{
|
||||
BinaryOperation,
|
||||
NotOperation,
|
||||
},
|
||||
statements::Statement,
|
||||
types::Type,
|
||||
values::Value,
|
||||
};
|
||||
|
||||
@ -33,8 +24,6 @@ use pest::{
|
||||
prec_climber::{Assoc, Operator, PrecClimber},
|
||||
Parser, Span,
|
||||
};
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "leo.pest"]
|
||||
@ -182,61 +171,3 @@ impl<'ast> FromPest<'ast> for Expression<'ast> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::input_model))]
|
||||
pub struct InputModel<'ast> {
|
||||
pub mutable: Option<Mutable>,
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub visibility: Option<Visibility>,
|
||||
pub _type: Type<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::function_definition))]
|
||||
pub struct Function<'ast> {
|
||||
pub function_name: Identifier<'ast>,
|
||||
pub parameters: Vec<InputModel<'ast>>,
|
||||
pub returns: Vec<Type<'ast>>,
|
||||
pub statements: Vec<Statement<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
// Utilities
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::EOI))]
|
||||
pub struct EOI;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::LINE_END))]
|
||||
pub struct LineEnd;
|
||||
|
||||
// Tests
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::test))]
|
||||
pub struct Test<'ast> {
|
||||
pub function: Function<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
// File
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::file))]
|
||||
pub struct File<'ast> {
|
||||
pub imports: Vec<Import<'ast>>,
|
||||
pub circuits: Vec<Circuit<'ast>>,
|
||||
pub functions: Vec<Function<'ast>>,
|
||||
pub tests: Vec<Test<'ast>>,
|
||||
pub eoi: EOI,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, Function}, common::Static};
|
||||
use crate::{ast::Rule, common::Static, function::Function};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{access::AssigneeAccess, ast::Rule, common::Identifier, types::Type};
|
||||
use crate::{access::AssigneeAccess, ast::Rule, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
7
ast/src/common/eoi.rs
Normal file
7
ast/src/common/eoi.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use crate::ast::Rule;
|
||||
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::EOI))]
|
||||
pub struct EOI;
|
7
ast/src/common/line_end.rs
Normal file
7
ast/src/common/line_end.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use crate::ast::Rule;
|
||||
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::LINE_END))]
|
||||
pub struct LineEnd;
|
@ -1,9 +1,15 @@
|
||||
pub mod assignee;
|
||||
pub use assignee::*;
|
||||
|
||||
pub mod eoi;
|
||||
pub use eoi::*;
|
||||
|
||||
pub mod identifier;
|
||||
pub use identifier::*;
|
||||
|
||||
pub mod line_end;
|
||||
pub use line_end::*;
|
||||
|
||||
pub mod mutable;
|
||||
pub use mutable::*;
|
||||
|
||||
|
@ -2,7 +2,6 @@ use crate::{ast::Rule, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::range))]
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::{ast::Rule, common::Range, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::{ast::Rule, common::Spread, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
use crate::{ast::Rule, common::Test, imports::Import, circuits::Circuit};
|
||||
use crate::{ast::Rule, common::EOI, functions::{Function, Test}, imports::Import, circuits::Circuit};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::file))]
|
2
ast/src/files/mod.rs
Normal file
2
ast/src/files/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod file;
|
||||
pub use file::*;
|
@ -0,0 +1,15 @@
|
||||
use crate::{ast::Rule, common::Identifier, functions::InputModel, statements::Statement, types::Type};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::function_definition))]
|
||||
pub struct Function<'ast> {
|
||||
pub function_name: Identifier<'ast>,
|
||||
pub parameters: Vec<InputModel<'ast>>,
|
||||
pub returns: Vec<Type<'ast>>,
|
||||
pub statements: Vec<Statement<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
use crate::{ast::Rule, common::{Identifier, Visibility, Mutable}, types::Type};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::input_model))]
|
||||
pub struct InputModel<'ast> {
|
||||
pub mutable: Option<Mutable>,
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub visibility: Option<Visibility>,
|
||||
pub _type: Type<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
pub mod function;
|
||||
pub use function::*;
|
||||
|
||||
pub mod function_input;
|
||||
pub use function_input::*;
|
||||
|
||||
pub mod test_function;
|
||||
pub use test_function::*;
|
@ -0,0 +1,12 @@
|
||||
use crate::{ast::Rule, functions::Function};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::test))]
|
||||
pub struct Test<'ast> {
|
||||
pub function: Function<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{LineEnd, Rule}, imports::{ImportSource, ImportSymbol}};
|
||||
use crate::{ast::Rule, common::LineEnd, imports::{ImportSource, ImportSymbol}};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -3,6 +3,9 @@
|
||||
// Declared in common/assignee.rs
|
||||
assignee = { identifier ~ access_assignee* }
|
||||
|
||||
// Declared in common/file.rs
|
||||
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test* ~ NEWLINE* ~ EOI }
|
||||
|
||||
// Declared in common/identifier.rs
|
||||
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
protected_name = { visibility | "let" | "for"| "if" | "else" | "as" | "return" }
|
||||
@ -291,7 +294,3 @@ LINE_END = {";" ~ NEWLINE*}
|
||||
/// Tests
|
||||
|
||||
test = { "test" ~ function_definition }
|
||||
|
||||
/// Program File
|
||||
|
||||
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test* ~ NEWLINE* ~ EOI }
|
||||
|
@ -23,6 +23,12 @@ pub use errors::*;
|
||||
pub mod expressions;
|
||||
pub use expressions::*;
|
||||
|
||||
pub mod files;
|
||||
pub use files::*;
|
||||
|
||||
pub mod functions;
|
||||
pub use functions::*;
|
||||
|
||||
pub mod imports;
|
||||
pub use imports::*;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, LineEnd}, expressions::Expression};
|
||||
use crate::{ast::Rule, common::LineEnd, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, LineEnd}, common::Assignee, expressions::Expression, operations::AssignOperation};
|
||||
use crate::{ast::Rule, common::{Assignee, LineEnd}, expressions::Expression, operations::AssignOperation};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, LineEnd}, common::{Variable}, expressions::Expression};
|
||||
use crate::{ast::Rule, common::{LineEnd, Variable}, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, LineEnd}, expressions::Expression};
|
||||
use crate::{ast::Rule, common::LineEnd, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, LineEnd}, common::{Identifier, Variable}, expressions::{Expression}};
|
||||
use crate::{ast::Rule, common::{Identifier, LineEnd, Variable}, expressions::{Expression}};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -5,7 +5,7 @@ use crate::{
|
||||
errors::CompilerError,
|
||||
GroupType, InputValue, Program,
|
||||
};
|
||||
use leo_ast::ast;
|
||||
use leo_ast::{ast, files::File};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
@ -75,7 +75,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
generate_test_constraints::<F, G>(cs, self.program)
|
||||
}
|
||||
|
||||
// pub fn compile(&self) -> Result<ast::File, CompilerError> {
|
||||
// pub fn compile(&self) -> Result<File, CompilerError> {
|
||||
// // Read in the main file as string
|
||||
// let unparsed_file = fs::read_to_string(&self.main_file_path).map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
|
||||
//
|
||||
@ -83,7 +83,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
// let mut file = ast::parse(&unparsed_file).map_err(|_| CompilerError::FileParsingError)?;
|
||||
//
|
||||
// // Build the abstract syntax tree
|
||||
// let syntax_tree = ast::File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
||||
// let syntax_tree = File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
||||
// log::debug!("{:#?}", syntax_tree);
|
||||
//
|
||||
// Ok(syntax_tree)
|
||||
@ -101,7 +101,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
|
||||
// Build the abstract syntax tree
|
||||
let syntax_tree =
|
||||
ast::File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
||||
File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
||||
log::debug!("{:#?}", syntax_tree);
|
||||
|
||||
// Build program from abstract syntax tree
|
||||
|
@ -5,7 +5,7 @@ use crate::{
|
||||
types::Program,
|
||||
GroupType, Import,
|
||||
};
|
||||
use leo_ast::ast;
|
||||
use leo_ast::{ast, files::File};
|
||||
|
||||
use from_pest::FromPest;
|
||||
use snarkos_models::{
|
||||
@ -42,7 +42,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
|
||||
// generate ast from file
|
||||
let syntax_tree =
|
||||
ast::File::from_pest(&mut file).map_err(|_| ImportError::SyntaxTreeError)?;
|
||||
File::from_pest(&mut file).map_err(|_| ImportError::SyntaxTreeError)?;
|
||||
|
||||
// generate aleo program from file
|
||||
let mut program = Program::from(syntax_tree, import.path_string.clone());
|
||||
|
@ -2,11 +2,11 @@
|
||||
|
||||
use crate::{types, Import, ImportSymbol};
|
||||
use leo_ast::{
|
||||
File,
|
||||
access::{
|
||||
Access,
|
||||
AssigneeAccess,
|
||||
},
|
||||
ast,
|
||||
circuits::{
|
||||
Circuit,
|
||||
CircuitField,
|
||||
@ -33,6 +33,11 @@ use leo_ast::{
|
||||
PostfixExpression,
|
||||
TernaryExpression
|
||||
},
|
||||
functions::{
|
||||
Function,
|
||||
InputModel,
|
||||
Test
|
||||
},
|
||||
imports::{
|
||||
Import as AstImport,
|
||||
ImportSymbol as AstImportSymbol,
|
||||
@ -789,8 +794,8 @@ impl<'ast> From<Circuit<'ast>> for types::Circuit {
|
||||
|
||||
/// pest ast -> function types::Parameters
|
||||
|
||||
impl<'ast> From<ast::InputModel<'ast>> for types::InputModel {
|
||||
fn from(parameter: ast::InputModel<'ast>) -> Self {
|
||||
impl<'ast> From<InputModel<'ast>> for types::InputModel {
|
||||
fn from(parameter: InputModel<'ast>) -> Self {
|
||||
types::InputModel {
|
||||
identifier: types::Identifier::from(parameter.identifier),
|
||||
mutable: parameter.mutable.is_some(),
|
||||
@ -805,8 +810,8 @@ impl<'ast> From<ast::InputModel<'ast>> for types::InputModel {
|
||||
|
||||
/// pest ast -> types::Function
|
||||
|
||||
impl<'ast> From<ast::Function<'ast>> for types::Function {
|
||||
fn from(function_definition: ast::Function<'ast>) -> Self {
|
||||
impl<'ast> From<Function<'ast>> for types::Function {
|
||||
fn from(function_definition: Function<'ast>) -> Self {
|
||||
let function_name = types::Identifier::from(function_definition.function_name);
|
||||
let parameters = function_definition
|
||||
.parameters
|
||||
@ -858,8 +863,8 @@ impl<'ast> From<AstImport<'ast>> for Import {
|
||||
}
|
||||
|
||||
/// pest ast -> Test
|
||||
impl<'ast> From<ast::Test<'ast>> for types::Test {
|
||||
fn from(test: ast::Test) -> Self {
|
||||
impl<'ast> From<Test<'ast>> for types::Test {
|
||||
fn from(test: Test) -> Self {
|
||||
types::Test(types::Function::from(test.function))
|
||||
}
|
||||
}
|
||||
@ -867,7 +872,7 @@ impl<'ast> From<ast::Test<'ast>> for types::Test {
|
||||
/// pest ast -> types::Program
|
||||
|
||||
impl<'ast> types::Program {
|
||||
pub fn from(file: ast::File<'ast>, name: String) -> Self {
|
||||
pub fn from(file: File<'ast>, name: String) -> Self {
|
||||
// Compiled ast -> aleo program representation
|
||||
let imports = file
|
||||
.imports
|
||||
|
Loading…
Reference in New Issue
Block a user