test canonicalization

This commit is contained in:
gluax 2022-02-16 10:10:26 -08:00
parent 2ea3709441
commit 1377d60f07
2 changed files with 13 additions and 3 deletions

View File

@ -22,6 +22,7 @@
#![allow(clippy::upper_case_acronyms)] #![allow(clippy::upper_case_acronyms)]
#![doc = include_str!("../README.md")] #![doc = include_str!("../README.md")]
use leo_ast::AstPass;
use leo_errors::emitter::Handler; use leo_errors::emitter::Handler;
use leo_errors::{CompilerError, Result}; use leo_errors::{CompilerError, Result};
use leo_span::symbol::create_session_if_not_set_then; use leo_span::symbol::create_session_if_not_set_then;
@ -34,16 +35,18 @@ use std::path::PathBuf;
pub struct Compiler<'a> { pub struct Compiler<'a> {
handler: &'a Handler, handler: &'a Handler,
main_file_path: PathBuf, main_file_path: PathBuf,
output_directory: PathBuf,
} }
impl<'a> Compiler<'a> { impl<'a> Compiler<'a> {
/// ///
/// Returns a new Leo compiler. /// Returns a new Leo compiler.
/// ///
pub fn new(handler: &'a Handler, main_file_path: PathBuf) -> Self { pub fn new(handler: &'a Handler, main_file_path: PathBuf, output_directory: PathBuf) -> Self {
Self { Self {
handler, handler,
main_file_path, main_file_path,
output_directory,
} }
} }
@ -72,11 +75,18 @@ impl<'a> Compiler<'a> {
.map_err(|e| CompilerError::file_read_error(self.main_file_path.clone(), e))?; .map_err(|e| CompilerError::file_read_error(self.main_file_path.clone(), e))?;
// Use the parser to construct the abstract syntax tree (ast). // Use the parser to construct the abstract syntax tree (ast).
let ast: leo_ast::Ast = leo_parser::parse_ast( let mut ast: leo_ast::Ast = leo_parser::parse_ast(
self.handler, self.handler,
self.main_file_path.to_str().unwrap_or_default(), self.main_file_path.to_str().unwrap_or_default(),
program_string, program_string,
)?; )?;
// Write the AST snapshot post parsing.
ast.to_json_file_without_keys(self.output_directory.clone(), "inital_ast.json", &["span"])?;
// Canonicalize the AST.
ast = leo_ast_passes::Canonicalizer::do_pass(Default::default(), ast.into_repr())?;
// Write the AST snapshot post parsing
ast.to_json_file_without_keys(self.output_directory.clone(), "canonicalization_ast.json", &["span"])?;
Ok(ast) Ok(ast)
} }

View File

@ -178,7 +178,7 @@ impl Command for Build {
// Initialize error handler // Initialize error handler
let handler = leo_errors::emitter::Handler::default(); let handler = leo_errors::emitter::Handler::default();
let program = Compiler::new(&handler, main_file_path); let program = Compiler::new(&handler, main_file_path, output_directory);
// Compute the current program checksum // Compute the current program checksum
let program_checksum = program.checksum()?; let program_checksum = program.checksum()?;