mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 07:07:07 +03:00
turn back to identifiers, looked into inlining imports in asg
This commit is contained in:
parent
93509bf5c1
commit
4d89d122d5
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1199,6 +1199,7 @@ dependencies = [
|
||||
"criterion",
|
||||
"indexmap",
|
||||
"leo-ast",
|
||||
"leo-ast-passes",
|
||||
"leo-errors",
|
||||
"leo-parser",
|
||||
"num-bigint",
|
||||
|
@ -30,6 +30,10 @@ version = "1.7"
|
||||
version = "1.5.3"
|
||||
path = "../ast"
|
||||
|
||||
[dependencies.leo-ast-passes]
|
||||
path = "../ast-passes"
|
||||
version = "1.5.3"
|
||||
|
||||
[dependencies.leo-errors]
|
||||
path = "../errors"
|
||||
version = "1.5.3"
|
||||
|
@ -103,22 +103,25 @@ impl<'a> Program<'a> {
|
||||
|
||||
// Prepare header-like scope entries.
|
||||
for (name, circuit) in program.circuits.iter() {
|
||||
assert_eq!(name.name, circuit.circuit_name.name);
|
||||
let asg_circuit = Circuit::init(scope, circuit)?;
|
||||
|
||||
scope.circuits.borrow_mut().insert(name.clone(), asg_circuit);
|
||||
scope.circuits.borrow_mut().insert(name.name.to_string(), asg_circuit);
|
||||
}
|
||||
|
||||
// Second pass for circuit members.
|
||||
for (name, circuit) in program.circuits.iter() {
|
||||
assert_eq!(name.name, circuit.circuit_name.name);
|
||||
let asg_circuit = Circuit::init_member(scope, circuit)?;
|
||||
|
||||
scope.circuits.borrow_mut().insert(name.clone(), asg_circuit);
|
||||
scope.circuits.borrow_mut().insert(name.name.to_string(), asg_circuit);
|
||||
}
|
||||
|
||||
for (name, function) in program.functions.iter() {
|
||||
assert_eq!(name.name, function.identifier.name);
|
||||
let function = Function::init(scope, function)?;
|
||||
|
||||
scope.functions.borrow_mut().insert(name.clone(), function);
|
||||
scope.functions.borrow_mut().insert(name.name.to_string(), function);
|
||||
}
|
||||
|
||||
for (name, global_const) in program.global_consts.iter() {
|
||||
@ -146,11 +149,12 @@ impl<'a> Program<'a> {
|
||||
|
||||
let mut functions = IndexMap::new();
|
||||
for (name, function) in program.functions.iter() {
|
||||
let asg_function = *scope.functions.borrow().get(name).unwrap();
|
||||
assert_eq!(name.name, function.identifier.name);
|
||||
let asg_function = *scope.functions.borrow().get(name.name.as_ref()).unwrap();
|
||||
|
||||
asg_function.fill_from_ast(function)?;
|
||||
|
||||
let name = name.clone();
|
||||
let name = name.name.to_string();
|
||||
|
||||
if functions.contains_key(&name) {
|
||||
return Err(AsgError::duplicate_function_definition(name, &function.span).into());
|
||||
@ -161,11 +165,12 @@ impl<'a> Program<'a> {
|
||||
|
||||
let mut circuits = IndexMap::new();
|
||||
for (name, circuit) in program.circuits.iter() {
|
||||
let asg_circuit = *scope.circuits.borrow().get(name).unwrap();
|
||||
assert_eq!(name.name, circuit.circuit_name.name);
|
||||
let asg_circuit = *scope.circuits.borrow().get(name.name.as_ref()).unwrap();
|
||||
|
||||
asg_circuit.fill_from_ast(circuit)?;
|
||||
|
||||
circuits.insert(name.clone(), asg_circuit);
|
||||
circuits.insert(name.name.to_string(), asg_circuit);
|
||||
}
|
||||
|
||||
Ok(Program {
|
||||
|
@ -29,7 +29,7 @@ pub struct Canonicalizer {
|
||||
// If we are in a circuit keep track of the circuit name.
|
||||
circuit_name: Option<Identifier>,
|
||||
in_circuit: bool,
|
||||
alias_lookup: Box<dyn Fn(String) -> Option<Alias>>,
|
||||
alias_lookup: Box<dyn Fn(Identifier) -> Option<Alias>>,
|
||||
}
|
||||
|
||||
impl AstPass for Canonicalizer {
|
||||
@ -41,11 +41,11 @@ impl AstPass for Canonicalizer {
|
||||
}
|
||||
|
||||
impl Canonicalizer {
|
||||
pub fn new(aliases: IndexMap<String, Alias>) -> Self {
|
||||
pub fn new(aliases: IndexMap<Identifier, Alias>) -> Self {
|
||||
Self {
|
||||
circuit_name: None,
|
||||
in_circuit: false,
|
||||
alias_lookup: Box::new(move |alias: String| -> Option<Alias> { aliases.get(&alias).cloned() }),
|
||||
alias_lookup: Box::new(move |alias: Identifier| -> Option<Alias> { aliases.get(&alias).cloned() }),
|
||||
}
|
||||
}
|
||||
|
||||
@ -501,7 +501,7 @@ impl ReconstructingReducer for Canonicalizer {
|
||||
Ok(array)
|
||||
}
|
||||
Type::CircuitOrAlias(identifier) => {
|
||||
if let Some(alias_type) = (self.alias_lookup)(identifier.name.to_string()) {
|
||||
if let Some(alias_type) = (self.alias_lookup)(identifier.clone()) {
|
||||
return self.reduce_type(type_, alias_type.represents, &alias_type.name.span);
|
||||
}
|
||||
|
||||
|
@ -125,9 +125,9 @@ where
|
||||
expected_input: Vec<FunctionInput>,
|
||||
import_statements: Vec<ImportStatement>,
|
||||
empty_imports: IndexMap<String, Program>,
|
||||
mut aliases: IndexMap<String, Alias>,
|
||||
mut circuits: IndexMap<String, Circuit>,
|
||||
mut functions: IndexMap<String, Function>,
|
||||
mut aliases: IndexMap<Identifier, Alias>,
|
||||
mut circuits: IndexMap<Identifier, Circuit>,
|
||||
mut functions: IndexMap<Identifier, Function>,
|
||||
mut global_consts: IndexMap<String, DefinitionStatement>,
|
||||
) -> Result<Program> {
|
||||
if !empty_imports.is_empty() {
|
||||
@ -160,7 +160,7 @@ where
|
||||
}
|
||||
|
||||
// TODO copyable AST.
|
||||
for (package, symbol, span) in imported_symbols.into_iter() {
|
||||
/* for (package, symbol, span) in imported_symbols.into_iter() {
|
||||
let pretty_package = package.join(".");
|
||||
|
||||
let resolved_package = resolved_packages
|
||||
@ -201,7 +201,7 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
Ok(Program {
|
||||
name: program.name.clone(),
|
||||
|
@ -17,7 +17,7 @@
|
||||
//! A Leo program consists of import, circuit, and function definitions.
|
||||
//! Each defined type consists of ast statements and expressions.
|
||||
|
||||
use crate::{Alias, Circuit, DefinitionStatement, Function, FunctionInput, ImportStatement};
|
||||
use crate::{Alias, Circuit, DefinitionStatement, Function, FunctionInput, Identifier, ImportStatement};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -30,10 +30,10 @@ pub struct Program {
|
||||
pub expected_input: Vec<FunctionInput>,
|
||||
pub import_statements: Vec<ImportStatement>,
|
||||
pub imports: IndexMap<String, Program>,
|
||||
pub aliases: IndexMap<String, Alias>,
|
||||
pub circuits: IndexMap<String, Circuit>,
|
||||
pub aliases: IndexMap<Identifier, Alias>,
|
||||
pub circuits: IndexMap<Identifier, Circuit>,
|
||||
pub global_consts: IndexMap<String, DefinitionStatement>,
|
||||
pub functions: IndexMap<String, Function>,
|
||||
pub functions: IndexMap<Identifier, Function>,
|
||||
}
|
||||
|
||||
impl AsRef<Program> for Program {
|
||||
|
@ -382,9 +382,9 @@ pub trait ReconstructingReducer {
|
||||
expected_input: Vec<FunctionInput>,
|
||||
import_statements: Vec<ImportStatement>,
|
||||
imports: IndexMap<String, Program>,
|
||||
aliases: IndexMap<String, Alias>,
|
||||
circuits: IndexMap<String, Circuit>,
|
||||
functions: IndexMap<String, Function>,
|
||||
aliases: IndexMap<Identifier, Alias>,
|
||||
circuits: IndexMap<Identifier, Circuit>,
|
||||
functions: IndexMap<Identifier, Function>,
|
||||
global_consts: IndexMap<String, DefinitionStatement>,
|
||||
) -> Result<Program> {
|
||||
Ok(Program {
|
||||
|
@ -50,12 +50,6 @@ impl ParserContext {
|
||||
}
|
||||
Token::Ident(ident) if ident.as_ref() == "test" => {
|
||||
return Err(ParserError::test_function(&token.span).into());
|
||||
// self.expect(Token::Test)?;
|
||||
// let (id, function) = self.parse_function_declaration()?;
|
||||
// tests.insert(id, TestFunction {
|
||||
// function,
|
||||
// input_file: None,
|
||||
// });
|
||||
}
|
||||
Token::Const => {
|
||||
let (name, global_const) = self.parse_global_const_declaration()?;
|
||||
@ -408,14 +402,14 @@ impl ParserContext {
|
||||
/// Returns an [`(Identifier, Circuit)`] tuple of AST nodes if the next tokens represent a
|
||||
/// circuit name and definition statement.
|
||||
///
|
||||
pub fn parse_circuit(&mut self) -> Result<(String, Circuit)> {
|
||||
pub fn parse_circuit(&mut self) -> Result<(Identifier, Circuit)> {
|
||||
self.expect(Token::Circuit)?;
|
||||
let name = self.expect_ident()?;
|
||||
self.expect(Token::LeftCurly)?;
|
||||
let members = self.parse_circuit_declaration()?;
|
||||
|
||||
Ok((
|
||||
name.name.to_string(),
|
||||
name.clone(),
|
||||
Circuit {
|
||||
circuit_name: name,
|
||||
core_mapping: std::cell::RefCell::new(None),
|
||||
@ -473,7 +467,7 @@ impl ParserContext {
|
||||
/// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name
|
||||
/// and function definition.
|
||||
///
|
||||
pub fn parse_function_declaration(&mut self) -> Result<(String, Function)> {
|
||||
pub fn parse_function_declaration(&mut self) -> Result<(Identifier, Function)> {
|
||||
let mut annotations = Vec::new();
|
||||
while self.peek_token().as_ref() == &Token::At {
|
||||
annotations.push(self.parse_annotation()?);
|
||||
@ -497,7 +491,7 @@ impl ParserContext {
|
||||
};
|
||||
let block = self.parse_block()?;
|
||||
Ok((
|
||||
name.name.to_string(),
|
||||
name.clone(),
|
||||
Function {
|
||||
annotations,
|
||||
identifier: name,
|
||||
@ -529,7 +523,7 @@ impl ParserContext {
|
||||
/// Returns an [`(String, Alias)`] AST node if the next tokens represent a global
|
||||
/// const definition statement and assignment.
|
||||
///
|
||||
pub fn parse_type_alias(&mut self) -> Result<(String, Alias)> {
|
||||
pub fn parse_type_alias(&mut self) -> Result<(Identifier, Alias)> {
|
||||
self.expect(Token::Type)?;
|
||||
let name = self.expect_ident()?;
|
||||
self.expect(Token::Assign)?;
|
||||
@ -537,7 +531,7 @@ impl ParserContext {
|
||||
self.expect(Token::Semicolon)?;
|
||||
|
||||
Ok((
|
||||
name.name.to_string(),
|
||||
name.clone(),
|
||||
Alias {
|
||||
represents: type_,
|
||||
name,
|
||||
|
Loading…
Reference in New Issue
Block a user