Implement object inheritance: # Child = > Parent (I0-I2)
- Parse `# Child = > Parent` object decls (base must be a simple name after `>`; dotted bases and `# Foo = 1` are diagnostics). - Validate in load: unknown base types and cyclic inheritance (A → B → A) fail with line/col. - Method resolution walks the base chain (most specific first) on both tree and bytecode backends; `_type` stays the child name; same-name child ## overrides the parent. - Add inherit.rs, wire Program.object_bases, gold fixtures (inherit-parent-method / inherit-override + cycle/unknown-base/bad-rhs) and public 09-inherit demo pages. Docs: objects.md, markdown-mapping, roadmap, skill. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a23fdedfa2
commit
5456d9ff83
25 files changed
+590
-41
No files matched your search
@@ -15,6 +15,8 @@ struct FlatFun {
|
||||
name: String,
|
||||
level: u8,
|
||||
parent: Option<usize>,
|
||||
/// Inheritance base type name (`# Child = > Parent`), objects only.
|
||||
base: Option<String>,
|
||||
params: Vec<Param>,
|
||||
body: Vec<Stmt>,
|
||||
children: Vec<usize>,
|
||||
@@ -60,6 +62,19 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let name_to_flat: HashMap<&str, usize> = objects
|
||||
.iter()
|
||||
.map(|(n, i)| (n.as_str(), *i))
|
||||
.collect();
|
||||
let mut object_bases = Vec::with_capacity(objects.len());
|
||||
for (name, _) in &objects {
|
||||
let flat_i = name_to_flat[name.as_str()];
|
||||
let base = flat[flat_i]
|
||||
.base
|
||||
.as_deref()
|
||||
.and_then(|b| name_to_flat.get(b).copied());
|
||||
object_bases.push(base);
|
||||
}
|
||||
|
||||
let path_buf = path.map(|p| p.to_path_buf());
|
||||
let mut functions = Vec::new();
|
||||
@@ -74,6 +89,7 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
|
||||
functions,
|
||||
entry,
|
||||
objects,
|
||||
object_bases,
|
||||
methods,
|
||||
parents,
|
||||
children,
|
||||
@@ -134,6 +150,7 @@ fn collect_fun(
|
||||
name: fun.name.clone(),
|
||||
level: fun.level,
|
||||
parent,
|
||||
base: fun.base.clone(),
|
||||
params: fun.params.clone(),
|
||||
body: fun.body.clone(),
|
||||
children: Vec::new(),
|
||||
|
||||
@@ -92,6 +92,8 @@ pub struct Program {
|
||||
pub entry: usize,
|
||||
/// Top-level object name → function index (level-1).
|
||||
pub objects: Vec<(String, usize)>,
|
||||
/// Parallel to `objects`: base object flat index (`# Child = > Parent`).
|
||||
pub object_bases: Vec<Option<usize>>,
|
||||
/// (object_fn_idx, method_name) → method fn idx
|
||||
pub methods: Vec<(usize, String, usize)>,
|
||||
/// Parallel to `functions`: parent fn index for dynamic `call_fn` lookup.
|
||||
|
||||
+46
-11
@@ -402,25 +402,38 @@ impl Vm {
|
||||
}
|
||||
vals.reverse();
|
||||
let type_name = instance_type_name_bc(&recv).map_err(|m| self.err_at(span, m))?;
|
||||
let obj_idx = program
|
||||
let start_oi = program
|
||||
.objects
|
||||
.iter()
|
||||
.find(|(n, _)| n == &type_name)
|
||||
.map(|(_, i)| *i)
|
||||
.position(|(n, _)| n == &type_name)
|
||||
.ok_or_else(|| {
|
||||
self.err_at(span, format!("unknown object type `{type_name}`"))
|
||||
})?;
|
||||
let method_fid = program
|
||||
.methods
|
||||
.iter()
|
||||
.find(|(o, m, _)| *o == obj_idx && m == &method_name)
|
||||
.map(|(_, _, i)| *i)
|
||||
.ok_or_else(|| {
|
||||
let method_fid = resolve_method_fid(program, start_oi, &method_name).ok_or_else(
|
||||
|| {
|
||||
let mut chain = Vec::new();
|
||||
let mut oi = Some(start_oi);
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
while let Some(i) = oi {
|
||||
if !seen.insert(i) {
|
||||
break;
|
||||
}
|
||||
chain.push(program.objects[i].0.as_str());
|
||||
oi = program.object_bases.get(i).copied().flatten().and_then(
|
||||
|base_flat| {
|
||||
program.objects.iter().position(|(_, f)| *f == base_flat)
|
||||
},
|
||||
);
|
||||
}
|
||||
self.err_at(
|
||||
span,
|
||||
format!("unknown method `{type_name}.{method_name}`"),
|
||||
format!(
|
||||
"unknown method `{type_name}.{method_name}` (searched {})",
|
||||
chain.join(" → ")
|
||||
),
|
||||
)
|
||||
})?;
|
||||
},
|
||||
)?;
|
||||
let callee = &program.functions[method_fid];
|
||||
if callee.params.len() != argc {
|
||||
return Err(self.err_at(span, "method argument count mismatch"));
|
||||
@@ -546,6 +559,28 @@ impl Vm {
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_method_fid(program: &Program, start_oi: usize, method_name: &str) -> Option<usize> {
|
||||
let mut oi = Some(start_oi);
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
while let Some(i) = oi {
|
||||
if !seen.insert(i) {
|
||||
break;
|
||||
}
|
||||
let obj_flat = program.objects[i].1;
|
||||
if let Some((_, _, fid)) = program
|
||||
.methods
|
||||
.iter()
|
||||
.find(|(o, m, _)| *o == obj_flat && m == method_name)
|
||||
{
|
||||
return Some(*fid);
|
||||
}
|
||||
oi = program.object_bases.get(i).copied().flatten().and_then(|base_flat| {
|
||||
program.objects.iter().position(|(_, f)| *f == base_flat)
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn pop(stack: &mut Vec<Value>) -> Result<Value, String> {
|
||||
stack
|
||||
.pop()
|
||||
|
||||
Reference in new issue
Block a user