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
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: inheritance rhs must be > Base
|
||||
---
|
||||
|
||||
# Foo = 1
|
||||
|
||||
# main
|
||||
|
||||
> print text=nope
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
title: cyclic inheritance
|
||||
---
|
||||
|
||||
# A = > B
|
||||
|
||||
# B = > A
|
||||
|
||||
# main
|
||||
|
||||
> print text=nope
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: unknown base type
|
||||
---
|
||||
|
||||
# Child = > MissingParent
|
||||
|
||||
# main
|
||||
|
||||
> print text=nope
|
||||
@@ -67,6 +67,24 @@ fn structure_object_handle() {
|
||||
assert_out("tests/structure/object-handle.mq.md", "counter\n3\n4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structure_inherit_parent_method() {
|
||||
assert_out(
|
||||
"tests/structure/inherit-parent-method.mq.md",
|
||||
"Loud
|
||||
Hello, world!",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structure_inherit_override() {
|
||||
assert_out(
|
||||
"tests/structure/inherit-override.mq.md",
|
||||
"Hello, world!
|
||||
HELLO, world!",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structure_nested_call() {
|
||||
assert_out("tests/structure/nested-call.mq.md", "Hello World!");
|
||||
@@ -457,6 +475,33 @@ fn error_table_row_marker_only() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_inherit_cycle() {
|
||||
assert_err(
|
||||
"tests/errors/inherit-cycle.mq.md",
|
||||
"5:1",
|
||||
"cyclic inheritance",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_inherit_unknown_base() {
|
||||
assert_err(
|
||||
"tests/errors/inherit-unknown-base.mq.md",
|
||||
"5:1",
|
||||
"unknown base type",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_inherit_bad_rhs() {
|
||||
assert_err(
|
||||
"tests/errors/inherit-bad-rhs.mq.md",
|
||||
"5:1",
|
||||
"object inheritance requires",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_bad_int() {
|
||||
assert_err(
|
||||
@@ -554,6 +599,26 @@ fn bytecode_hello() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytecode_inherit_parent_method() {
|
||||
assert_out_backend(
|
||||
"tests/structure/inherit-parent-method.mq.md",
|
||||
"bytecode",
|
||||
"Loud
|
||||
Hello, world!",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytecode_inherit_override() {
|
||||
assert_out_backend(
|
||||
"tests/structure/inherit-override.mq.md",
|
||||
"bytecode",
|
||||
"Hello, world!
|
||||
HELLO, world!",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytecode_branch() {
|
||||
assert_out_backend("tests/structure/branch.mq.md", "bytecode", "零");
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: inherit method override
|
||||
---
|
||||
|
||||
# Greeter
|
||||
|
||||
## hello
|
||||
+ `who`
|
||||
|
||||
*`msg` = "Hello, `who`!" *
|
||||
**`msg`**
|
||||
|
||||
# Loud = > Greeter
|
||||
|
||||
## hello
|
||||
+ `who`
|
||||
|
||||
*`msg` = "HELLO, `who`!" *
|
||||
**`msg`**
|
||||
|
||||
# main
|
||||
|
||||
*`g` = > Greeter *
|
||||
*`m` = > `g`.hello who=world *
|
||||
> print text=`m`
|
||||
*`l` = > Loud *
|
||||
*`m2` = > `l`.hello who=world *
|
||||
> print text=`m2`
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: inherit parent method
|
||||
> lib/json.mq.md
|
||||
---
|
||||
|
||||
# Greeter
|
||||
|
||||
## hello
|
||||
+ `who`
|
||||
|
||||
*`msg` = "Hello, `who`!" *
|
||||
**`msg`**
|
||||
|
||||
# Loud = > Greeter
|
||||
|
||||
# main
|
||||
|
||||
*`l` = > Loud *
|
||||
*`ty` = > json.get value=`l` key=_type *
|
||||
> print text=`ty`
|
||||
*`m` = > `l`.hello who=world *
|
||||
> print text=`m`
|
||||
Reference in new issue
Block a user