mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-11 15:05:33 +03:00
Migrates imports
This commit is contained in:
parent
6f9f397310
commit
35a897282f
@ -3,9 +3,10 @@ use crate::{
|
||||
errors::constraints::ImportError,
|
||||
new_scope,
|
||||
types::Program,
|
||||
GroupType, Import,
|
||||
GroupType,
|
||||
};
|
||||
use leo_ast::{ast, files::File};
|
||||
use leo_types::{Import};
|
||||
|
||||
use from_pest::FromPest;
|
||||
use snarkos_models::{
|
||||
|
@ -16,9 +16,6 @@ pub use self::field::*;
|
||||
pub mod group;
|
||||
pub use self::group::*;
|
||||
|
||||
pub mod imports;
|
||||
pub use self::imports::*;
|
||||
|
||||
pub mod types;
|
||||
pub use self::types::*;
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
//! A typed Leo program consists of import, circuit, and function definitions.
|
||||
//! Each defined type consists of typed statements and expressions.
|
||||
|
||||
use crate::Import;
|
||||
use leo_types::{Circuit, Identifier, Function, TestFunction};
|
||||
use leo_types::{Circuit, Identifier,Import, Function, TestFunction};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
@ -1,41 +1,13 @@
|
||||
//! Logic to convert from an abstract syntax tree (ast) representation to a Leo program.
|
||||
|
||||
use crate::{types, Import, ImportSymbol};
|
||||
use crate::{types};
|
||||
use leo_ast::{
|
||||
files::File,
|
||||
imports::{
|
||||
Import as AstImport,
|
||||
ImportSymbol as AstImportSymbol,
|
||||
},
|
||||
};
|
||||
use leo_types::{Circuit, Function, Identifier, TestFunction};
|
||||
use leo_types::{Circuit, Function, Import, Identifier, TestFunction};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// pest ast -> Import
|
||||
|
||||
impl<'ast> From<AstImportSymbol<'ast>> for ImportSymbol {
|
||||
fn from(symbol: AstImportSymbol<'ast>) -> Self {
|
||||
ImportSymbol {
|
||||
symbol: Identifier::from(symbol.value),
|
||||
alias: symbol.alias.map(|alias| Identifier::from(alias)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstImport<'ast>> for Import {
|
||||
fn from(import: AstImport<'ast>) -> Self {
|
||||
Import {
|
||||
path_string: import.source.value,
|
||||
symbols: import
|
||||
.symbols
|
||||
.into_iter()
|
||||
.map(|symbol| ImportSymbol::from(symbol))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> types::Program
|
||||
|
||||
impl<'ast> types::Program {
|
||||
|
@ -1,21 +1,29 @@
|
||||
//! The Import type for a Leo program.
|
||||
//! The import type for a Leo program.
|
||||
|
||||
use leo_types::Identifier;
|
||||
use crate::ImportSymbol;
|
||||
use leo_ast::imports::Import as AstImport;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ImportSymbol {
|
||||
pub symbol: Identifier,
|
||||
pub alias: Option<Identifier>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Import {
|
||||
pub path_string: String,
|
||||
pub symbols: Vec<ImportSymbol>,
|
||||
}
|
||||
|
||||
impl<'ast> From<AstImport<'ast>> for Import {
|
||||
fn from(import: AstImport<'ast>) -> Self {
|
||||
Import {
|
||||
path_string: import.source.value,
|
||||
symbols: import
|
||||
.symbols
|
||||
.into_iter()
|
||||
.map(|symbol| ImportSymbol::from(symbol))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Import {
|
||||
pub fn new(source: String, symbols: Vec<ImportSymbol>) -> Import {
|
||||
Import {
|
||||
@ -50,16 +58,6 @@ impl Import {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ImportSymbol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if self.alias.is_some() {
|
||||
write!(f, "\t{} as {}", self.symbol, self.alias.as_ref().unwrap())
|
||||
} else {
|
||||
write!(f, "\t{}", self.symbol)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Import {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
29
types/src/imports/import_symbol.rs
Normal file
29
types/src/imports/import_symbol.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use crate::Identifier;
|
||||
use leo_ast::imports::ImportSymbol as AstImportSymbol;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ImportSymbol {
|
||||
pub symbol: Identifier,
|
||||
pub alias: Option<Identifier>,
|
||||
}
|
||||
|
||||
impl<'ast> From<AstImportSymbol<'ast>> for ImportSymbol {
|
||||
fn from(symbol: AstImportSymbol<'ast>) -> Self {
|
||||
ImportSymbol {
|
||||
symbol: Identifier::from(symbol.value),
|
||||
alias: symbol.alias.map(|alias| Identifier::from(alias)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ImportSymbol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if self.alias.is_some() {
|
||||
write!(f, "\t{} as {}", self.symbol, self.alias.as_ref().unwrap())
|
||||
} else {
|
||||
write!(f, "\t{}", self.symbol)
|
||||
}
|
||||
}
|
||||
}
|
5
types/src/imports/mod.rs
Normal file
5
types/src/imports/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
pub mod import;
|
||||
pub use import::*;
|
||||
|
||||
pub mod import_symbol;
|
||||
pub use import_symbol::*;
|
@ -16,6 +16,9 @@ pub use expression::*;
|
||||
pub mod functions;
|
||||
pub use functions::*;
|
||||
|
||||
pub mod imports;
|
||||
pub use imports::*;
|
||||
|
||||
pub mod input_value;
|
||||
pub use input_value::*;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user