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.
This commit is contained in:
parent
4b96515362
commit
723f52dc1d
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
Loading…
Reference in New Issue