Tuesday, September 22, 2009

Referencing symbols in another namespace

Today, I got this code to DWIW:
#include <std/io>
#include <std/test>

namespace test {
    namespace hll: close :: test :: nested {
        namespace deeply {
            void goodbye() {
                say("Adios, amigo.");
            }
        }
    }


   
    void test() :main {
        using namespace ::test::nested::deeply;
        say("namespace-function-say");
        hello();
    }
   
    namespace nested::deeply {
        void hello() {
            say("Hello, world");
        }
    }
   
    namespace X {
        void xray() {
            say("Specs!");
        }
       
        namespace ::test::nested::deeply {
            void say(string what) {
                asm(what) {{ say %0 }};
            }
        }
    }
}


What I want, in this case, is for all of the various invocations of 'say', except the one in xray(), to refer to the say function declared down at the bottom.

There's a bunch of stuff I did wrong that I'd love to talk about, but it's late and I'm tired. So I'll say that compiler writers have things a lot easier than I thought - it's the guy writing the specs that does most of the heavy lifting. In this case, I'm writing a compiler with no spec, so every detail is a learning experience. (And let's face it: I'm old, and learning is painful.)

Anyway, here's the output:
.namespace []
.sub "anon"  :subid("post15")
.end


.HLL "close"

.namespace ["test";"nested";"deeply"]
.sub "say"  :subid("10_1253616492")
    .param pmc param_12
    .lex "what", param_12
    get_global $P13, "what"
 say $P13
    .return ()
.end


.HLL "close"

.namespace ["test";"nested";"deeply"]
.sub "hello"  :subid("11_1253616492")
    get_hll_global $P15, ["test";"nested";"deeply"], "say"
    $P16 = $P15("Hello, world")
    .return ($P16)
.end


.HLL "close"

.namespace ["test"]
.sub "test" :main :subid("12_1253616492")
    get_hll_global $P18, ["test";"nested";"deeply"], "say"
    $P18("namespace-function-say")
    get_hll_global $P19, ["test";"nested";"deeply"], "hello"
    $P20 = $P19()
    .return ($P20)
.end


.HLL "close"

.namespace ["test";"nested";"deeply"]
.sub "goodbye"  :subid("13_1253616492")
    get_hll_global $P22, ["test";"nested";"deeply"], "say"
    $P23 = $P22("Adios, amigo.")
    .return ($P23)
.end


.HLL "close"

.namespace ["test";"X"]
.sub "xray"  :subid("14_1253616492")
    get_global $P25, "say"
    $P26 = $P25("Specs!")
    .return ($P26)
.end

It's worth noting that I don't stop on an error - I keep trying to generate stuff. This will probably not be true in production - any error will cause an exit 1, etc. But for now, I need to see what's going on. Also, the order of generation is presently determined by the traversal order of some internal trees I've built. My next step will be to generate functions in the same order they appear in the source code. (With the caveat that variable initializers will get shuffled together.)

No comments:

Post a Comment