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:
cflmyandCursor committed 2026-08-10 10:01:05 +08:00
1 parent a23fdedfa2
commit 5456d9ff83
25 files changed
+590 -41

No files matched your search

+35 -7
View File
@@ -732,13 +732,20 @@ impl Interpreter {
.cloned()
.ok_or_else(|| self.err(format!("undefined variable `{recv_name}`")))?;
let type_name = instance_type_name(&recv).map_err(|m| self.err(m))?;
let (owner, obj) = find_object_type(module, &type_name)
.ok_or_else(|| self.err(format!("unknown object type `{type_name}`")))?;
let target = obj
.children
.iter()
.find(|c| c.name == method)
.ok_or_else(|| self.err(format!("unknown method `{type_name}.{method}`")))?;
if find_object_type(module, &type_name).is_none() {
return Err(self.err(format!("unknown object type `{type_name}`")));
}
let (owner, _obj, target) = find_method_along_bases(module, &type_name, method)
.ok_or_else(|| {
let chain = crate::inherit::inheritance_chain(module, &type_name)
.into_iter()
.map(|o| o.name.as_str())
.collect::<Vec<_>>()
.join(" → ");
self.err(format!(
"unknown method `{type_name}.{method}` (searched {chain})"
))
})?;
let bound = bind_function_args(self, module, fun, env, &target.params, ev_args, false)
.map_err(|m| self.err(m))?;
let mut call_env = Env::new();
@@ -918,6 +925,27 @@ fn find_object_type<'a>(module: &'a Module, name: &str) -> Option<(&'a Module, &
})
}
/// Resolve `` `obj`.method `` along the inheritance chain (`_type` first, then bases).
fn find_method_along_bases<'a>(
module: &'a Module,
type_name: &str,
method: &str,
) -> Option<(&'a Module, &'a Function, &'a Function)> {
let mut cur = type_name.to_string();
let mut seen = std::collections::HashSet::new();
while seen.insert(cur.clone()) {
let (owner, obj) = find_object_type(module, &cur)?;
if let Some(target) = obj.children.iter().find(|c| c.name == method) {
return Some((owner, obj, target));
}
match &obj.base {
Some(b) => cur = b.clone(),
None => break,
}
}
None
}
fn find_function_anywhere<'a>(module: &'a Module, name: &str) -> Option<&'a Function> {
for f in &module.functions {
if f.name == name {