tomhrr / dale

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

Restricted behaviour for intern/extern variables #163

Closed porky11 closed 6 years ago

porky11 commented 7 years ago

The body of intern/extern varialbes will be set at compile time. This should be documented somewhere: (def test (var intern int (printf "hello\n"))) ;;gets printed at compile-time

No init functions get called on these variables. It is also not possible to define initializers on other types:

(import cstdio)

(def monster (struct intern ((a int))))

(def init (fn intern bool ((a (ref monster)))
  (printf "init monster\n")
  true))

(def setf-copy-init (fn intern bool ((a (p monster)) (b (p (const int))))
  (printf "init monster copy\n")
  true))

(def setf-move-init (fn intern bool ((a (p monster)) (b (rv-ref (const int))))
  (printf "init monster mov\n")
  true))

(def monster0 (var intern monster)) ;;nothing gets printed
(def monster1 (var intern monster 1)) ;;error, wrong type

(def main (fn extern-c void (void)
  (let ((a monster)
        (b monster 2))  ;;these work as expected
    (do))))

Extern functions (extern-c) are not yet accessible:

(def glCreateProgram (fn extern-c uint (void)))

(def program (var intern uint (glCreateProgram))) ;;here glCreateProgram is not yet accessible

(def main (fn extern-c void (void)
  (setv program (glCreateProgram)))) ;here it is accessible

(compile with flag -lGL, opengl functions may not be a good example, you would define them manually anyway, to ensure that everyting important is already initialized)

tomhrr commented 6 years ago

Thanks for this. Initialisation of non-procedure-scoped variables has now been documented.

If a library is specified via the -l flag, it will not be available at compile time. The -a flag can be used to make a dynamic library's symbols available at compile time.