initial structure impl

This commit is contained in:
2025-03-26 21:39:24 -04:00
parent 0614d48fcc
commit 021434d2f1
23 changed files with 390 additions and 84 deletions
+6 -3
View File
@@ -37,7 +37,10 @@ impl PType {
output: &mut CompilerOutput,
span: FileSpan,
) -> Type {
match namespace.get(&self.name).and_then(|ids| ids.ty) {
let Some(name) = self.name.as_ref() else {
return Type::Error;
};
match namespace.get(&name).and_then(|ids| ids.ty) {
Some(id) => {
if self.args.is_empty() {
Type::Concrete(id)
@@ -51,10 +54,10 @@ impl PType {
}
}
None => {
if let Ok(num) = self.name.parse::<u32>() {
if let Ok(num) = name.parse::<u32>() {
Type::Bits(num)
} else {
match self.name.as_str() {
match name.as_str() {
"slice" => {
let inner = self.args[0].lower(namespace, output);
Type::Slice(Box::new(inner))
+35 -8
View File
@@ -1,5 +1,8 @@
use super::{func::FnLowerCtx, FnLowerable, PExpr, UnaryOp};
use crate::ir::{DataDef, IRUInstruction, Origin, Type, VarInst};
use crate::{
ir::{DataDef, IRUInstruction, Origin, Type, VarInst},
parser::PInfixOp,
};
impl FnLowerable for PExpr {
type Output = VarInst;
@@ -13,7 +16,7 @@ impl FnLowerable for PExpr {
DataDef {
ty: Type::Bits(8).arr(data.len() as u32),
origin: Origin::File(l.span),
label: format!("string \"{}\"", s.replace("\n", "\\n"))
label: format!("string \"{}\"", s.replace("\n", "\\n")),
},
data,
);
@@ -42,7 +45,7 @@ impl FnLowerable for PExpr {
DataDef {
ty,
origin: Origin::File(l.span),
label: format!("num {n:?}")
label: format!("num {n:?}"),
},
n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec(),
);
@@ -56,15 +59,39 @@ impl FnLowerable for PExpr {
PExpr::Ident(i) => ctx.get_var(i)?,
PExpr::BinaryOp(op, e1, e2) => {
let res1 = e1.lower(ctx)?;
let res2 = e2.lower(ctx)?;
op.traitt();
todo!();
if *op == PInfixOp::Access {
let sty = &ctx.map.get_var(res1.id).ty;
let Type::Concrete(tid) = sty else {
ctx.err(format!("Type {:?} has no fields", ctx.map.type_name(sty)));
return None;
};
let struc = ctx.map.get_struct(*tid);
let Some(box PExpr::Ident(ident)) = &e2.inner else {
ctx.err(format!("Field accesses must be identifiers",));
return None;
};
let fname = &ident.as_ref()?.0;
let Some(field) = struc.fields.get(fname) else {
ctx.err(format!("Field '{fname}' not in struct"));
return None;
};
let temp = ctx.temp(field.ty.clone());
ctx.push(IRUInstruction::Access {
dest: temp,
src: res1,
field: fname.to_string(),
});
temp
} else {
let res2 = e2.lower(ctx)?;
todo!()
}
}
PExpr::UnaryOp(op, e) => {
let res = e.lower(ctx)?;
match op {
UnaryOp::Ref => {
let temp = ctx.temp(ctx.map.get_var(res.id).ty.clone());
let temp = ctx.temp(ctx.map.get_var(res.id).ty.clone().rf());
ctx.push(IRUInstruction::Ref {
dest: temp,
src: res,
@@ -120,7 +147,7 @@ impl FnLowerable for PExpr {
temp
}
PExpr::Group(e) => e.lower(ctx)?,
PExpr::Construct(c) => todo!(),
PExpr::Construct(c) => c.lower(ctx)?,
})
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ use super::{CompilerMsg, CompilerOutput, FileSpan, FnLowerable, Node, PFunction}
use crate::{
ir::{
FnDef, FnID, IRInstructions, IRUFunction, IRUInstruction, Idents, NamespaceGuard, Origin,
Type, VarDef, VarID, VarInst,
Type, VarDef, VarInst,
},
parser,
};
+2 -1
View File
@@ -1,10 +1,11 @@
mod arch;
mod asm;
mod block;
mod def;
mod expr;
mod func;
mod module;
mod arch;
mod struc;
use super::*;
+3
View File
@@ -4,6 +4,9 @@ use super::{PModule, CompilerOutput};
impl PModule {
pub fn lower(&self, map: &mut NamespaceGuard, output: &mut CompilerOutput) {
for s in &self.structs {
s.lower(map, output);
}
let mut fns = Vec::new();
for f in &self.functions {
if let Some(id) = f.lower_header(map, output) {
+96
View File
@@ -0,0 +1,96 @@
use std::collections::HashMap;
use crate::{
common::{CompilerMsg, CompilerOutput, FileSpan},
ir::{IRUInstruction, NamespaceGuard, Origin, StructDef, StructField, VarInst},
parser::{Node, PConstruct, PConstructFields, PStruct, PStructFields},
};
use super::{FnLowerCtx, FnLowerable};
impl FnLowerable for PConstruct {
type Output = VarInst;
fn lower(&self, ctx: &mut FnLowerCtx) -> Option<VarInst> {
let ty = self.name.lower(ctx.map, ctx.output);
let fields = match &self.fields {
PConstructFields::Named(nodes) => nodes
.iter()
.flat_map(|n| {
let def = n.as_ref()?;
let name = def.name.as_ref()?.to_string();
let expr = def.val.as_ref()?.lower(ctx)?;
Some((name, expr))
})
.collect(),
PConstructFields::Tuple(nodes) => nodes
.iter()
.enumerate()
.flat_map(|(i, n)| {
let expr = n.as_ref()?.lower(ctx)?;
Some((format!("{i}"), expr))
})
.collect(),
PConstructFields::None => HashMap::new(),
};
let id = ctx.temp(ty);
ctx.push(IRUInstruction::Construct { dest: id, fields });
Some(id)
}
}
impl PStruct {
pub fn lower(
&self,
map: &mut NamespaceGuard,
output: &mut CompilerOutput,
span: FileSpan,
) -> Option<()> {
let mut offset = 0;
let fields = match &self.fields {
PStructFields::Named(nodes) => nodes
.iter()
.flat_map(|n| {
let def = n.as_ref()?;
let name = def.name.as_ref()?.to_string();
let tynode = def.ty.as_ref()?;
let ty = tynode.lower(map, output);
let size = map.size_of_type(&ty).unwrap_or_else(|| {
output.err(CompilerMsg {
msg: format!("Size of type '{}' unknown", map.type_name(&ty)),
spans: vec![tynode.span],
});
0
});
let res = Some((name, StructField { ty, offset }));
offset += size;
res
})
.collect(),
PStructFields::Tuple(nodes) => nodes
.iter()
.enumerate()
.flat_map(|(i, n)| {
let ty = n.as_ref()?.lower(map, output, span);
let size = map.size_of_type(&ty)?;
let res = Some((format!("{i}"), StructField { ty, offset }));
offset += size;
res
})
.collect(),
PStructFields::None => HashMap::new(),
};
map.def_type(StructDef {
name: self.name.as_ref()?.to_string(),
origin: Origin::File(span),
size: offset,
fields,
});
Some(())
}
}
impl Node<PStruct> {
pub fn lower(&self, map: &mut NamespaceGuard, output: &mut CompilerOutput) {
self.as_ref().map(|i| i.lower(map, output, self.span));
}
}