diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 19:04:34 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-12-30 19:04:45 -0800 |
| commit | 8a3c32408733a2f5e14f8a2dbafa3f980b2f73a1 (patch) | |
| tree | 68f1e8a8b956c33ed24cc7a6a369fd97b8849321 /lib/object.nom | |
| parent | 359152da1772ce501609edd8f84b4985ed3e42f2 (diff) | |
Update to new syntax.
Diffstat (limited to 'lib/object.nom')
| -rw-r--r-- | lib/object.nom | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/lib/object.nom b/lib/object.nom index 6f16410..72e1588 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -1,14 +1,16 @@ -#!/usr/bin/env nomsu -V5.12.12.8 +#!/usr/bin/env nomsu -V6.12.12.8 # This file contains the implementation of an Object-Oriented programming system. -$globals.METAMETHOD_MAP = {..} - "as text": "__tostring", "clean up": "__gc", "+ 1": "__add", "- 1": "__sub" - "* 1": "__mul", "/ 1": "__div", "-": "__unm", "// 1": "__idiv", "mod 1": "__mod" - "^ 1": "__pow", "& 1": "__band", "| 1": "__bor", "~ 1": "__bxor", "~": "__bnot" - "<< 1": "__bshl", ">> 1": "__bshr", "== 1": "__eq", "< 1": "__lt", "<= 1": "__le" - "set 1 = 2": "__newindex", size: "__len", iterate: "__ipairs" - "iterate all": "__pairs" +$globals.METAMETHOD_MAP = { + ."as text" = "__tostring", ."clean up" = "__gc", ."+ 1" = "__add" + ."- 1" = "__sub", ."* 1" = "__mul", ."/ 1" = "__div", ."-" = "__unm" + ."// 1" = "__idiv", ."mod 1" = "__mod", ."^ 1" = "__pow", ."& 1" = "__band" + ."| 1" = "__bor", ."~ 1" = "__bxor", ."~" = "__bnot", ."<< 1" = "__bshl" + .">> 1" = "__bshr", ."== 1" = "__eq", ."< 1" = "__lt", ."<= 1" = "__le" + ."set 1 = 2" = "__newindex", .size = "__len", .iterate = "__ipairs" + ."iterate all" = "__pairs" +} test: object (Dog): @@ -18,45 +20,45 @@ test: my action [bark, woof]: $barks = [: for $ in 1 to $me.barks: add "Bark!"] - return ($barks|joined with " ") + return ($barks, joined with " ") my action [get pissed off]: $me.barks += 1 - $d = (Dog {barks: 2}) + $d = (Dog {.barks = 2}) assume (type of $d) == "Dog" assume ($d is a "Dog") assume $d.barks == 2 - assume (($d|bark) == "Bark! Bark!") - assume (($d|woof) == "Bark! Bark!") - $d|get pissed off + assume (($d, bark) == "Bark! Bark!") + assume (($d, woof) == "Bark! Bark!") + $d, get pissed off assume ($d.barks == 3) - assume (($d|bark) == "Bark! Bark! Bark!") + assume (($d, bark) == "Bark! Bark! Bark!") assume ($d.genus == "Canus") assume ("\($d.class)" == "Dog") assume ($d.genus == "Canus") assume ($d.barks == 3) $d2 = (Dog {}) assume ($d2.barks == 0) or barf "Default initializer failed" - with {$d: Dog {barks: 1}}: - assume (($d|bark) == "Bark!") + with [$d = (Dog {.barks = 1})]: + assume (($d, bark) == "Bark!") object (Corgi) extends (Dog): my action [sploot] "splooted" my action [bark, woof]: $barks = [: for $ in 1 to $me.barks: add "Yip!"] - return ($barks|joined with " ") + return ($barks, joined with " ") $corg = (Corgi {}) assume ($corg.barks == 0) - with {$d: Corgi {barks: 1}}: - assume (($d|sploot) == "splooted") or barf "subclass method failed" - assume (($d|bark) == "Yip!") or barf "inheritance failed" - assume (($d|woof) == "Yip!") + with [$d = (Corgi {.barks = 1})]: + assume (($d, sploot) == "splooted") or barf "subclass method failed" + assume (($d, bark) == "Yip!") or barf "inheritance failed" + assume (($d, woof) == "Yip!") - with {$d: Dog {barks: 2}}: - assume (($d|bark) == "Bark! Bark!") + with [$d = (Dog {.barks = 2})]: + assume (($d, bark) == "Bark! Bark!") (my action $actions $body) compiles to: - lua> " + lua> (" local fn_name = \$actions[1].stub:as_lua_id() local \$args = List(\$actions[1]:get_args()) table.insert(\$args, 1, \(\$me)) @@ -73,19 +75,20 @@ test: lua:add(\(\($alias_args -> $actions.1) as lua)) end end - return lua" + return lua + ") (object $classname extends $parent $class_body) compiles to: unless ($classname.type == "Action"): - compile error at $classname \ + compile error at $classname .."Expected this to be an action, not a \$classname.type" for $ in $classname: unless ($ is text): compile error at $ "Class names should not have arguments." - return (..) - Lua " + return + Lua (" do local class = {name=\(quote $classname.stub)} class.__type = class.name @@ -109,7 +112,8 @@ test: for stub,metamethod in pairs(globals.METAMETHOD_MAP) do class[metamethod] = class[stub:as_lua_id()] end - end" + end + ") -(object $classname $class_body) parses as (..) +(object $classname $class_body) parses as object $classname extends (nil) $class_body |
