mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-11 10:22:33 +03:00
make macro to make this easier
This commit is contained in:
parent
61e791c671
commit
fdf54ea9a1
@ -25,6 +25,17 @@ pub struct CompilerOptions {
|
||||
pub type_inference_enabled: bool,
|
||||
}
|
||||
|
||||
impl CompilerOptions {
|
||||
pub fn new_all_false() -> Self {
|
||||
Self {
|
||||
canonicalization_enabled: false,
|
||||
constant_folding_enabled: false,
|
||||
dead_code_elimination_enabled: false,
|
||||
type_inference_enabled: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CompilerOptions {
|
||||
///
|
||||
/// All compiler optimizations are enabled by default.
|
||||
|
@ -18,3 +18,6 @@
|
||||
|
||||
pub mod reducing_director;
|
||||
pub use reducing_director::*;
|
||||
|
||||
pub mod stage;
|
||||
pub use stage::*;
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
//! Compiles a Leo program from a file path.
|
||||
|
||||
use crate::CompilerOptions;
|
||||
use indexmap::IndexMap;
|
||||
use leo_asg::{
|
||||
ArrayAccessExpression as AsgArrayAccessExpression,
|
||||
@ -100,20 +99,26 @@ use leo_ast::{
|
||||
};
|
||||
use tendril::StrTendril;
|
||||
|
||||
pub struct CombineAstAsgDirector<R: ReconstructingReducer> {
|
||||
ast_reducer: R,
|
||||
options: CompilerOptions,
|
||||
pub trait CombinerOptions {
|
||||
fn type_inference_enabled(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
|
||||
pub fn new(ast_reducer: R, options: CompilerOptions) -> Self {
|
||||
pub struct CombineAstAsgDirector<R: ReconstructingReducer, O: CombinerOptions> {
|
||||
ast_reducer: R,
|
||||
options: O,
|
||||
}
|
||||
|
||||
impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
|
||||
pub fn new(ast_reducer: R, options: O) -> Self {
|
||||
Self { ast_reducer, options }
|
||||
}
|
||||
|
||||
pub fn reduce_type(&mut self, ast: &AstType, asg: &AsgType, span: &Span) -> Result<AstType, ReducerError> {
|
||||
let new = match (ast, asg) {
|
||||
(AstType::Array(ast_type, ast_dimensions), AsgType::Array(asg_type, asg_dimensions)) => {
|
||||
if self.options.type_inference_enabled {
|
||||
if self.options.type_inference_enabled() {
|
||||
AstType::Array(
|
||||
Box::new(self.reduce_type(ast_type, asg_type, span)?),
|
||||
ArrayDimensions(vec![PositiveNumber {
|
||||
@ -410,7 +415,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
|
||||
pub fn reduce_value(&mut self, ast: &ValueExpression, asg: &AsgConstant) -> Result<ValueExpression, ReducerError> {
|
||||
let mut new = ast.clone();
|
||||
|
||||
if self.options.type_inference_enabled {
|
||||
if self.options.type_inference_enabled() {
|
||||
if let ValueExpression::Implicit(tendril, span) = ast {
|
||||
match &asg.value {
|
||||
ConstValue::Int(int) => {
|
||||
@ -631,7 +636,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
|
||||
|
||||
type_ = match &ast.type_ {
|
||||
Some(ast_type) => Some(self.reduce_type(&ast_type, &asg_type, &ast.span)?),
|
||||
None if self.options.type_inference_enabled => Some((&asg_type).into()),
|
||||
None if self.options.type_inference_enabled() => Some((&asg_type).into()),
|
||||
_ => None,
|
||||
};
|
||||
} else {
|
||||
@ -639,7 +644,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
|
||||
Some(ast_type) => {
|
||||
Some(self.reduce_type(&ast_type, &asg.variables.first().unwrap().borrow().type_, &ast.span)?)
|
||||
}
|
||||
None if self.options.type_inference_enabled => {
|
||||
None if self.options.type_inference_enabled() => {
|
||||
Some((&asg.variables.first().unwrap().borrow().type_).into())
|
||||
}
|
||||
_ => None,
|
||||
|
65
compiler/src/stages/stage.rs
Normal file
65
compiler/src/stages/stage.rs
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2019-2021 Aleo Systems Inc.
|
||||
// This file is part of the Leo library.
|
||||
|
||||
// The Leo library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// The Leo library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Compiles a Leo program from a file path.
|
||||
|
||||
use crate::{CombineAstAsgDirector, CombinerOptions};
|
||||
use leo_asg::Program as AsgProgram;
|
||||
use leo_ast::{Ast, Program as AstProgram, ReconstructingReducer, ReducerError};
|
||||
|
||||
macro_rules! stage {
|
||||
($stage_name:ident, $function:item) => {
|
||||
pub struct $stage_name {
|
||||
in_circuit: bool,
|
||||
}
|
||||
|
||||
pub struct Options;
|
||||
|
||||
impl CombinerOptions for Options {
|
||||
$function
|
||||
}
|
||||
|
||||
impl ReconstructingReducer for $stage_name {
|
||||
fn in_circuit(&self) -> bool {
|
||||
self.in_circuit
|
||||
}
|
||||
|
||||
fn swap_in_circuit(&mut self) {
|
||||
self.in_circuit = !self.in_circuit;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for $stage_name {
|
||||
fn default() -> Self {
|
||||
Self { in_circuit: false }
|
||||
}
|
||||
}
|
||||
|
||||
impl $stage_name {
|
||||
pub fn stage_ast(ast: &AstProgram, asg: &AsgProgram) -> Result<Ast, ReducerError> {
|
||||
Ok(Ast::new(CombineAstAsgDirector::new(Self::default(), Options{})
|
||||
.reduce_program(ast, asg)?))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
stage!(
|
||||
TypeInferenceStage,
|
||||
fn type_inference_enabled(&self) -> bool {
|
||||
true
|
||||
}
|
||||
);
|
Loading…
Reference in New Issue
Block a user