From f754f89c6e8404673a440536b9e8c960017bd4f3 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Thu, 23 Sep 2021 08:57:09 -0700 Subject: [PATCH] Updated test-framework/.../tgc.rs to remove spans from AST; changed compiler test options to disable spans --- compiler/src/compiler.rs | 24 ++++++++++---------- compiler/src/test.rs | 2 +- test-framework/src/bin/tgc.rs | 42 +++++++++++++---------------------- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index b674d3e1d7..2f64e4eab2 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -246,10 +246,10 @@ impl<'a, F: PrimeField, G: GroupType> Compiler<'a, F, G> { let mut ast: leo_ast::Ast = parse_ast(self.main_file_path.to_str().unwrap_or_default(), program_string)?; if self.ast_snapshot_options.initial { - if !self.ast_snapshot_options.spans_enabled { - ast.to_json_file_without_keys(self.output_directory.clone(), "initial_ast.json", &["span"])?; - } else { + if self.ast_snapshot_options.spans_enabled { ast.to_json_file(self.output_directory.clone(), "initial_ast.json")?; + } else { + ast.to_json_file_without_keys(self.output_directory.clone(), "initial_ast.json", &["span"])?; } } @@ -260,10 +260,10 @@ impl<'a, F: PrimeField, G: GroupType> Compiler<'a, F, G> { )?; if self.ast_snapshot_options.imports_resolved { - if !self.ast_snapshot_options.spans_enabled { - ast.to_json_file_without_keys(self.output_directory.clone(), "imports_resolved.json", &["span"])?; - } else { + if self.ast_snapshot_options.spans_enabled { ast.to_json_file(self.output_directory.clone(), "imports_resolved_ast.json")?; + } else { + ast.to_json_file_without_keys(self.output_directory.clone(), "imports_resolved_ast.json", &["span"])?; } } @@ -271,10 +271,10 @@ impl<'a, F: PrimeField, G: GroupType> Compiler<'a, F, G> { ast = leo_ast_passes::Canonicalizer::do_pass(ast.into_repr())?; if self.ast_snapshot_options.canonicalized { - if !self.ast_snapshot_options.spans_enabled { - ast.to_json_file_without_keys(self.output_directory.clone(), "canonicalization_ast.json", &["span"])?; - } else { + if self.ast_snapshot_options.spans_enabled { ast.to_json_file(self.output_directory.clone(), "canonicalization_ast.json")?; + } else { + ast.to_json_file_without_keys(self.output_directory.clone(), "canonicalization_ast.json", &["span"])?; } } @@ -292,14 +292,14 @@ impl<'a, F: PrimeField, G: GroupType> Compiler<'a, F, G> { .phase_ast(&self.program, &asg.clone().into_repr()) .expect("Failed to produce type inference ast."); - if !self.ast_snapshot_options.spans_enabled { + if self.ast_snapshot_options.spans_enabled { + new_ast.to_json_file(self.output_directory.clone(), "type_inferenced_ast.json")?; + } else { new_ast.to_json_file_without_keys( self.output_directory.clone(), "type_inferenced_ast.json", &["span"], )?; - } else { - new_ast.to_json_file(self.output_directory.clone(), "type_inferenced_ast.json")?; } } diff --git a/compiler/src/test.rs b/compiler/src/test.rs index c228b9f355..88a63b6690 100644 --- a/compiler/src/test.rs +++ b/compiler/src/test.rs @@ -118,7 +118,7 @@ impl Namespace for CompileNamespace { let parsed = parse_program( &test.content, Some(AstSnapshotOptions { - spans_enabled: true, + spans_enabled: false, initial: true, imports_resolved: true, canonicalized: true, diff --git a/test-framework/src/bin/tgc.rs b/test-framework/src/bin/tgc.rs index c55661b122..e7a68def7e 100644 --- a/test-framework/src/bin/tgc.rs +++ b/test-framework/src/bin/tgc.rs @@ -114,40 +114,29 @@ fn run_with_args(opt: Opt) -> Result<(), Box> { // Do this to match test-framework ast bc of spans let text = &text[end_of_header + 2..]; // Write all files into the directory. - let (initial, imports_resolved, canonicalized, type_inferenced) = generate_asts(cwd, text)?; - - target.push("initial_ast.json"); - fs::write(target.clone(), initial)?; - target.pop(); - - target.push("imports_resolved_ast.json"); - fs::write(target.clone(), imports_resolved)?; - target.pop(); - - target.push("canonicalization_ast.json"); - fs::write(target.clone(), canonicalized)?; - target.pop(); - - target.push("type_inferenced_ast.json"); - fs::write(target.clone(), type_inferenced)?; + generate_asts(cwd, target, text)?; } } Ok(()) } -/// Do what Compiler does - prepare 3 stages of AST: initial, canonicalized and type_inferenced -fn generate_asts(path: PathBuf, text: &str) -> Result<(String, String, String, String), Box> { +/// Do what Compiler does - prepare 3 stages of AST: initial, imports_resolved, canonicalized and type_inferenced +/// Write these ASTs without Spans to JSON +fn generate_asts(src_path: PathBuf, target_path: PathBuf, text: &str) -> Result<(), Box> { std::env::set_var("LEO_TESTFRAMEWORK", "true"); - let mut ast = leo_parser::parse_ast(path.clone().into_os_string().into_string().unwrap(), text)?; - let initial = ast.to_json_string()?; + let mut ast = leo_parser::parse_ast(src_path.clone().into_os_string().into_string().unwrap(), text)?; - ast = leo_ast_passes::Importer::do_pass(ast.into_repr(), &mut ImportParser::new(path, Default::default()))?; - let imports_resolved = ast.to_json_string()?; + ast.to_json_file_without_keys(target_path.clone(), "initial_ast.json", &["span"])?; + + ast = leo_ast_passes::Importer::do_pass(ast.into_repr(), &mut ImportParser::new(src_path, Default::default()))?; + + ast.to_json_file_without_keys(target_path.clone(), "imports_resolved_ast.json", &["span"])?; ast = leo_ast_passes::Canonicalizer::do_pass(ast.into_repr())?; - let canonicalized = ast.to_json_string()?; + + ast.to_json_file_without_keys(target_path.clone(), "canonicalization_ast.json", &["span"])?; let mut ti_ast = ast.into_repr(); ti_ast.name = "test".to_string(); // Do this to match test-framework ast @@ -156,12 +145,13 @@ fn generate_asts(path: PathBuf, text: &str) -> Result<(String, String, String, S let type_inferenced = TypeInferencePhase::default() .phase_ast(&ti_ast, &asg.clone().into_repr()) - .expect("Failed to produce type inference ast.") - .to_json_string()?; + .expect("Failed to produce type inference ast."); + + type_inferenced.to_json_file_without_keys(target_path, "type_inferenced_ast.json", &["span"])?; std::env::remove_var("LEO_TESTFRAMEWORK"); - Ok((initial, imports_resolved, canonicalized, type_inferenced)) + Ok(()) } fn handle_error(res: Result<(), Box>) {