1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00
mal/racket/step2_eval.rkt
Joel Martin efa2daef57 Fix empty list eval in step2 for most languages.
I think the only remaining ones are ada, elisp, factor, and rust.
2016-04-02 18:40:49 -05:00

50 lines
1.3 KiB
Racket
Executable File

#!/usr/bin/env racket
#lang racket
(require "types.rkt" "readline.rkt" "reader.rkt" "printer.rkt")
;; read
(define (READ str)
(read_str str))
;; eval
(define (eval-ast ast env)
(cond
[(symbol? ast)
(or (hash-ref env ast
(lambda () (raise (string-append "'"
(symbol->string ast)
"' not found")))))]
[(_sequential? ast) (_map (lambda (x) (EVAL x env)) ast)]
[(hash? ast) (make-hash
(dict-map ast (lambda (k v) (cons k (EVAL v env)))))]
[else ast]))
(define (EVAL ast env)
(if (or (not (list? ast)) (empty? ast))
(eval-ast ast env)
(let* ([el (eval-ast ast env)]
[f (first el)]
[args (rest el)])
(apply f args))))
;; print
(define (PRINT exp)
(pr_str exp true))
;; repl
(define repl-env (hash '+ + '- - '* * '/ /))
(define (rep str)
(PRINT (EVAL (READ str) repl-env)))
(define (repl-loop)
(let ([line (readline "user> ")])
(when (not (eq? nil line))
(with-handlers
([string? (lambda (exc) (printf "Error: ~a~n" exc))]
[blank-exn? (lambda (exc) null)])
(printf "~a~n" (rep line)))
(repl-loop))))
(repl-loop)