Edit example a bit, fix macro generation in function parameters

This commit is contained in:
Sofia 2025-07-31 22:48:16 +03:00
parent deed96bbfd
commit 4f0ee72c83
3 changed files with 67 additions and 47 deletions

View File

@ -4,5 +4,6 @@ import std::print;
fn main() -> u8 { fn main() -> u8 {
let bytes = include_bytes!("./macro_easy_file.txt"); let bytes = include_bytes!("./macro_easy_file.txt");
print(String::new() + bytes.length()); print(String::new() + bytes.length());
return (bytes as *u8)[0]; print(String::new() + (include_bytes!("./macro_easy_file.txt") as *u8)[1] as u64);
return (include_bytes!("./macro_easy_file.txt") as *u8)[0];
} }

View File

@ -1311,45 +1311,47 @@ impl mir::Expression {
if val.1 == *type_kind { if val.1 == *type_kind {
Some(val) Some(val)
} else { } else {
match (&val.1, type_kind) { let (ty, other) = if !state.should_load {
(TypeKind::CodegenPtr(inner), TypeKind::UserPtr(ty2)) => match *inner.clone() { let TypeKind::CodegenPtr(inner) = &val.1 else {
TypeKind::UserPtr(_) => Some(StackValue( panic!();
};
(*inner.clone(), TypeKind::CodegenPtr(Box::new(type_kind.clone())))
} else {
(val.1.clone(), type_kind.clone())
};
dbg!(&ty, type_kind);
match (&ty, type_kind) {
(TypeKind::UserPtr(_), TypeKind::UserPtr(_)) => Some(StackValue(
val.0.derive( val.0.derive(
scope scope
.block .block
.build(Instr::BitCast( .build(Instr::BitCast(val.instr(), other.get_type(scope.type_values)))
val.instr(),
Type::Ptr(Box::new(type_kind.get_type(scope.type_values))),
))
.unwrap(), .unwrap(),
), ),
TypeKind::CodegenPtr(Box::new(type_kind.clone())), other.clone(),
)), )),
TypeKind::Borrow(ty1, _) => match *ty1.clone() { (TypeKind::Borrow(ty1, _), TypeKind::UserPtr(ty2)) => {
TypeKind::Array(ty1, _) => { if let TypeKind::Array(ty1, _) = ty1.as_ref() {
if ty1 == *ty2 { if ty1 == ty2 {
Some(StackValue( Some(StackValue(
val.0.derive( val.0.derive(
scope scope
.block .block
.build(Instr::BitCast( .build(Instr::BitCast(val.instr(), other.get_type(scope.type_values)))
val.instr(),
Type::Ptr(Box::new(type_kind.get_type(scope.type_values))),
))
.unwrap(), .unwrap(),
), ),
TypeKind::CodegenPtr(Box::new(type_kind.clone())), other,
)) ))
} else { } else {
return Err(ErrorKind::Null); return Err(ErrorKind::Null).unwrap();
}
} else {
return Err(ErrorKind::Null).unwrap();
} }
} }
_ => return Err(ErrorKind::Null), (TypeKind::Char, TypeKind::U8)
},
_ => panic!(),
},
(TypeKind::UserPtr(_), TypeKind::UserPtr(_))
| (TypeKind::Char, TypeKind::U8)
| (TypeKind::U8, TypeKind::Char) | (TypeKind::U8, TypeKind::Char)
| (TypeKind::U8, TypeKind::I8) => Some(StackValue( | (TypeKind::U8, TypeKind::I8) => Some(StackValue(
val.0.derive( val.0.derive(
@ -1361,8 +1363,7 @@ impl mir::Expression {
type_kind.clone(), type_kind.clone(),
)), )),
_ => { _ => {
let cast_instr = val let cast_instr = ty
.1
.get_type(scope.type_values) .get_type(scope.type_values)
.cast_instruction(val.instr(), &type_kind.get_type(scope.type_values)) .cast_instruction(val.instr(), &type_kind.get_type(scope.type_values))
.unwrap(); .unwrap();
@ -1378,11 +1379,26 @@ impl mir::Expression {
mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?, mir::ExprKind::AssociatedFunctionCall(ty, call) => codegen_function_call(Some(ty), call, scope, state)?,
mir::ExprKind::GlobalRef(global_name, ty) => { mir::ExprKind::GlobalRef(global_name, ty) => {
let global_value = scope.globals.get(global_name).unwrap(); let global_value = scope.globals.get(global_name).unwrap();
let value = scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap();
if !state.should_load {
let allocated = scope
.block
.build(Instr::Alloca(ty.get_type(scope.type_values)))
.unwrap();
scope.block.build(Instr::Store(allocated, value)).unwrap();
let a = Some(StackValue( let a = Some(StackValue(
StackValueKind::Literal(scope.block.build(Instr::GetGlobal(global_value.clone())).unwrap()), StackValueKind::Literal(allocated),
ty.clone(), TypeKind::CodegenPtr(Box::new(ty.clone())),
)); ));
a a
} else {
let a = Some(StackValue(StackValueKind::Literal(value), ty.clone()));
a
}
} }
}; };
if let Some(value) = &value { if let Some(value) = &value {

View File

@ -118,10 +118,13 @@ impl mir::Expression {
let mut globals = Vec::new(); let mut globals = Vec::new();
match &mut self.0 { match &mut self.0 {
mir::ExprKind::FunctionCall(function_call) => { mir::ExprKind::FunctionCall(function_call) => {
for param in &mut function_call.parameters {
globals.extend(param.gen_macros(data, state, map));
}
if function_call.is_macro { if function_call.is_macro {
if let Some(existing_macro) = data.macros.get(&function_call.name) { if let Some(existing_macro) = data.macros.get(&function_call.name) {
let mut literals = Vec::new(); let mut literals = Vec::new();
for param in &function_call.parameters { for param in &mut function_call.parameters {
match &param.0 { match &param.0 {
super::ExprKind::Literal(literal) => literals.push(literal.clone()), super::ExprKind::Literal(literal) => literals.push(literal.clone()),
_ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1), _ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1),