Updated things API for inheritance.

This commit is contained in:
Bruce Hill 2019-01-15 18:10:56 -08:00
parent e6d53864f7
commit 4e712fd8d3

View File

@ -12,7 +12,7 @@ test:
$barks = [: for $ in 1 to $its.barks: add "Bark!"] $barks = [: for $ in 1 to $its.barks: add "Bark!"]
return ($barks, joined with " ") return ($barks, joined with " ")
($it, gets pissed off) means: $it.barks += 1 ($its, get pissed off) means: $its.barks += 1
(Dog).genus = "Canus" (Dog).genus = "Canus"
$d = (a Dog with {.barks = 2}) $d = (a Dog with {.barks = 2})
assume "\$d" == "Dog {barks: 2}" assume "\$d" == "Dog {barks: 2}"
@ -21,7 +21,7 @@ test:
assume $d.barks == 2 assume $d.barks == 2
assume (($d, bark) == "Bark! Bark!") assume (($d, bark) == "Bark! Bark!")
assume (($d, woof) == "Bark! Bark!") assume (($d, woof) == "Bark! Bark!")
$d, gets pissed off $d, get pissed off
assume ($d.barks == 3) assume ($d.barks == 3)
assume (($d, bark) == "Bark! Bark! Bark!") assume (($d, bark) == "Bark! Bark! Bark!")
assume ($d.genus == "Canus") assume ($d.genus == "Canus")
@ -35,8 +35,7 @@ test:
with [$d = (a Dog with {.barks = 1})]: with [$d = (a Dog with {.barks = 1})]:
assume (($d, bark) == "Bark!") assume (($d, bark) == "Bark!")
a (Corgi) is a thing: a (Corgi) is a (Dog):
$it can [set up, gets pissed off] like a (Dog)
($it, as text) means "Dogloaf \{: for $k = $v in $it: add $k = $v}" ($it, as text) means "Dogloaf \{: for $k = $v in $it: add $k = $v}"
($its, sploot) means "sploooot" ($its, sploot) means "sploooot"
[($its, bark), ($its, woof)] all mean: [($its, bark), ($its, woof)] all mean:
@ -67,20 +66,6 @@ test:
assume assume
((Vec {.x = 1, .y = 2}) + (Vec {.x = 10, .y = 10})) != (Vec {.x = 0, .y = 0}) ((Vec {.x = 1, .y = 2}) + (Vec {.x = 10, .y = 10})) != (Vec {.x = 0, .y = 0})
[
$it can $actions like a $class, $it can $actions like an $class
$it has $actions like a $class, $it has $actions like an $class
$it $actions like a $class, $it $actions like an $class
] all compile to:
$lua = (Lua "")
$class_expr = ($class as lua expr)
$lines = []
for $a in $actions:
$lines,
add "\($it as lua expr).\($a.stub, as lua id) = \$class_expr.\($a.stub, as lua id)"
$lua, add $lines joined with "\n"
return $lua
$METAMETHOD_MAP = { $METAMETHOD_MAP = {
."as text" = "__tostring", ."clean up" = "__gc", ."+" = "__add", ."-" = "__sub" ."as text" = "__tostring", ."clean up" = "__gc", ."+" = "__add", ."-" = "__sub"
."*" = "__mul", ."/" = "__div", .negative = "__unm", ."//" = "__idiv" ."*" = "__mul", ."/" = "__div", .negative = "__unm", ."//" = "__idiv"
@ -92,7 +77,10 @@ $METAMETHOD_MAP = {
$($ as text like a dict) = ({}'s metatable).__tostring $($ as text like a dict) = ({}'s metatable).__tostring
external: external:
(a class named $classname with $members $(initialize $)) means: [
a $parent class named $classname with $members $(initialize $)
an $parent class named $classname with $members $(initialize $)
] all mean:
$class = {.__type = $classname} $class = {.__type = $classname}
$class.__index = $class $class.__index = $class
$class.class = $class $class.class = $class
@ -109,6 +97,7 @@ external:
fail "Cannot set \$key, it's not one of the allowed member fields." fail "Cannot set \$key, it's not one of the allowed member fields."
set $class's metatable to { set $class's metatable to {
.__index = $parent
.__tostring = ($class -> $class.__type) .__tostring = ($class -> $class.__type)
.__call = .__call =
for ($class with $initial_values): for ($class with $initial_values):
@ -128,33 +117,42 @@ external:
return $class return $class
[ [
a $classname is a thing with $members $class_body a $classname is a $parent with $members $class_body
an $classname is a thing with $members $class_body an $classname is a $parent with $members $class_body
a $classname is an $parent with $members $class_body
an $classname is an $parent with $members $class_body
] all compile to: ] all compile to:
$class_id = ($classname.stub, as lua id) $class_id = ($classname.stub, as lua id)
if $class_body:
$body_lua = ($class_body as lua)
$body_lua, remove free vars [$class_id]
$body_lua, declare locals
$lua = $lua =
Lua (" Lua ("
\$class_id = a_class_named_1_with(\(quote $classname.stub), \($members as lua)\( \$class_id = a_1_class_named_2_with(\($parent as lua), \(quote $classname.stub), \($members as lua)\(
( (
Lua (" Lua ("
, function(\$class_id) , function(\$class_id)
local it, its = \$class_id, \$class_id; local it, its = \$class_id, \$class_id;
\$body_lua \($class_body as lua)
end end
") ")
) if $class_body else "" ) if $class_body else ""
)) ))
a_\$class_id = function(initial_values) return \($classname.stub, as lua id)(initial_values or {}) end a_\$class_id = function(initial_values) return \$class_id(initial_values or {}) end
an_\$class_id, a_\($class_id)_with, an_\($class_id)_with = a_\$class_id, a_\$class_id, a_\$class_id an_\$class_id, a_\($class_id)_with, an_\($class_id)_with = a_\$class_id, a_\$class_id, a_\$class_id
") ")
$lua, add free vars [$class_id, "a_\$class_id", "an_\$class_id"] $lua, add free vars [$class_id, "a_\$class_id", "an_\$class_id"]
return $lua return $lua
[a $classname is a thing $class_body, an $classname is a thing] all parse as [
a $classname is a thing with (nil) $class_body a $classname is a thing with $members $class_body
an $classname is a thing with $members $class_body
a $classname is an thing with $members $class_body
an $classname is an thing with $members $class_body
] all parse as (a $classname is a (nil) with $members $class_body)
[a $classname is a thing $class_body, an $classname is a thing $class_body]
..all parse as (a $classname is a (nil) with (nil) $class_body)
[
a $classname is a $parent $class_body, an $classname is a $parent $class_body
a $classname is an $parent $class_body, an $classname is an $parent $class_body
] all parse as (a $classname is a $parent with (nil) $class_body)