From aa969e72a34cc3d6e39495a4210442a5e9c7952c Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Fri, 4 Nov 2022 23:08:52 +0100 Subject: Recursive state rotation using macro magic --- depre/main.scm | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/depre/main.scm b/depre/main.scm index 81bdb03..11b18fc 100644 --- a/depre/main.scm +++ b/depre/main.scm @@ -25,6 +25,18 @@ ((car fs)) (loop (cdr fs)))))) +;; Transition by chance, according to the state +;; Example: +;; - You are too tired for that, so you do this instead +;; - Or you actually do it +(define (f-or-tired f tired) + #f + ) + +(define (f-or-depressed f depressed) + #f + ) + ;; Simple I/O (define (answer message) (lambda () @@ -71,16 +83,29 @@ (loop (read))))))) ;; States: -(define start + +;; Some macro magic for indirection that enables recursion. +;; I don't really know how it works (yet!) +(define %states (make-hash-table)) +(define-macro (register-state name f) + `(hash-set! %states ,name ,f)) +(define-macro (to-state name) + `(lambda () ((hash-ref %states ,name)))) + +(register-state + 'begining + (state "This is the game beginning, as a test" + (list (cons "Return to start" (to-state 'start))))) + +(register-state + 'start (state "Hi, game starts" (list (cons "Start game" (combine (answer "Let's start, then.") - (state "heading" - (list (cons "hola" (lambda () (display "HOLA"))) - (cons "adios" (lambda () (display "adios"))))))) + (to-state 'begining))) (cons "Quit game" (answer "Good bye!"))) #:hide-status)) -(start) +((to-state 'start)) -- cgit v1.2.3