tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.02k stars 48 forks source link

Namespaces in macros #154

Open porky11 opened 7 years ago

porky11 commented 7 years ago

It would be nice to be able to get the namespace in introspection Needed functions: (symbol-namespace mc name) returns namespace of a variable or extern-c function (symbol-namespace mc name args) returns namespace of a function or macro called with args (current-namespace mc) returns the current namespace used by macro (parent-namespace mc namespace) returns the parent namespace of a namespace

Some macro, that would be useful too: since . means root namespace .. may mean parent-namespace too


(using-namespace std.macros
(import macros)
(import cstdlib)

(def debug (macro intern (name rest) ;;use some debug-version of the function
  (def args (var auto \ (get-varargs-list mc (- (arg-count mc) 1) rest)))
  (def namespace (symbol-namespace name args)) ;;namespace of a symbol which takes args as arguments
  (if (not (null args))
    (qq using-namespace (uq namespace)
      (uql args))
    (do
      (make-node mc)))))

(namespace a
  (def a (fn intern void (void)
    (printf "a\n")))
  (namespace debug
    (def a (macro intern (rest)
      (printf "called a with wrong number of arguments\n")
      (exit 1)
      (make-node mc)))))
)
(def main (fn extern-c void (void)
  (using-namespace a
  (using-namespace b
    (debug a)
    (debug a 1))))