prepare for modules

This commit is contained in:
2025-04-25 04:16:54 -04:00
parent 0ceb82445e
commit 4e7c201690
30 changed files with 369 additions and 333 deletions

View File

@@ -17,7 +17,7 @@ impl LProgram {
pub fn create(p: &UProgram) -> Result<Self, String> {
let start = p
.names
.id::<UFunc>("start")
.id::<UFunc>("crate")
.ok_or("no start method found")?;
let mut ssbuilder = SymbolSpaceBuilder::with_entries(&[start]);
let entry = ssbuilder.func(&start);

View File

@@ -100,7 +100,7 @@ macro_rules! impl_kind {
($struc:ty, $idx:expr, $field:ident, $name:expr) => {
impl_kind!($struc, $idx, $field, $name, nofin);
impl Finish for $struc {
fn finish(_: &mut UProgram, _: ID<Self>) {}
fn finish(_: &mut UProgram, _: ID<Self>, _: &str) {}
}
};
($struc:ty, $idx:expr, $field:ident, $name:expr, nofin) => {
@@ -133,9 +133,9 @@ pub type DataID = ID<UData>;
pub type GenericID = ID<UGeneric>;
impl Finish for UFunc {
fn finish(p: &mut UProgram, id: ID<Self>) {
fn finish(p: &mut UProgram, id: ID<Self>, name: &str) {
let var = p.def_searchable(
p.names.name(id).to_string(),
name.to_string(),
Some(UVar {
parent: None,
ty: Type::Placeholder,
@@ -153,5 +153,5 @@ pub trait Kind: Sized {
}
pub trait Finish: Sized {
fn finish(program: &mut UProgram, id: ID<Self>);
fn finish(program: &mut UProgram, id: ID<Self>, name: &str);
}

View File

@@ -1,17 +1,18 @@
mod kind;
mod instr;
mod ty;
mod program;
mod validate;
mod assoc;
mod error;
mod inst;
mod maps;
mod instr;
mod kind;
mod program;
mod ty;
mod validate;
use super::*;
use maps::*;
pub use maps::Idents;
pub use kind::*;
pub use instr::*;
pub use ty::*;
pub use program::*;
use assoc::*;
pub use assoc::Idents;
pub use inst::*;
pub use instr::*;
pub use kind::*;
pub use program::*;
pub use ty::*;

View File

@@ -2,16 +2,19 @@ use super::*;
use std::collections::HashMap;
pub struct UProgram {
// kinds
pub fns: Vec<Option<UFunc>>,
pub vars: Vec<Option<UVar>>,
pub structs: Vec<Option<UStruct>>,
pub types: Vec<Option<UGeneric>>,
pub data: Vec<Option<UData>>,
// associated data
pub names: NameMap,
pub origins: OriginMap,
pub fn_var: FnVarMap,
pub temp: usize,
pub path: Vec<String>,
pub name_stack: Vec<HashMap<String, Idents>>,
pub temp: usize,
}
impl UProgram {
@@ -25,8 +28,9 @@ impl UProgram {
names: NameMap::new(),
origins: OriginMap::new(),
fn_var: FnVarMap::new(),
temp: 0,
path: Vec::new(),
name_stack: vec![HashMap::new()],
temp: 0,
}
}
pub fn push(&mut self) {
@@ -35,6 +39,12 @@ impl UProgram {
pub fn pop(&mut self) {
self.name_stack.pop();
}
pub fn push_name(&mut self, name: &str) {
self.path.push(name.to_string());
}
pub fn pop_name(&mut self) {
self.path.pop();
}
pub fn get_idents(&self, name: &str) -> Option<Idents> {
for map in self.name_stack.iter().rev() {
let res = map.get(name);
@@ -70,7 +80,7 @@ impl UProgram {
fn temp_var_inner(&mut self, origin: Origin, ty: Type, parent: Option<FieldRef>) -> VarInst {
let v = self.def(
format!("temp{}", self.temp),
&format!("temp{}", self.temp),
Some(UVar { parent, ty }),
origin,
);
@@ -85,23 +95,32 @@ impl UProgram {
K::from_program_mut(self)[id.0] = Some(k);
}
pub fn def<K: Kind + Finish>(&mut self, name: String, k: Option<K>, origin: Origin) -> ID<K> {
self.names.push::<K>(name);
pub fn def<K: Kind + Finish>(&mut self, name: &str, k: Option<K>, origin: Origin) -> ID<K> {
self.names.push::<K>(self.path_for(name));
self.origins.push::<K>(origin);
let vec = K::from_program_mut(self);
let id = ID::new(vec.len());
vec.push(k);
K::finish(self, id);
K::finish(self, id, name);
id
}
pub fn path_for(&self, name: &str) -> String {
if self.path.is_empty() {
return name.to_string();
}
let mut path = self.path.join("::");
path = path + "::" + name;
path
}
pub fn def_searchable<K: Kind + Finish>(
&mut self,
name: String,
k: Option<K>,
origin: Origin,
) -> ID<K> {
let id = self.def(name.clone(), k, origin);
let id = self.def(&name, k, origin);
self.name_on_stack(id, name);
id
}

View File

@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use super::{GenericID, Len, StructID, UInstruction, UProgram, UVar, VarID};
@@ -80,7 +80,7 @@ impl UProgram {
pub fn resolve_instr_types(
&self,
vars: &mut Vec<Option<UVar>>,
vars: &mut [Option<UVar>],
i: &UInstruction,
) -> Result<(), VarID> {
'outer: {
@@ -161,13 +161,10 @@ impl UProgram {
args[i] = ty;
}
}
// for arg in &args {
// println!("{:?}", self.type_name(arg));
// }
set(vars, dest.id, Type::Struct { id, args });
}
UInstruction::If { cond, body } => {}
UInstruction::Loop { body } => {}
UInstruction::If { cond, body: _ } => {}
UInstruction::Loop { body: _ } => {}
UInstruction::Break => {}
UInstruction::Continue => {}
}

View File

@@ -24,7 +24,7 @@ impl UProgram {
}
if var.ty == Type::Infer {
output.err(CompilerMsg {
msg: format!("Var {:?} cannot be inferred", id),
msg: format!("Var {:?} cannot be inferred!", id),
spans: vec![self.origins.get(id)],
});
}
@@ -40,7 +40,7 @@ impl UProgram {
output.check_assign(self, &var.ty, ft, self.origins.get(id));
} else {
output.err(CompilerMsg {
msg: format!("invalid parent!"),
msg: "invalid parent!".to_string(),
spans: vec![self.origins.get(id)],
});
}