From 723f52dc1d61b007cb7f68555539332442cc7ba0 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Thu, 29 Apr 2010 23:57:42 -0500 Subject: [PATCH] Rather than a single form, accept an entire module as input. Module consists of mixed (declare ...) forms and expressions. Groups of (declare ...) forms become (letrec ...)s surrounding later expressions and (declare ...) groups. The (declare (fn-name . arglist) forms...) syntax is supported. --- compiler.scm | 3 ++- libcompiler/reader.scm | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 libcompiler/reader.scm diff --git a/compiler.scm b/compiler.scm index c6cfe56..fe9ecba 100755 --- a/compiler.scm +++ b/compiler.scm @@ -1,10 +1,11 @@ #! /usr/bin/mzscheme #lang scheme/base +(require (file "libcompiler/reader.scm")) (require (file "libcompiler/compiler.scm")) (require (file "libcompiler/writer.scm")) -(write-rla-value (compile-function `(lambda argv ,(read)))) +(write-rla-value (compile-function (read-module))) (write-char #\Newline) ; vim:set sw=2 expandtab: diff --git a/libcompiler/reader.scm b/libcompiler/reader.scm new file mode 100644 index 0000000..7b572a7 --- /dev/null +++ b/libcompiler/reader.scm @@ -0,0 +1,53 @@ +#lang scheme/base + +(require scheme/list) + +(provide read-module) + +(define (read-module [port (current-input-port)]) + `(lambda argv + ,@(let iter ([forms (read-forms port)] + [bindings '()]) + (cond + [(null? forms) (if (null? bindings) + '() + `(letrec ,bindings))] + [(simple-declare-form? (first forms)) + (iter (cdr forms) (cons (cdr (first forms)) + bindings))] + [(lambda-declare-form? (first forms)) + (iter (cdr forms) (cons `(,(first (second (first forms))) + (lambda ,(cdr (second (first forms))) + ,@(cddr (first forms)))) + bindings))] + [(begin-form? (first forms)) + (iter (append (cdr (first forms)) (cdr forms)) bindings)] + [(null? bindings) + (cons (first forms) (iter (cdr forms) '()))] + [else + `((letrec ,bindings ,@(iter forms '())))])))) + +(define (read-forms [port (current-input-port)]) + (reverse (let iter ([form (read port)] + [forms '()]) + (if (eof-object? form) + forms + (iter (read port) (cons form forms)))))) + +(define (simple-declare-form? form) + (and (pair? form) + (eq? (first form) 'declare) + (symbol? (second form)) + (null? (cdddr form)))) + +(define (lambda-declare-form? form) + (and (pair? form) + (eq? (first form) 'declare) + (pair? (second form)) + (symbol? (first (second form))))) + +(define (begin-form? form) + (and (pair? form) + (eq? (first form) 'begin))) + +; vim:set sw=2 expandtab: