From 496f8a3fbbc1e60994a83f13c2240e2fdf6bbf91 Mon Sep 17 00:00:00 2001 From: ManDude <7569514+ManDude@users.noreply.github.com> Date: Sat, 17 Apr 2021 02:50:38 +0100 Subject: [PATCH] Add `case` macro for switch-case constructs (#362) --- goal_src/goal-lib.gc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/goal_src/goal-lib.gc b/goal_src/goal-lib.gc index 46a5ce838..a838e182f 100644 --- a/goal_src/goal-lib.gc +++ b/goal_src/goal-lib.gc @@ -353,6 +353,20 @@ `(none) ) +(defmacro case (switch &rest cases) + "A switch-case construct. switch is saved onto a local variable and compared against each case, sequentially. + else can be used as the default case, but it must be the last one." + + (with-gensyms (sw) + `(let ((sw ,switch)) + (cond ,@(apply + (lambda (x) `(,@(if (eq? (first x) 'else) `(else ,@(rest x)) `((= sw ,(first x)) ,@(rest x))))) + cases) + ) + ) + ) + ) + ;;;;;;;;;;;;;;;;;;; ;; Math Macros ;;;;;;;;;;;;;;;;;;; @@ -531,7 +545,7 @@ ;; (Fake) MIPS Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; these are macros for MIPS instructions which we may want to keep in the source code for +;; these are macros for MIPS instructions which we may want to keep in the source code for ;; readibility/curiosity/documentation, but will not translate into any actual instructions at all ;; A macro that generates a macro for the specified instruction