This commit is contained in:
Victor Fors 2021-09-19 23:25:22 +02:00
parent b35a815aa3
commit ae8603b120

View File

@ -327,55 +327,77 @@
result result
(loop (cdr ln))))))) (loop (cdr ln)))))))
(define (lisp body environments exit) (define lisp-builtins
(define (eval body) `((test . ,(lambda (function-args)
(lisp body environments exit)) (show "test function called")))
(define (reference symbol) (if . ,(lambda (function-args)
(cdr (any-or (curry assoc symbol) environments (exit (string-append "Undefined reference: " (symbol->string symbol)))))) (match function-args
; (define (apply function function-args) ((e x y) (if (lisp-eval e)
; (if (lisp-eval x)
(if (atom? body) (lisp-eval y)))
(if (symbol? body) (_ (exit "malformed if expression")))))
(reference body) (quote . ,(lambda (function-args)
body) (match function-args
(let ((function-name (car body))
(function-args (cdr body)))
(if (symbol? function-name)
(case function-name
((test) (show "test function called"))
((if) (match function-args
((e x y) (if (eval e)
(eval x)
(eval y)))
(_ (exit "malformed if expression"))))
((quote) (match function-args
((v) v) ((v) v)
(_ (exit "malformed quote expression")))) (_ (exit "malformed quote expression")))))
((cons) (match function-args (cons . ,(lambda (function-args)
(match function-args
((a b) (cons (eval a) (eval b))) ((a b) (cons (eval a) (eval b)))
(_ (exit "malformed cons expression")))) (_ (exit "malformed cons expression")))))
((car) (match function-args (car . ,(lambda (function-args)
(match function-args
((a) (let ((ae (eval a))) ((a) (let ((ae (eval a)))
(if (atom? ae) (if (atom? ae)
(exit "tried to take car of atom") (exit "tried to take car of atom")
(car (eval a))))) (car (eval a)))))
(_ (exit "malformed car expression")))) (_ (exit "malformed car expression")))))
((cdr) (match function-args (cdr . ,(lambda (function-args)
((a) (cdr (eval a))))) (match function-args
((atom) (match function-args ((a) (cdr (eval a))))))
(atom . ,(lambda (function-args)
(match function-args
((a) (atom? (eval a))) ((a) (atom? (eval a)))
(_ (exit "malformed atom expression")))) (_ (exit "malformed atom expression")))))
((eq) (match function-args (eq . ,(lambda (function-args)
(match function-args
((a b) (equal? (eval a) (eval b))) ((a b) (equal? (eval a) (eval b)))
(_ (exit "malformed eval expression")))) (_ (exit "malformed eval expression")))))
; ((set) (match function-args (set . ,(lambda (function-args)
; ((a b) (if ( (match function-args
((lambda) (match function-args ((a b) (if (symbol? a)
(bind a b))))))
(lambda . ,(lambda (function-args)
match function-args
((args exp . exps) ((args exp . exps)
(if (and (list? args) (every symbol? args)) (if (and (list? args) (every symbol? args))
(cons args (cons exp exps)) (cons args (cons exp exps))
(exit "malformed lambda expression"))) (exit "malformed lambda expression"))
(_ (exit "malformed lambda expression")))) (_ (exit "malformed lambda expression")))))))
(define (lisp body environments exit)
(define (lisp-eval body)
(cond ((atom? body) body)
((symbol? body) (reference body))
((list? body) (let ((ln (map lisp-eval body)))
(apply (car ln) (cdr ln))))
(else (exit "Unknown value type in evaluation."))))
(define (reference symbol)
(cdr (any-or (curry assoc symbol) environments (exit (string-append "Undefined reference: " (symbol->string symbol))))))
(define (bind name value)
(set! environments (cons (let loop ((environment (car environments)))
(if (null? environment)
(list (cons name value))
(if (eq? name (caar environment))
(cons (cons name value) (cdr environment))
(loop (cdr environment)))))
(cdr environments))))
(define (lisp-apply function args)
(if (function? function)
(apply function args)
(case function-name
(else (let ((function (reference function-name environments))) (else (let ((function (reference function-name environments)))
(let ((function-arguments (car function)) (let ((function-arguments (car function))
(argument-values (cdr body)) (argument-values (cdr body))
@ -387,27 +409,7 @@
(define (run-lisp body) (define (run-lisp body)
(call/cc (lambda (exit) (call/cc (lambda (exit)
(cons #t (lisp body '(()) (compose exit (curry cons #f))))))) (cons #t (lisp body (list lisp-builtins) (compose exit (curry cons #f)))))))
;; (if (and (list function)
;; (>= (length function) 2)
;; (list function-arguments)
;; (every symbol? (car function)))
(define +script-primitives+
`((if . ,(lambda (condition body1 body2)
(script (if (script condition)
body1
body2))))
(eq . ,(lambda (a b)
(equals? (script a) (script b))))
(and . ,(lambda (a b)
(and (script a) (script b))))
(or . ,(lambda (a b)
(or (script a) (script b))))
(not . ,(lambda (a)
(not (script a))))))
(define (print-room-description room) (define (print-room-description room)
(newline) (newline)
@ -588,7 +590,7 @@
(define (do-command-message tag message-tag message) (define (do-command-message tag message-tag message)
(if (not (and (symbol? tag) (symbol? message-tag) (string? message))) (if (not (and (symbol? tag) (symbol? message-tag) (string? message)))
(show "I didn't quite understand that") (show "I didn't quite understand that.")
(let ((object (match-object tag (visible-objects 'you)))) (let ((object (match-object tag (visible-objects 'you))))
(if (not object) (if (not object)
(show "You can't see that here.") (show "You can't see that here.")
@ -596,6 +598,13 @@
((enter) (set-enter-message object message)) ((enter) (set-enter-message object message))
(else (show "Invalid message name."))))))) (else (show "Invalid message name.")))))))
(define (do-command-goto tag)
(if (not (symbol? tag))
(show "I didn't quite understand that.")
(begin
(move-object 'you tag)
(print-room-description (get-container 'you)))))
(define +cardinal-sets+ (define +cardinal-sets+
'((north n) '((north n)
(northeast ne north-east) (northeast ne north-east)
@ -695,6 +704,7 @@
(('destroy x) (do-command-destroy x)) (('destroy x) (do-command-destroy x))
(('aliases x) (do-command-aliases x)) (('aliases x) (do-command-aliases x))
(('message x y z) (do-command-message x y z)) (('message x y z) (do-command-message x y z))
(('goto x) (do-command-goto x))
(_ (set! success #f))) (_ (set! success #f)))
(set! success #f)))) (set! success #f))))
success)) success))