tomhrr / dale

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

Add the posibility to define global values in macros inside functions (and other macros) #134

Open porky11 opened 8 years ago

porky11 commented 8 years ago

I don't know, how this should be done.

If eval-expression could also define functions, it would be done this way:

(using-namespace std.macros
  (def double (macro intern (a)
    (let ((val int)
          (T \ (type-of mc a false))
          (expr \ (qq do
            (def double (fn intern (uq T) ((a (uq T)))
              (+ a a)))
              0)))
    (if (and (eval-expression mc (q int) (qq  * x y) (cast (# val) (p void)))
                (exists-fn mc (qq (uq T) double (uq T))))
      (qq double (uq a))
      (qq no-op))))))
porky11 commented 7 years ago

Now I know, how this could be done, but the previous suggestion was stupid, since eval-expession only evaluates funciton calls

porky11 commented 7 years ago

Currently it's not possible, to define something at macroexpansion time (see #134) This would be useful for macros which automatically define functions:

(def + (macro intern (a b)
  …
  (instantiate-as-function + type-of-a)
  (if (or (exists-fn …) (exists-macro …))
    (qq + (uq a) (uq b))
    (report-error …))))

A solution may be implementing def (and other top-level-forms) as a function or macro, which expand to functions (attr cto), that manipulate the internal objects (introspection also is implemented as internal functions, and has access to the internal information, so I don't think, this should be too difficult). Since everything is implemented as function, functions have to be able to be called at toplevel, and are evaluated at compile time. (this is already kind of possible. When defining non-auto variables, the initializer gets evaluated at compile time, and is not inside a function or macro) This is similar to lisp, but functions in lisp would normally be implemented as function pointers or even more complicated objects. So you could also write:

(printf "a")
;;instead of
(def macro-print (macro intern (a)
  (printf a)
  (q 0)))
(macro-print "a")