Make static always infer type, add function patcher to go.fnl demo

This commit is contained in:
Jeremy Penner 2023-12-30 12:04:47 -05:00
parent 1d6d033e5d
commit 30eb3b4393
2 changed files with 26 additions and 8 deletions

26
go.fnl
View file

@ -9,17 +9,33 @@
(var result (SDL.CreateWindowAndRenderer w h flags (& window) (& renderer))) (var result (SDL.CreateWindowAndRenderer w h flags (& window) (& renderer)))
(return result window renderer))) (return result window renderer)))
(local last-event (printform (static SDL.Event (q (SDL.Event {type 0}))))) (local clear (static (def [renderer [SDL.Renderer]]
(SDL.SetRenderDrawColor renderer 0 0 0 255)
(SDL.RenderClear renderer))))
(local hello (def [renderer [SDL.Renderer]]
(clear renderer)
(SDL.SetRenderDrawColor renderer 0 0 255 255)
(var rect (SDL.Rect {x 100 y 100 w 50 h 30}))
(SDL.RenderFillRect renderer (& rect))
(SDL.RenderPresent renderer)))
(local last-event (printform (static (q (SDL.Event {type 0})))))
(fn event-loop [f] (fn event-loop [f]
(while (not= (SDL.PollEvent (last-event:getpointer)) 0) (f (last-event:get)))) (while (not= (SDL.PollEvent (last-event:getpointer)) 0) (f (last-event:get))))
(local rect (def [x int y int w int h int] (local rect (def [x int y int w int h int]
(return (SDL.Rect {x x y y w w h h})))) (return (SDL.Rect {x x y y w w h h}))))
(fn patch-clear [r g b]
(let [new-clear (def [renderer [SDL.Renderer]]
(SDL.SetRenderDrawColor renderer r g b 255)
(SDL.RenderClear renderer))]
((def [] (set clear new-clear))))) ; no idea why (clear:set new-clear) doesn't work
(SDL.Init SDL.INIT_EVERYTHING) (SDL.Init SDL.INIT_EVERYTHING)
(let [windowflags SDL.WINDOW_RESIZABLE (let [windowflags SDL.WINDOW_RESIZABLE
(ok window renderer) (unpacktuple (CreateWindowAndRenderer 640 480 windowflags))] (ok window renderer) (unpacktuple (CreateWindowAndRenderer 640 480 windowflags))]
(SDL.SetRenderDrawColor renderer 0 0 0 255) (event-loop #nil)
(SDL.RenderClear renderer) (hello renderer)
(SDL.RenderPresent renderer) {: window : renderer : rect : event-loop : clear : hello : patch-clear})
{: window : renderer : rect : event-loop})

View file

@ -207,6 +207,8 @@
(block (.. "terra (" argdefs ")" rettyp) stmts :end scope #(comp.expr $1 scope)))) (block (.. "terra (" argdefs ")" rettyp) stmts :end scope #(comp.expr $1 scope))))
(.. "(function () local terra anonfn :: " ((. type-constructors "->") arglist scope) " return anonfn end)()"))) (.. "(function () local terra anonfn :: " ((. type-constructors "->") arglist scope) " return anonfn end)()")))
(set forms.def comp.def)
(fn forms.return [vals scope] (.. "return " (commasep vals #(comp.expr $1 scope)))) (fn forms.return [vals scope] (.. "return " (commasep vals #(comp.expr $1 scope))))
(fn forms.cast [[typ expr] scope] (fn forms.cast [[typ expr] scope]
@ -260,8 +262,8 @@
(indented #(comp.expr last-expr scope) scope) "\n" (indented #(comp.expr last-expr scope) scope) "\n"
(indent :end scope))))) (indent :end scope)))))
(fn comp.global [[typ initial-val] scope] (fn comp.global [[initial-val] scope]
(.. "global(" (comp.type typ scope) "," (scope:expr initial-val) ")")) (.. "global(" (scope:expr initial-val) ")"))
(fn build [expr compiler] (fn build [expr compiler]
(let [scope (new-scope) (let [scope (new-scope)
@ -276,7 +278,7 @@
(fn ttype [expr] (build expr comp.type)) (fn ttype [expr] (build expr comp.type))
(fn tquote [...] (build [...] comp.quote)) (fn tquote [...] (build [...] comp.quote))
(fn def [...] (build [...] comp.def)) (fn def [...] (build [...] comp.def))
(fn static [typ initial-value] (build [typ initial-value] comp.global)) (fn static [initial-value] (build [initial-value] comp.global))
(fn eval [expr] `(,(def `[] `(return ,expr)))) (fn eval [expr] `(,(def `[] `(return ,expr))))
(fn compile [compiler-name expr] (build expr (. comp (tostring compiler-name)))) (fn compile [compiler-name expr] (build expr (. comp (tostring compiler-name))))