Make global identifier for macro-generated globals better

This commit is contained in:
Sofia 2025-07-29 15:19:14 +03:00
parent baa7bafafc
commit f5f55079a9

View File

@ -1,11 +1,15 @@
use std::collections::HashMap; use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::mir::{self, FunctionCall, GlobalKind, GlobalValue, IfExpression, Literal, TypeKind, WhileStatement}; use crate::mir::{self, FunctionCall, GlobalKind, GlobalValue, IfExpression, Literal, TypeKind, WhileStatement};
use super::pass::{Pass, PassResult, PassState}; use super::pass::{Pass, PassResult, PassState};
pub trait MacroFunction: std::fmt::Debug { pub trait MacroFunction: std::fmt::Debug {
fn generate<'ctx, 'a>(&self, params: &[mir::Literal]) -> Result<(Vec<GlobalValue>, mir::ExprKind), ErrorKind>; fn generate<'ctx, 'a>(
&self,
params: &[mir::Literal],
prefix: String,
) -> Result<(Vec<GlobalValue>, mir::ExprKind), ErrorKind>;
} }
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(thiserror::Error, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
@ -30,6 +34,8 @@ pub struct MacroPass {
pub(crate) macros: HashMap<String, Box<dyn MacroFunction>>, pub(crate) macros: HashMap<String, Box<dyn MacroFunction>>,
} }
pub struct MacroData {}
type MacroPassState<'st, 'sc> = PassState<'st, 'sc, (), ErrorKind>; type MacroPassState<'st, 'sc> = PassState<'st, 'sc, (), ErrorKind>;
impl Pass for MacroPass { impl Pass for MacroPass {
@ -106,7 +112,13 @@ impl mir::Expression {
} }
let (generated_globals, expr) = state.or_else( let (generated_globals, expr) = state.or_else(
existing_macro existing_macro
.generate(&literals) .generate(
&literals,
format!(
"macro.{}.{}.{}",
function_call.name, self.1.range.start, self.1.range.end
),
)
.map(|(globals, kind)| (globals, mir::Expression(kind, self.1))), .map(|(globals, kind)| (globals, mir::Expression(kind, self.1))),
(Vec::new(), self.clone()), (Vec::new(), self.clone()),
self.1, self.1,
@ -190,7 +202,11 @@ pub fn form_macros() -> HashMap<String, Box<dyn MacroFunction>> {
#[derive(Debug)] #[derive(Debug)]
pub struct TestMacro; pub struct TestMacro;
impl MacroFunction for TestMacro { impl MacroFunction for TestMacro {
fn generate<'ctx, 'a>(&self, literals: &[mir::Literal]) -> Result<(Vec<GlobalValue>, mir::ExprKind), ErrorKind> { fn generate<'ctx, 'a>(
&self,
literals: &[mir::Literal],
global_name: String,
) -> Result<(Vec<GlobalValue>, mir::ExprKind), ErrorKind> {
if literals.len() != 1 { if literals.len() != 1 {
return Err(ErrorKind::InvalidAmountOfParams(literals.len() as u32, 1)); return Err(ErrorKind::InvalidAmountOfParams(literals.len() as u32, 1));
} }
@ -214,7 +230,6 @@ impl MacroFunction for TestMacro {
let len = literals.len(); let len = literals.len();
let global_name = "sometestglobalvalue".to_owned();
let global = GlobalValue { let global = GlobalValue {
name: global_name.clone(), name: global_name.clone(),
kind: GlobalKind::Array(literals), kind: GlobalKind::Array(literals),