Refactor creation of CodeGenerator and management of known data_types and functions.
This was a mess to say to the least. The mess started when we wanted to make all definitions in codegen use immutable maps of references -- which was and still is a good idea. Yet, the population of the data types and functions definitions was done somehow in a separate step, in a rather ad-hoc manner. This commit changes that to ensure the project's data_types and functions are populated while type checking the AST such that we need not to redo it after. The code for registering the data type definitions and function definitions was also duplicated in at least 3 places. It is now a method of the TypedModule. Note: this change isn't only just cosmetic, it's also necessary for the commit that follows which aims at adding tests to the set of available function definitions, thus allowing to make property tests callable.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::TestProject;
|
||||
use crate::module::CheckedModules;
|
||||
use aiken_lang::ast::{Definition, Function, TraceLevel, Tracing, TypedTest, TypedValidator};
|
||||
use pretty_assertions::assert_eq;
|
||||
use uplc::{
|
||||
ast::{Constant, Data, DeBruijn, Name, Program, Term, Type},
|
||||
builder::{CONSTR_FIELDS_EXPOSER, CONSTR_INDEX_EXPOSER},
|
||||
@@ -8,10 +9,6 @@ use uplc::{
|
||||
optimize,
|
||||
};
|
||||
|
||||
use crate::module::CheckedModules;
|
||||
|
||||
use super::TestProject;
|
||||
|
||||
enum TestType {
|
||||
Func(TypedTest),
|
||||
Validator(TypedValidator),
|
||||
@@ -22,12 +19,7 @@ fn assert_uplc(source_code: &str, expected: Term<Name>, should_fail: bool) {
|
||||
|
||||
let modules = CheckedModules::singleton(project.check(project.parse(source_code)));
|
||||
|
||||
let mut generator = modules.new_generator(
|
||||
&project.functions,
|
||||
&project.data_types,
|
||||
&project.module_types,
|
||||
Tracing::All(TraceLevel::Verbose),
|
||||
);
|
||||
let mut generator = project.new_generator(Tracing::All(TraceLevel::Verbose));
|
||||
|
||||
let Some(checked_module) = modules.values().next() else {
|
||||
unreachable!("There's got to be one right?")
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{
|
||||
builtins,
|
||||
module::{CheckedModule, ParsedModule},
|
||||
package_name::PackageName,
|
||||
utils,
|
||||
};
|
||||
use aiken_lang::{
|
||||
ast::{
|
||||
DataTypeKey, FunctionAccessKey, ModuleKind, TraceLevel, Tracing, TypedDataType,
|
||||
TypedFunction,
|
||||
},
|
||||
gen_uplc::CodeGenerator,
|
||||
line_numbers::LineNumbers,
|
||||
parser,
|
||||
tipo::TypeInfo,
|
||||
IdGenerator,
|
||||
};
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::{
|
||||
builtins,
|
||||
module::{CheckedModule, ParsedModule},
|
||||
package_name::PackageName,
|
||||
};
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
mod gen_uplc;
|
||||
|
||||
@@ -26,9 +26,10 @@ mod gen_uplc;
|
||||
pub struct TestProject {
|
||||
pub package: PackageName,
|
||||
pub id_gen: IdGenerator,
|
||||
pub module_types: HashMap<String, TypeInfo>,
|
||||
pub functions: IndexMap<FunctionAccessKey, TypedFunction>,
|
||||
pub data_types: IndexMap<DataTypeKey, TypedDataType>,
|
||||
pub module_types: HashMap<String, TypeInfo>,
|
||||
pub module_sources: HashMap<String, (String, LineNumbers)>,
|
||||
}
|
||||
|
||||
impl TestProject {
|
||||
@@ -53,9 +54,20 @@ impl TestProject {
|
||||
module_types,
|
||||
functions,
|
||||
data_types,
|
||||
module_sources: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_generator(&'_ self, tracing: Tracing) -> CodeGenerator<'_> {
|
||||
CodeGenerator::new(
|
||||
utils::indexmap::as_ref_values(&self.functions),
|
||||
utils::indexmap::as_ref_values(&self.data_types),
|
||||
utils::indexmap::as_str_ref_values(&self.module_types),
|
||||
utils::indexmap::as_str_ref_values(&self.module_sources),
|
||||
tracing,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse(&self, source_code: &str) -> ParsedModule {
|
||||
let kind = ModuleKind::Validator;
|
||||
let name = "test_module".to_owned();
|
||||
@@ -88,6 +100,17 @@ impl TestProject {
|
||||
)
|
||||
.expect("Failed to type-check module");
|
||||
|
||||
// Register function definitions & data-types for easier access later.
|
||||
ast.register_definitions(&mut self.functions, &mut self.data_types);
|
||||
|
||||
// Register module sources for an easier access later.
|
||||
self.module_sources.insert(
|
||||
module.name.clone(),
|
||||
(module.code.clone(), LineNumbers::new(&module.code)),
|
||||
);
|
||||
|
||||
// Register the types from this module so they can be
|
||||
// imported into other modules.
|
||||
self.module_types
|
||||
.insert(module.name.clone(), ast.type_info.clone());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user