mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 03:04:13 +03:00
More fixes
This commit is contained in:
parent
047ed7a08b
commit
028a01efde
@ -327,8 +327,7 @@ pub trait ProgramReconstructor: StatementReconstructor {
|
||||
|
||||
fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
|
||||
ProgramScope {
|
||||
name: input.name,
|
||||
network: input.network,
|
||||
program_id: input.program_id,
|
||||
structs: input
|
||||
.structs
|
||||
.into_iter()
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
//! A Leo program consists of import statements and program scopes.
|
||||
|
||||
pub mod program_id;
|
||||
pub use program_id::*;
|
||||
|
||||
pub mod program_scope;
|
||||
pub use program_scope::*;
|
||||
|
||||
@ -31,7 +34,7 @@ pub struct Program {
|
||||
/// A map from import names to import definitions.
|
||||
pub imports: IndexMap<Identifier, Program>,
|
||||
/// A map from program names to program scopes.
|
||||
pub program_scopes: IndexMap<(Identifier, Identifier), ProgramScope>,
|
||||
pub program_scopes: IndexMap<ProgramId, ProgramScope>,
|
||||
}
|
||||
|
||||
impl fmt::Display for Program {
|
||||
|
95
compiler/ast/src/program/program_id.rs
Normal file
95
compiler/ast/src/program/program_id.rs
Normal file
@ -0,0 +1,95 @@
|
||||
// Copyright (C) 2019-2022 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/>.
|
||||
|
||||
use crate::Identifier;
|
||||
|
||||
use core::fmt;
|
||||
use serde::de::Visitor;
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// An identifier for a program that is eventually deployed to the network.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct ProgramId {
|
||||
/// The name of the program.
|
||||
pub name: Identifier,
|
||||
/// The network associated with the program.
|
||||
pub network: Identifier,
|
||||
}
|
||||
|
||||
impl fmt::Display for ProgramId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}.{}", self.name, self.network)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for ProgramId {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
// Converts an element that implements Serialize into a string.
|
||||
fn to_json_string<E: Serialize, Error: serde::ser::Error>(element: &E) -> Result<String, Error> {
|
||||
serde_json::to_string(&element).map_err(|e| Error::custom(e.to_string()))
|
||||
}
|
||||
|
||||
// Load the struct elements into a BTreeMap (to preserve serialized ordering of keys).
|
||||
let mut key: BTreeMap<String, String> = BTreeMap::new();
|
||||
key.insert("name".to_string(), self.name.to_string());
|
||||
key.insert("network".to_string(), to_json_string(&self.network)?);
|
||||
|
||||
// Convert the serialized object into a string for use as a key.
|
||||
serializer.serialize_str(&to_json_string(&key)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ProgramId {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
struct ProgramIdVisitor;
|
||||
|
||||
impl Visitor<'_> for ProgramIdVisitor {
|
||||
type Value = ProgramId;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a string encoding the ast ProgramId struct")
|
||||
}
|
||||
|
||||
/// Implementation for recovering a string that serializes Identifier.
|
||||
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
|
||||
// Converts a serialized string into an element that implements Deserialize.
|
||||
fn to_json_string<'a, D: Deserialize<'a>, Error: serde::de::Error>(
|
||||
serialized: &'a str,
|
||||
) -> Result<D, Error> {
|
||||
serde_json::from_str::<'a>(serialized).map_err(|e| Error::custom(e.to_string()))
|
||||
}
|
||||
|
||||
// Convert the serialized string into a BTreeMap to recover ProgramId.
|
||||
let key: BTreeMap<String, String> = to_json_string(value)?;
|
||||
|
||||
let name: Identifier = match key.get("name") {
|
||||
Some(name) => to_json_string(name)?,
|
||||
None => return Err(E::custom("missing 'name' in serialized ProgramId struct")),
|
||||
};
|
||||
|
||||
let network: Identifier = match key.get("network") {
|
||||
Some(network) => to_json_string(network)?,
|
||||
None => return Err(E::custom("missing 'network' in serialized ProgramId struct")),
|
||||
};
|
||||
|
||||
Ok(ProgramId { name, network })
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(ProgramIdVisitor)
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
|
||||
//! A Leo program scope consists of struct, function, and mapping definitions.
|
||||
|
||||
use crate::{Function, Identifier, Mapping, Struct};
|
||||
use crate::{Function, Identifier, Mapping, ProgramId, Struct};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use leo_span::Span;
|
||||
@ -26,10 +26,8 @@ use std::fmt;
|
||||
/// Stores the Leo program scope abstract syntax tree.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ProgramScope {
|
||||
/// The name of the program scope.
|
||||
pub name: Identifier,
|
||||
/// The network of the program scope.
|
||||
pub network: Identifier,
|
||||
/// The program id of the program scope.
|
||||
pub program_id: ProgramId,
|
||||
/// A map from struct names to struct definitions.
|
||||
pub structs: IndexMap<Identifier, Struct>,
|
||||
/// A map from mapping names to mapping definitions.
|
||||
@ -42,7 +40,7 @@ pub struct ProgramScope {
|
||||
|
||||
impl fmt::Display for ProgramScope {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
writeln!(f, "program {}.{} {{", self.name, self.network)?;
|
||||
writeln!(f, "program {} {{", self.program_id)?;
|
||||
for (_, struct_) in self.structs.iter() {
|
||||
writeln!(f, " {}", struct_)?;
|
||||
}
|
||||
|
@ -101,13 +101,12 @@ impl<'a> Compiler<'a> {
|
||||
// Note that parsing enforces that there is exactly one program scope in a file.
|
||||
// TODO: Clean up check.
|
||||
let program_scope = self.ast.ast.program_scopes.values().next().unwrap();
|
||||
let program_scope_name =
|
||||
with_session_globals(|s| program_scope.name.name.as_str(s, |string| string.to_string()));
|
||||
let program_scope_name = format!("{}", program_scope.program_id.name);
|
||||
if program_scope_name != self.program_name {
|
||||
return Err(CompilerError::program_scope_name_does_not_match(
|
||||
program_scope_name,
|
||||
self.program_name.clone(),
|
||||
program_scope.name.span,
|
||||
program_scope.program_id.name.span,
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
@ -44,11 +44,11 @@ impl ParserContext<'_> {
|
||||
false => {
|
||||
parsed_program_scope = true;
|
||||
let program_scope = self.parse_program_scope()?;
|
||||
program_scopes.insert((program_scope.name, program_scope.network), program_scope);
|
||||
program_scopes.insert(program_scope.program_id, program_scope);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Err(Self::unexpected_item(&self.token).into()),
|
||||
_ => return Err(Self::unexpected_item(&self.token, &[Token::Import, Token::Program]).into()),
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,10 +63,10 @@ impl ParserContext<'_> {
|
||||
})
|
||||
}
|
||||
|
||||
fn unexpected_item(token: &SpannedToken) -> ParserError {
|
||||
fn unexpected_item(token: &SpannedToken, expected: &[Token]) -> ParserError {
|
||||
ParserError::unexpected(
|
||||
&token.token,
|
||||
[Token::Function, Token::Struct, Token::Identifier(sym::test)]
|
||||
expected
|
||||
.iter()
|
||||
.map(|x| format!("'{}'", x))
|
||||
.collect::<Vec<_>>()
|
||||
@ -133,6 +133,9 @@ impl ParserContext<'_> {
|
||||
self.expect(&Token::Dot)?;
|
||||
let network = self.expect_identifier()?;
|
||||
|
||||
// Construct the program id.
|
||||
let program_id = ProgramId { name, network };
|
||||
|
||||
// Check that the program network is valid.
|
||||
if network.name != sym::aleo {
|
||||
return Err(ParserError::invalid_network(network.span).into());
|
||||
@ -162,7 +165,20 @@ impl ParserContext<'_> {
|
||||
}
|
||||
Token::Circuit => return Err(ParserError::circuit_is_deprecated(self.token.span).into()),
|
||||
Token::RightCurly => break,
|
||||
_ => return Err(Self::unexpected_item(&self.token).into()),
|
||||
_ => {
|
||||
return Err(Self::unexpected_item(
|
||||
&self.token,
|
||||
&[
|
||||
Token::Struct,
|
||||
Token::Record,
|
||||
Token::Mapping,
|
||||
Token::At,
|
||||
Token::Function,
|
||||
Token::Transition,
|
||||
],
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,8 +186,7 @@ impl ParserContext<'_> {
|
||||
let end = self.expect(&Token::RightCurly)?;
|
||||
|
||||
Ok(ProgramScope {
|
||||
name,
|
||||
network,
|
||||
program_id,
|
||||
functions,
|
||||
structs,
|
||||
mappings,
|
||||
@ -380,7 +395,13 @@ impl ParserContext<'_> {
|
||||
fn parse_annotation(&mut self) -> Result<Annotation> {
|
||||
// Parse the `@` symbol and identifier.
|
||||
let start = self.expect(&Token::At)?;
|
||||
let identifier = self.expect_identifier()?;
|
||||
let identifier = match self.token.token {
|
||||
Token::Program => Identifier {
|
||||
name: sym::program,
|
||||
span: self.expect(&Token::Program)?,
|
||||
},
|
||||
_ => self.expect_identifier()?,
|
||||
};
|
||||
let span = start + identifier.span;
|
||||
|
||||
// TODO: Verify that this check is sound.
|
||||
|
@ -47,12 +47,8 @@ impl<'a> CodeGenerator<'a> {
|
||||
let program_scope: &ProgramScope = input.program_scopes.values().next().unwrap();
|
||||
|
||||
// Print the program id.
|
||||
writeln!(
|
||||
program_string,
|
||||
"program {}.{};",
|
||||
program_scope.name, program_scope.network
|
||||
)
|
||||
.expect("Failed to write program id to string.");
|
||||
writeln!(program_string, "program {};", program_scope.program_id)
|
||||
.expect("Failed to write program id to string.");
|
||||
|
||||
// Newline separator.
|
||||
program_string.push('\n');
|
||||
|
@ -92,8 +92,7 @@ impl ProgramScopeConsumer for StaticSingleAssigner {
|
||||
|
||||
fn consume_program_scope(&mut self, input: ProgramScope) -> Self::Output {
|
||||
ProgramScope {
|
||||
name: input.name,
|
||||
network: input.network,
|
||||
program_id: input.program_id,
|
||||
structs: input.structs,
|
||||
mappings: input.mappings,
|
||||
functions: input
|
||||
|
Loading…
Reference in New Issue
Block a user