#lang scheme/base
(require "parse.ss"
"../ns.ss"
"parse-tx.ss"
(for-template
scheme/base)
(for-syntax
"../tools.ss"
"parse-tx.ss"
scheme/base))
(provide
(all-defined-out)
(all-from-out "parse.ss")
(all-from-out "parse-tx.ss"))
(define-syntax (rpn-lambda stx)
(syntax-case stx ()
((_ . txs)
#`(lambda (p)
#,(foldr (lambda (compile expr)
(append (syntax->list compile)
(list #'p expr)))
#'p (syntax->list #'txs))))))
(define-syntax rpn:-compile
(syntax-rules ()
((_ (compile code ...)) (compile code ...))))
(define-syntax-rule (prefix-parsers namespace ((name arg ...) template) ...)
(ns namespace
(define-syntaxes (name ...)
(values (rpn-syntax-rules () ((_ arg ...) template)) ...))))
(define-syntax-rule (prefix-parsers/meta ns lang: (pat code) ...)
(begin
(prefix-parsers ns (pat (,(lang: . code))) ...)))
(define-syntax (rpn-lex stx)
(syntax-case stx ()
((_ compile str)
(let ((words
(port->syntax-list
(open-input-string (syntax->datum #'str)) stx)))
#`(compile #,@words)))))
(define-syntax (rpn-let-locals stx)
(syntax-case stx ()
((_ (namespace
program:
pop-values)
(formal ...) p sub)
(let ((flist (syntax->list #'(formal ...))))
#`(let-values (((p formal ...) (pop-values p #,(length flist))))
(ns namespace
(let ((formal (program: ',formal)) ...)
sub)))))))
(define (rpn-take-reversed n lst)
(let _take ((n n)
(in lst)
(out '()))
(if (or (zero? n)
(null? lst))
(values out in)
(_take (sub1 n)
(cdr in)
(cons (car in) out)))))
(define (rpn-apply->list fn args)
(let ((args/void
(call-with-values
(lambda () (apply fn args))
list)))
(if (void? (car args/void))
'()
args/void)))
(define (rpn-wrap-dynamic fn)
(lambda stack
(define (go n stack [optional '()])
(let-values (((args stack+) (rpn-take-reversed n stack)))
(append
(rpn-apply->list fn (append args optional))
stack+)))
(define (dispatch n)
(cond
((arity-at-least? n)
(go (arity-at-least-value n) (cdr stack) (car stack)))
((number? n) (go n stack))
((list? n) (dispatch (car n))) (else (error 'rpn-wrap-dynamic "~a" n))))
(dispatch (procedure-arity fn))))
(define-syntax (rpn-wrap-static stx)
(syntax-case stx ()
((_ nargs fn)
(let ((formals
(generate-temporaries
(build-list (syntax->datum #'nargs) add1))))
#`(lambda (#,@(reverse formals) . stack)
(cons (fn #,@formals) stack))))))
(define (rpn-wrap: . fns)
(lambda stack
(foldl apply
stack
(map rpn-wrap-dynamic fns))))
(define-syntax-rule (rpn-register-entry reg!)
(syntax-rules ()
((_ #f _ ) (void))
((_ #f _ . _) (syntax-error))
((_ name compile . code)
(reg! 'name (compile . code)))))