define plot function

This commit is contained in:
Sébastien Bourdeauducq 2024-10-04 18:15:34 +08:00
parent 0488f85b9e
commit 216e96bb05
1 changed files with 85 additions and 35 deletions

View File

@ -229,6 +229,41 @@ fn handle_global_var(
} }
} }
fn register_cells_function(
composer: &mut composer::TopLevelComposer,
resolver: Arc<dyn nac3core::symbol_resolver::SymbolResolver + Send + Sync>,
internal_resolver: &ResolverInternal,
name: &str,
signature: typedef::FunSignature,
) {
let ty = composer.unifier.add_ty(typedef::TypeEnum::TFunc(signature));
let name_ref = name.into();
let mut function_def = composer::TopLevelComposer::make_top_level_function_def(
name.to_string(),
name_ref,
ty,
Some(resolver.clone()),
None,
);
if let toplevel::TopLevelDef::Function {
ref mut instance_to_symbol,
..
} = function_def
{
instance_to_symbol.insert("".to_string(), "__nac3_cells_".to_string() + name);
} else {
unreachable!();
};
composer
.definition_ast_list
.push((RwLock::new(function_def).into(), None));
internal_resolver.add_id_def(
name_ref,
toplevel::DefinitionId(composer.definition_ast_list.len() - 1),
);
internal_resolver.add_id_type(name_ref, ty);
}
fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result<(), String> { fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result<(), String> {
let mut target_machine_options = codegen::CodeGenTargetMachineOptions::from_host(); let mut target_machine_options = codegen::CodeGenTargetMachineOptions::from_host();
target_machine_options.reloc_mode = inkwell::targets::RelocMode::PIC; target_machine_options.reloc_mode = inkwell::targets::RelocMode::PIC;
@ -263,6 +298,56 @@ fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result
as Arc<dyn nac3core::symbol_resolver::SymbolResolver + Send + Sync>; as Arc<dyn nac3core::symbol_resolver::SymbolResolver + Send + Sync>;
let irrt = codegen::irrt::load_irrt(&context, resolver.as_ref()); let irrt = codegen::irrt::load_irrt(&context, resolver.as_ref());
let list_tvar = if let typedef::TypeEnum::TObj { params, .. } =
&*composer.unifier.get_ty_immutable(primitive.list)
{
typedef::iter_type_vars(params).nth(0).unwrap()
} else {
unreachable!()
};
let list_float = composer
.unifier
.subst(
primitive.list,
&typedef::into_var_map([typedef::TypeVar {
id: list_tvar.id,
ty: primitive.float,
}]),
)
.unwrap();
register_cells_function(
&mut composer,
resolver.clone(),
internal_resolver.as_ref(),
"slider",
typedef::FunSignature {
args: vec![typedef::FuncArg {
name: "prev".into(),
ty: primitive.float,
default_value: None,
is_vararg: false,
}],
ret: primitive.float,
vars: typedef::VarMap::new(),
},
);
register_cells_function(
&mut composer,
resolver.clone(),
internal_resolver.as_ref(),
"plot",
typedef::FunSignature {
args: vec![typedef::FuncArg {
name: "data".into(),
ty: list_float,
default_value: None,
is_vararg: false,
}],
ret: primitive.none,
vars: typedef::VarMap::new(),
},
);
let parser_result = let parser_result =
match nac3parser::parser::parse_program(code.as_str(), String::from("cell1").into()) { match nac3parser::parser::parse_program(code.as_str(), String::from("cell1").into()) {
Ok(parser_result) => parser_result, Ok(parser_result) => parser_result,
@ -324,41 +409,6 @@ fn compile(code: &String, run_symbol: &String, output_filename: &Path) -> Result
} }
} }
let ty = composer
.unifier
.add_ty(typedef::TypeEnum::TFunc(typedef::FunSignature {
args: vec![typedef::FuncArg {
name: "value".into(),
ty: primitive.float,
default_value: None,
is_vararg: false,
}],
ret: primitive.float,
vars: typedef::VarMap::new(),
}));
let name_ref = "slider".into();
let mut function_def = composer::TopLevelComposer::make_top_level_function_def(
"slider".to_string(),
name_ref,
ty,
Some(resolver.clone()),
None,
);
if let toplevel::TopLevelDef::Function { ref mut instance_to_symbol, .. } = function_def {
instance_to_symbol.insert("".to_string(), "__nac3_cells_slider".to_string());
} else {
unreachable!();
};
composer.definition_ast_list.push((
RwLock::new(function_def).into(),
None,
));
internal_resolver.add_id_def(
name_ref,
toplevel::DefinitionId(composer.definition_ast_list.len() - 1),
);
internal_resolver.add_id_type(name_ref, ty);
let signature = typedef::FunSignature { let signature = typedef::FunSignature {
args: vec![], args: vec![],
ret: primitive.none, ret: primitive.none,