In the last part I said that we were going to be doing some refactoring. Let’s start with a little of that and a recap. The first thing that we’re going to do is remove define and if from our language. We’ll put them back in later, but first let’s look at what our language looks like with only scheme-syntax defined.

(use srfi-69) (define global-syntax-definitions (make-hash-table)) (define frame (make-hash-table)) (define (set-symbol! symbol value) (hash-table-set! frame symbol value)) (define (lookup-variable-value symbol) (if (hash-table-exists? frame symbol) (hash-table-ref frame symbol) "Error: Unbound variable")) (define (self-evaluating? expr) (or (number? expr) (string? expr) (char? expr) (boolean? expr))) (define (lispy-eval expr) (cond ((self-evaluating? expr) expr) ((symbol? expr) (lookup-variable-value expr)) (else (if (hash-table-exists? global-syntax-definitions (car expr)) ((hash-table-ref global-syntax-definitions (car expr)) (cdr expr)) "Not implemented")))) (hash-table-set! global-syntax-definitions 'scheme-syntax (lambda (expr) (hash-table-set! global-syntax-definitions (car expr) (eval (cadr expr))))) (define (repl) (define input (read)) (print ";===> " (lispy-eval input)) (repl))

Let’s take a moment to consider what these 25 lines of code can do. We now have a language that can understand self-evaluating Scheme values and lookup symbol values. In addition to that it has exactly one primitive, a macro that defines other primitives and gives us access to the underlying Scheme.

You may consider this language lacking, and I admit it does not have many features. However it is now possible to define any language construct (including procedures, eval and apply) from within Lispy with the help of scheme-syntax and the Scheme underneath. After writing these 25 lines we could define the rest of the language 100% from within Lispy.

However there are a few more things that conceptually belong in the core language. One of those things is the ability to load a Lispy file. This will let us break out our syntax definitions into another file and load it automatically before we start the repl.

(use srfi-69) (define global-syntax-definitions (make-hash-table)) (define frame (make-hash-table)) (define (set-symbol! symbol value) (hash-table-set! frame symbol value)) (define (lookup-variable-value symbol) (if (hash-table-exists? frame symbol) (hash-table-ref frame symbol) "Error: Unbound variable")) (define (self-evaluating? expr) (or (number? expr) (string? expr) (char? expr) (boolean? expr))) (define (lispy-eval expr) (cond ((self-evaluating? expr) expr) ((symbol? expr) (lookup-variable-value expr)) (else (if (hash-table-exists? global-syntax-definitions (car expr)) ((hash-table-ref global-syntax-definitions (car expr)) (cdr expr)) "Not implemented")))) (hash-table-set! global-syntax-definitions 'scheme-syntax (lambda (expr) (hash-table-set! global-syntax-definitions (car expr) (eval (cadr expr))))) (hash-table-set! global-syntax-definitions 'load (lambda (expr) (define f (open-input-file (car expr))) (let loop ((e (read f))) (if (equal? e #!eof) "Successfully Loaded!" (begin (lispy-eval e) (loop (read f))))))) (define (repl) (define input (read)) (print ";===> " (lispy-eval input)) (repl))

Now that we have load, we need something to load. Create a new file called syntax.chicken, or whatever you prefer. This is where we will define our primitive syntax forms like define, if and lambda.

(use srfi-69) (define global-syntax-definitions (make-hash-table)) (define frame (make-hash-table)) (define (set-symbol! symbol value) (hash-table-set! frame symbol value)) (define (lookup-variable-value symbol) (if (hash-table-exists? frame symbol) (hash-table-ref frame symbol) "Error: Unbound variable")) (define (self-evaluating? expr) (or (number? expr) (string? expr) (char? expr) (boolean? expr))) (define (lispy-eval expr) (cond ((self-evaluating? expr) expr) ((symbol? expr) (lookup-variable-value expr)) (else (if (hash-table-exists? global-syntax-definitions (car expr)) ((hash-table-ref global-syntax-definitions (car expr)) (cdr expr)) "Not implemented")))) (hash-table-set! global-syntax-definitions 'scheme-syntax (lambda (expr) (hash-table-set! global-syntax-definitions (car expr) (eval (cadr expr))))) (hash-table-set! global-syntax-definitions 'load (lambda (expr) (define f (open-input-file (car expr))) (let loop ((e (read f))) (if (equal? e #!eof) "Successfully Loaded!" (begin (lispy-eval e) (loop (read f))))))) ((hash-table-ref global-syntax-definitions 'load) '("syntax.chicken")) (define (repl) (define input (read)) (print ";===> " (lispy-eval input)) (repl))

syntax.chicken

(scheme-syntax define (lambda (expr) (set-symbol! (car expr) (lispy-eval (cadr expr))))) (scheme-syntax if (lambda (expr) (if (lispy-eval (car expr)) (lispy-eval (cadr expr)) (lispy-eval (caddr expr)))))

(repl) (define a 5) ;===> #<unspecified> a ;===> 5 (if 1 2 3) ;===> 2

Now that we have implemented load we are one step closer to having a platform that we can build any language we want with. To change languages all we would have to do is load a different syntax file.

I encourage you to play around with that concept for a little while. Write two different syntax.chicken files and implement a couple of basic forms like define, or and if. Try to make your two implementations as different as possible even if making it different doesn’t make sense. For instance in one implementation if could take a mandatory keyword before the alternate branch (if pred consequent else alt) or it could act like and, taking two predicates and only evaluating the consequent branch if both predicates evaluate to true.

Since one of the goals of Lispy is to experiment with language features this ability will come in very useful. In the future we will extend this concept to make our interpreter more of a language platform than a language itself.

This article is a little short on code because we made some larger conceptual changes that I did not want to breeze past. In the next article we’ll implement primitive procedures and apply. The ability to define procedures from within Lispy is just around the corner.

GOTO Part 5 | Primitive Procedures and apply