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

+17
View File
@@ -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(),