aboutsummaryrefslogtreecommitdiff
path: root/lib/object.nom
blob: 9229d21850be247c2b1e001cd2c270152a971423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use "core"

# TODO: make codegen less verbose

lua> "CLASSES = {}"

immediately
    compile [@, me] to: Lua value "self"
action [new %classname %inst]
    =lua "setmetatable(\%inst, CLASSES[\%classname])"
immediately
    parse [new %classname] as: new %classname {}

parse [as %instance %body] as
    lua> "local self;"
    do
        using
            lua> ".."
                self = \%instance
                local cls = self.class
                local old_self = self.class:set_self(self)
                ACTIONS = cls.ACTIONS
                COMPILE_ACTIONS = cls.COMPILE_ACTIONS
                ARG_ORDERS = cls.ARG_ORDERS
        ..do
            %body
    ..then always
        lua> ".."
            self.class:set_self(old_self)

parse [object %classname %class_body] as
    using
        %cls <- {..}
            name:%classname
            ACTIONS:=lua "ACTIONS", COMPILE_ACTIONS:=lua "COMPILE_ACTIONS"
            ARG_ORDERS:=lua "ARG_ORDERS"
        (=lua "CLASSES").%classname <- %cls
        lua> ".."
            setmetatable(\%cls, {__tostring=function() return \%classname end})
            local self = nil
            \%cls.set_self = function(_, inst)
                local old_self = self
                self = inst
                return old_self
            end
            \%cls.__index = \%cls
            \%cls.class = \%cls
        %class_body
        run ".."
            action [new \%classname %inst]
                say "NEWING"
                return: =lua "setmetatable(\\%inst, \\%cls)"
        lua> ".."
            if ACTIONS["as text"] then
                \%cls.__tostring = ACTIONS["as text"]
            end