Skip to content

Commit

Permalink
rewrote lisp if again as didnt handle constant true cases
Browse files Browse the repository at this point in the history
well enough. Also added the cond macro
  • Loading branch information
cbaggers committed Jul 5, 2015
1 parent f6f5b1e commit 56408b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
6 changes: 6 additions & 0 deletions language/macros.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

(in-package :varjo)

(v-defmacro :cond (&rest clauses)
`(if ,(caar clauses)
,(cadar clauses)
,@(when (rest clauses)
`((cond ,@(rest clauses))))))

(v-define-compiler-macro :+ (&rest numbers)
(cond
((= (length numbers) 1) (first numbers))
Expand Down
37 changes: 20 additions & 17 deletions language/special.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -488,24 +488,27 @@
:args-valid t
:return
(let* ((test-obj (varjo->glsl test-form env))
(always-true (or (eq test-form t)
(not (v-typep (code-type test-obj) 'v-bool))))
(then-obj (end-line (varjo->glsl then-form env)))
(else-obj (when else-form (end-line (varjo->glsl else-form env)))))

(when (not (v-typep (code-type test-obj) 'v-bool))
(setf test-obj (varjo->glsl t env))
(setf else-obj nil))

(when else-obj (assert (v-code-type-eq then-obj else-obj)))

(let ((result (free-name :result-from-if))
(result-type (type->type-spec (code-type then-obj))))
(expand->varjo->glsl
`(let (((,result ,result-type)))
(%if ,test-form
(setf ,result ,then-form)
,@(when else-form `((setf ,result ,else-form))))
,result)
env))))
(else-obj (if else-form
(end-line (varjo->glsl else-form env))
(if (not always-true)
(error "Type mismatch: else-case is nil which is of bool type, yet the then form is of ~s type."
(type->type-spec (code-type then-obj)))
(varjo->glsl nil env)))))
(if always-true
then-obj
(let ((result (free-name :result-from-if))
(result-type (type->type-spec (code-type then-obj))))
(when else-obj (assert (v-code-type-eq then-obj else-obj)))
(expand->varjo->glsl
`(let (((,result ,result-type)))
(%if ,test-form
(setf ,result ,then-form)
(setf ,result ,else-form))
,result)
env)))))

;; note that just like in lisp this only fails if false. 0 does not fail.
;; this is the if statement from gl. It has a return type type of :none
Expand Down

0 comments on commit 56408b8

Please sign in to comment.