aboutsummaryrefslogtreecommitdiff
path: root/lib/object.nom
blob: 27ef6c9c948b26e291f070ff4cfc180259594aa1 (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
use "core"

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 {}

immediately
    compile [call method %method] to: Lua value "self:\(%method as lua expr)"

parse [as %instance %body] as
    result of
        %old_self <- (me)
        (me) <- %instance
        try
            %body
        ..and if it barfs %msg
            (me) <- %old_self
            barf %msg
        ..or if it succeeds
            (me) <- %old_self

compile [object %classname %class_body] to
    %lua <-
        Lua ".."
            do
                local class = {name=\(%classname as lua expr)}
                setmetatable(class, {__tostring=function(cls) return cls.name end})
                CLASSES[class.name] = class
                class.__index = class
                class.class = class

    if: %class_body.type != "Block"
        %class_body <- [%class_body]
    for %statement in %class_body
        assume: (%statement.type is "Action") and (%statement.stub is "action % %")
        to %lua write "\n    class."
        to %lua write (%statement as lua)
    
    to %lua write ".."
            
            class.__tostring = class["A"..string.as_lua_id("as text")]
        end
    
    return %lua