mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 07:48:04 +03:00
rename static-check -> symbol-table 2
This commit is contained in:
parent
20ac83265d
commit
b5a05be09d
@ -29,7 +29,7 @@ use leo_imports::ImportParser;
|
||||
use leo_input::LeoInputParser;
|
||||
use leo_package::inputs::InputPairs;
|
||||
use leo_state::verify_local_data_commitment;
|
||||
use leo_static_check::{StaticCheck, SymbolTable};
|
||||
use leo_static_check::SymbolTable;
|
||||
use leo_typed::{Input, LeoTypedAst, MainInput, Program};
|
||||
|
||||
use snarkos_dpc::{base_dpc::instantiated::Components, SystemParameters};
|
||||
@ -192,7 +192,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
pub(crate) fn check_program(&self) -> Result<(), CompilerError> {
|
||||
// Create a new symbol table from the program, imported_programs, and program_input.
|
||||
let symbol_table =
|
||||
SymbolTable::run(&self.program, &self.imported_programs, &self.program_input).map_err(|mut e| {
|
||||
SymbolTable::new(&self.program, &self.imported_programs, &self.program_input).map_err(|mut e| {
|
||||
e.set_path(&self.main_file_path);
|
||||
|
||||
e
|
||||
@ -233,7 +233,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
self.imported_programs = ImportParser::parse(&self.program)?;
|
||||
|
||||
// Run static check on program.
|
||||
let symbol_table = StaticCheck::new(&self.program, &self.imported_programs, &self.program_input)?;
|
||||
let symbol_table = SymbolTable::new(&self.program, &self.imported_programs, &self.program_input)?;
|
||||
|
||||
// Run dynamic check on program.
|
||||
DynamicCheck::new(&self.program, symbol_table)?;
|
||||
|
@ -16,13 +16,13 @@
|
||||
|
||||
use crate::errors::{FunctionError, ImportError, OutputBytesError, OutputFileError};
|
||||
use leo_ast::ParserError;
|
||||
use leo_dynamic_check::DynamicCheckError;
|
||||
use leo_imports::ImportParserError;
|
||||
use leo_input::InputParserError;
|
||||
use leo_state::LocalDataVerificationError;
|
||||
use leo_static_check::SymbolTableError;
|
||||
|
||||
use bincode::Error as SerdeError;
|
||||
use leo_dynamic_check::DynamicCheckError;
|
||||
use leo_static_check::{StaticCheckError, SymbolTableError};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@ -72,9 +72,6 @@ pub enum CompilerError {
|
||||
#[error("{}", _0)]
|
||||
SerdeError(#[from] SerdeError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
StaticCheckError(#[from] StaticCheckError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
SymbolTableError(#[from] SymbolTableError),
|
||||
}
|
||||
|
@ -16,14 +16,14 @@
|
||||
|
||||
pub mod packages;
|
||||
|
||||
use crate::{assert_satisfied, expect_static_check_error, parse_program};
|
||||
use crate::{assert_satisfied, expect_symbol_table_error, parse_program};
|
||||
|
||||
#[test]
|
||||
fn test_core_circuit_invalid() {
|
||||
let program_bytes = include_bytes!("core_package_invalid.leo");
|
||||
let program = parse_program(program_bytes).err().unwrap();
|
||||
|
||||
expect_static_check_error(program);
|
||||
expect_symbol_table_error(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -31,7 +31,7 @@ fn test_core_circuit_star_fail() {
|
||||
let program_bytes = include_bytes!("core_circuit_star_fail.leo");
|
||||
let error = parse_program(program_bytes).err().unwrap();
|
||||
|
||||
expect_static_check_error(error);
|
||||
expect_symbol_table_error(error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -39,7 +39,7 @@ fn test_core_package_invalid() {
|
||||
let program_bytes = include_bytes!("core_package_invalid.leo");
|
||||
let error = parse_program(program_bytes).err().unwrap();
|
||||
|
||||
expect_static_check_error(error);
|
||||
expect_symbol_table_error(error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -47,7 +47,7 @@ fn test_core_unstable_package_invalid() {
|
||||
let program_bytes = include_bytes!("core_unstable_package_invalid.leo");
|
||||
let error = parse_program(program_bytes).err().unwrap();
|
||||
|
||||
expect_static_check_error(error);
|
||||
expect_symbol_table_error(error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -185,8 +185,8 @@ pub(crate) fn expect_dynamic_check_error(error: CompilerError) {
|
||||
assert!(matches!(error, CompilerError::DynamicCheckError(_)))
|
||||
}
|
||||
|
||||
pub(crate) fn expect_static_check_error(error: CompilerError) {
|
||||
assert!(matches!(error, CompilerError::StaticCheckError(_)))
|
||||
pub(crate) fn expect_symbol_table_error(error: CompilerError) {
|
||||
assert!(matches!(error, CompilerError::SymbolTableError(_)))
|
||||
}
|
||||
|
||||
pub(crate) fn generate_main_input(input: Vec<(&str, Option<InputValue>)>) -> MainInput {
|
||||
|
@ -24,7 +24,7 @@ use leo_ast::LeoAst;
|
||||
use leo_dynamic_check::DynamicCheck;
|
||||
|
||||
use leo_imports::ImportParser;
|
||||
use leo_static_check::{StaticCheck, SymbolTable};
|
||||
use leo_static_check::SymbolTable;
|
||||
use leo_typed::{Input, LeoTypedAst, Program};
|
||||
use std::path::PathBuf;
|
||||
|
||||
@ -58,8 +58,8 @@ impl TestDynamicCheck {
|
||||
// Create empty input.
|
||||
let input = Input::new();
|
||||
|
||||
// Create static check.
|
||||
let symbol_table = StaticCheck::new(&program, &import_parser, &input).unwrap();
|
||||
// Create symbol table.
|
||||
let symbol_table = SymbolTable::new(&program, &import_parser, &input).unwrap();
|
||||
|
||||
// Store fields for new dynamic check.
|
||||
Self { program, symbol_table }
|
||||
|
@ -14,9 +14,6 @@
|
||||
// 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/>.
|
||||
|
||||
pub mod static_check;
|
||||
pub use self::static_check::*;
|
||||
|
||||
pub mod symbol_table;
|
||||
pub use self::symbol_table::*;
|
||||
|
||||
|
@ -1,42 +0,0 @@
|
||||
// Copyright (C) 2019-2020 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::SymbolTableError;
|
||||
use leo_typed::Error as FormattedError;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/// Errors encountered when tracking variable, function and circuit names in a program.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum StaticCheckError {
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
SymbolTableError(#[from] SymbolTableError),
|
||||
}
|
||||
|
||||
impl StaticCheckError {
|
||||
///
|
||||
/// Sets the filepath for the error stacktrace.
|
||||
///
|
||||
pub fn set_path(&mut self, path: &Path) {
|
||||
match self {
|
||||
StaticCheckError::Error(error) => error.set_path(path),
|
||||
StaticCheckError::SymbolTableError(error) => error.set_path(path),
|
||||
}
|
||||
}
|
||||
}
|
@ -26,11 +26,8 @@ pub use self::errors::*;
|
||||
pub mod imports;
|
||||
pub use self::imports::*;
|
||||
|
||||
pub mod objects;
|
||||
pub use self::objects::*;
|
||||
|
||||
pub mod static_check;
|
||||
pub use self::static_check::*;
|
||||
pub mod symbol_table;
|
||||
pub use self::symbol_table::*;
|
||||
|
||||
pub mod types;
|
||||
pub use self::types::*;
|
||||
|
@ -1,18 +0,0 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
pub mod symbol_table;
|
||||
pub use self::symbol_table::*;
|
@ -1,100 +0,0 @@
|
||||
// Copyright (C) 2019-2020 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::{StaticCheckError, SymbolTable};
|
||||
use leo_imports::ImportParser;
|
||||
use leo_typed::{Input, Program};
|
||||
|
||||
/// Performs a static type check over a program.
|
||||
pub struct StaticCheck {
|
||||
table: SymbolTable,
|
||||
}
|
||||
|
||||
impl StaticCheck {
|
||||
///
|
||||
/// Returns a new `SymbolTable` from a given program, input, and import parser.
|
||||
///
|
||||
/// Runs pass one name checks and pass two type checks.
|
||||
/// Builds a symbol table of circuit and function types to be used in the dynamic check.
|
||||
///
|
||||
pub fn new(
|
||||
program: &Program,
|
||||
import_parser: &ImportParser,
|
||||
input: &Input,
|
||||
) -> Result<SymbolTable, StaticCheckError> {
|
||||
let mut check = Self::default();
|
||||
|
||||
// Run checks on program, imports, and input.
|
||||
check.check(program, import_parser, input)?;
|
||||
|
||||
// Return the symbol table of types.
|
||||
Ok(check.table)
|
||||
}
|
||||
|
||||
///
|
||||
/// Computes pass one and pass two checks on self.
|
||||
///
|
||||
pub fn check(
|
||||
&mut self,
|
||||
program: &Program,
|
||||
import_parser: &ImportParser,
|
||||
input: &Input,
|
||||
) -> Result<(), StaticCheckError> {
|
||||
// Insert input types.
|
||||
self.table
|
||||
.insert_input(input)
|
||||
.map_err(|err| StaticCheckError::SymbolTableError(err))?;
|
||||
|
||||
// Run pass one checks.
|
||||
self.pass_one(program, import_parser)?;
|
||||
|
||||
// Run pass two checks.
|
||||
self.pass_two(program)
|
||||
}
|
||||
|
||||
///
|
||||
/// Checks for duplicate circuit and function names given an unresolved program.
|
||||
///
|
||||
/// If a circuit or function name has no duplicates, then it is inserted into the symbol table.
|
||||
/// Variables defined later in the unresolved program cannot have the same name.
|
||||
///
|
||||
pub fn pass_one(&mut self, program: &Program, import_parser: &ImportParser) -> Result<(), StaticCheckError> {
|
||||
self.table
|
||||
.check_names(program, import_parser)
|
||||
.map_err(|err| StaticCheckError::SymbolTableError(err))
|
||||
}
|
||||
|
||||
///
|
||||
/// Checks for unknown types in circuit and function definitions given an unresolved program.
|
||||
///
|
||||
/// If a circuit or function definition only contains known types, then it is inserted into the
|
||||
/// symbol table. Variables defined later in the unresolved program can lookup the definition and
|
||||
/// refer to its expected types.
|
||||
///
|
||||
pub fn pass_two(&mut self, program: &Program) -> Result<(), StaticCheckError> {
|
||||
self.table
|
||||
.check_types(program)
|
||||
.map_err(|err| StaticCheckError::SymbolTableError(err))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for StaticCheck {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
table: SymbolTable::new(None),
|
||||
}
|
||||
}
|
||||
}
|
@ -49,18 +49,6 @@ pub struct SymbolTable {
|
||||
}
|
||||
|
||||
impl SymbolTable {
|
||||
///
|
||||
/// Creates a new symbol table with a given parent symbol table.
|
||||
///
|
||||
pub fn new(parent: Option<Box<SymbolTable>>) -> Self {
|
||||
SymbolTable {
|
||||
names: HashMap::new(),
|
||||
circuits: HashMap::new(),
|
||||
functions: HashMap::new(),
|
||||
parent,
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a new `SymbolTable` from a given, program, imported programs, and program input.
|
||||
///
|
||||
@ -69,8 +57,7 @@ impl SymbolTable {
|
||||
///
|
||||
/// Checks that each circuit or function definition contains valid types.
|
||||
///
|
||||
///
|
||||
pub fn run(
|
||||
pub fn new(
|
||||
program: &Program,
|
||||
import_parser: &ImportParser,
|
||||
input: &Input,
|
@ -17,7 +17,7 @@
|
||||
pub mod symbol_table;
|
||||
|
||||
use leo_ast::LeoAst;
|
||||
use leo_static_check::{StaticCheck, StaticCheckError, SymbolTableError};
|
||||
use leo_static_check::{SymbolTable, SymbolTableError};
|
||||
use leo_typed::{Input, LeoTypedAst};
|
||||
|
||||
use leo_imports::ImportParser;
|
||||
@ -26,11 +26,11 @@ use std::path::PathBuf;
|
||||
const TEST_PROGRAM_PATH: &str = "";
|
||||
|
||||
/// A helper struct to test a `SymbolTable`.
|
||||
pub struct TestStaticCheck {
|
||||
pub struct TestSymbolTable {
|
||||
typed: LeoTypedAst,
|
||||
}
|
||||
|
||||
impl TestStaticCheck {
|
||||
impl TestSymbolTable {
|
||||
///
|
||||
/// Returns a typed syntax tree given a Leo program.
|
||||
///
|
||||
@ -66,7 +66,7 @@ impl TestStaticCheck {
|
||||
let input = Input::new();
|
||||
|
||||
// Create new symbol table.
|
||||
let _symbol_table = StaticCheck::new(&program, &import_parser, &input).unwrap();
|
||||
let _symbol_table = SymbolTable::new(&program, &import_parser, &input).unwrap();
|
||||
}
|
||||
|
||||
///
|
||||
@ -79,16 +79,16 @@ impl TestStaticCheck {
|
||||
let program = self.typed.into_repr();
|
||||
|
||||
// Create new symbol table.
|
||||
let static_check = &mut StaticCheck::default();
|
||||
let static_check = &mut SymbolTable::default();
|
||||
|
||||
// Create empty import parser.
|
||||
let import_parser = ImportParser::new();
|
||||
|
||||
// Run pass one and expect an error.
|
||||
let error = static_check.pass_one(&program, &import_parser).unwrap_err();
|
||||
let error = static_check.check_names(&program, &import_parser).unwrap_err();
|
||||
|
||||
match error {
|
||||
StaticCheckError::SymbolTableError(SymbolTableError::Error(_)) => {} // Ok
|
||||
SymbolTableError::Error(_) => {} // Ok
|
||||
error => panic!("Expected a symbol table error found `{}`", error),
|
||||
}
|
||||
}
|
||||
@ -103,19 +103,19 @@ impl TestStaticCheck {
|
||||
let program = self.typed.into_repr();
|
||||
|
||||
// Create a new symbol table.
|
||||
let static_check = &mut StaticCheck::default();
|
||||
let static_check = &mut SymbolTable::default();
|
||||
|
||||
// Create empty import parser.
|
||||
let import_parser = ImportParser::new();
|
||||
|
||||
// Run the pass one and expect no errors.
|
||||
static_check.pass_one(&program, &import_parser).unwrap();
|
||||
static_check.check_names(&program, &import_parser).unwrap();
|
||||
|
||||
// Run the pass two and expect and error.
|
||||
let error = static_check.pass_two(&program).unwrap_err();
|
||||
let error = static_check.check_types(&program).unwrap_err();
|
||||
|
||||
match error {
|
||||
StaticCheckError::SymbolTableError(SymbolTableError::TypeError(_)) => {} //Ok
|
||||
SymbolTableError::TypeError(_) => {} //Ok
|
||||
error => panic!("Expected a type error found `{}`", error),
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
// 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::TestStaticCheck;
|
||||
use crate::TestSymbolTable;
|
||||
|
||||
///
|
||||
/// Defines a circuit `Foo {}`.
|
||||
@ -23,7 +23,7 @@ use crate::TestStaticCheck;
|
||||
#[test]
|
||||
fn test_duplicate_circuit() {
|
||||
let program_bytes = include_bytes!("duplicate_circuit.leo");
|
||||
let resolver = TestStaticCheck::new(program_bytes);
|
||||
let resolver = TestSymbolTable::new(program_bytes);
|
||||
|
||||
resolver.expect_pass_one_error();
|
||||
}
|
||||
@ -38,7 +38,7 @@ fn test_duplicate_circuit() {
|
||||
#[test]
|
||||
fn test_duplicate_function() {
|
||||
let program_bytes = include_bytes!("duplicate_function.leo");
|
||||
let resolver = TestStaticCheck::new(program_bytes);
|
||||
let resolver = TestSymbolTable::new(program_bytes);
|
||||
|
||||
resolver.expect_pass_one_error();
|
||||
}
|
||||
@ -52,7 +52,7 @@ fn test_duplicate_function() {
|
||||
#[test]
|
||||
fn test_self_not_available() {
|
||||
let program_bytes = include_bytes!("self_not_available.leo");
|
||||
let resolver = TestStaticCheck::new(program_bytes);
|
||||
let resolver = TestSymbolTable::new(program_bytes);
|
||||
|
||||
resolver.expect_pass_two_error();
|
||||
}
|
||||
@ -66,7 +66,7 @@ fn test_self_not_available() {
|
||||
#[test]
|
||||
fn test_undefined_circuit() {
|
||||
let program_bytes = include_bytes!("undefined_circuit.leo");
|
||||
let resolver = TestStaticCheck::new(program_bytes);
|
||||
let resolver = TestSymbolTable::new(program_bytes);
|
||||
|
||||
resolver.expect_pass_two_error();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user