WIP ast changes for program scope

This commit is contained in:
Pranav Gaddamadugu 2022-10-05 09:56:36 -07:00
parent 8733887d55
commit bd4b7be8cf
5 changed files with 19 additions and 83 deletions

View File

@ -74,7 +74,7 @@ use leo_errors::{AstError, Result};
/// These data types form a tree that begins from a [`Program`] type root.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Ast {
ast: Program,
pub ast: Program,
}
impl Ast {

View File

@ -1,81 +0,0 @@
// 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/>.
//! A Leo program consists of import, struct, and function definitions.
//! Each defined type consists of ast statements and expressions.
use crate::{Function, FunctionInput, Identifier, Mapping, Struct};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt;
/// Stores the Leo program abstract syntax tree.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Program {
/// The name of the program.
pub name: String,
/// The network of the program.
pub network: String,
/// Expected main function inputs.
/// Empty after parsing.
pub expected_input: Vec<FunctionInput>,
/// A map from import names to import definitions.
pub imports: IndexMap<Identifier, Program>,
/// A map from struct names to struct definitions.
pub structs: IndexMap<Identifier, Struct>,
/// A map from mapping names to mapping definitions.
pub mappings: IndexMap<Identifier, Mapping>,
/// A map from function names to function definitions.
pub functions: IndexMap<Identifier, Function>,
}
impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (id, _import) in self.imports.iter() {
writeln!(f, "import {}.leo;", id)?;
}
for (_, struct_) in self.structs.iter() {
struct_.fmt(f)?;
writeln!(f,)?;
}
for (_, mapping) in self.mappings.iter() {
mapping.fmt(f)?;
writeln!(f,)?;
}
for (_, function) in self.functions.iter() {
function.fmt(f)?;
writeln!(f,)?;
}
write!(f, "")
}
}
impl Default for Program {
/// Constructs an empty program node.
fn default() -> Self {
Self {
name: String::new(),
network: String::new(),
expected_input: vec![],
imports: IndexMap::new(),
structs: IndexMap::new(),
mappings: IndexMap::new(),
functions: IndexMap::new(),
}
}
}

View File

@ -17,7 +17,7 @@
//! The compiler for Leo programs.
//!
//! The [`Compiler`] type compiles Leo programs into R1CS circuits.
use leo_ast::Program;
use leo_ast::{Node, Program};
pub use leo_ast::{Ast, InputAst};
use leo_errors::emitter::Handler;
use leo_errors::{CompilerError, Result};
@ -97,6 +97,16 @@ impl<'a> Compiler<'a> {
// Use the parser to construct the abstract syntax tree (ast).
self.ast = leo_parser::parse_ast(self.handler, &prg_sf.src, prg_sf.start_pos)?;
// Check that the name associated with the program scope matches file name.
if with_session_globals(|s| self.ast.ast.name.name.as_str(s, |s| s != &self.program_name)) {
return Err(CompilerError::program_name_should_match_file_name(
&self.ast.ast.name,
&self.program_name,
self.ast.ast.name.span(),
)
.into());
}
if self.output_options.initial_ast {
self.write_ast_to_json("initial_ast.json")?;
}

View File

@ -56,4 +56,11 @@ create_messages!(
msg: format!("Failed to open current working directory. Error: {err}"),
help: None,
}
@formatted
program_name_should_match_file_name {
args: (program_name: impl Display, file_name: impl Display),
msg: format!("Program name `{program_name}` should match file name `{file_name}`"),
help: None,
}
);