2016-03-03 04:59:07 -05:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/extra.h"
|
|
|
|
|
|
|
|
static const char boot_rom[][80] = {
|
2017-04-02 06:16:25 -04:00
|
|
|
"(core#define-macro call-with-current-environment (core#lambda (form env) (list (",
|
|
|
|
"cadr form) env))) (core#define here (call-with-current-environment (core#lambda ",
|
|
|
|
"(env) env))) (core#define the (core#lambda (var) (make-identifier var here))) (c",
|
|
|
|
"ore#define the-builtin-define (the (core#quote core#define))) (core#define the-b",
|
|
|
|
"uiltin-lambda (the (core#quote core#lambda))) (core#define the-builtin-begin (th",
|
|
|
|
"e (core#quote core#begin))) (core#define the-builtin-quote (the (core#quote core",
|
|
|
|
"#quote))) (core#define the-builtin-set! (the (core#quote core#set!))) (core#defi",
|
|
|
|
"ne the-builtin-if (the (core#quote core#if))) (core#define the-builtin-define-ma",
|
|
|
|
"cro (the (core#quote core#define-macro))) (core#define the-define (the (core#quo",
|
|
|
|
"te define))) (core#define the-lambda (the (core#quote lambda))) (core#define the",
|
|
|
|
"-begin (the (core#quote begin))) (core#define the-quote (the (core#quote quote))",
|
|
|
|
") (core#define the-set! (the (core#quote set!))) (core#define the-if (the (core#",
|
|
|
|
"quote if))) (core#define the-define-macro (the (core#quote define-macro))) (core",
|
|
|
|
"#define-macro quote (core#lambda (form env) (core#if (= (length form) 2) (list t",
|
|
|
|
"he-builtin-quote (cadr form)) (error \"illegal quote form\" form)))) (core#define-",
|
|
|
|
"macro if (core#lambda (form env) ((core#lambda (len) (core#if (= len 4) (cons th",
|
|
|
|
"e-builtin-if (cdr form)) (core#if (= len 3) (list the-builtin-if (list-ref form ",
|
|
|
|
"1) (list-ref form 2) #undefined) (error \"illegal if form\" form)))) (length form)",
|
|
|
|
"))) (core#define-macro begin (core#lambda (form env) ((core#lambda (len) (if (= ",
|
|
|
|
"len 1) #undefined (if (= len 2) (cadr form) (if (= len 3) (cons the-builtin-begi",
|
|
|
|
"n (cdr form)) (list the-builtin-begin (cadr form) (cons the-begin (cddr form))))",
|
|
|
|
"))) (length form)))) (core#define-macro set! (core#lambda (form env) (if (= (len",
|
|
|
|
"gth form) 3) (if (identifier? (cadr form)) (cons the-builtin-set! (cdr form)) (e",
|
|
|
|
"rror \"illegal set! form\" form)) (error \"illegal set! form\" form)))) (core#define",
|
|
|
|
" check-formal (core#lambda (formal) (if (null? formal) #t (if (identifier? forma",
|
|
|
|
"l) #t (if (pair? formal) (if (identifier? (car formal)) (check-formal (cdr forma",
|
|
|
|
"l)) #f) #f))))) (core#define-macro lambda (core#lambda (form env) (if (= (length",
|
|
|
|
" form) 1) (error \"illegal lambda form\" form) (if (check-formal (cadr form)) (lis",
|
|
|
|
"t the-builtin-lambda (cadr form) (cons the-begin (cddr form))) (error \"illegal l",
|
|
|
|
"ambda form\" form))))) (core#define-macro define (lambda (form env) ((lambda (len",
|
|
|
|
") (if (= len 1) (error \"illegal define form\" form) (if (identifier? (cadr form))",
|
|
|
|
" (if (= len 3) (cons the-builtin-define (cdr form)) (error \"illegal define form\"",
|
|
|
|
" form)) (if (pair? (cadr form)) (list the-define (car (cadr form)) (cons the-lam",
|
|
|
|
"bda (cons (cdr (cadr form)) (cddr form)))) (error \"define: binding to non-varaib",
|
|
|
|
"le object\" form))))) (length form)))) (core#define-macro define-macro (lambda (f",
|
|
|
|
"orm env) (if (= (length form) 3) (if (identifier? (cadr form)) (cons the-builtin",
|
|
|
|
"-define-macro (cdr form)) (error \"define-macro: binding to non-variable object\" ",
|
|
|
|
"form)) (error \"illegal define-macro form\" form)))) (define-macro syntax-error (l",
|
|
|
|
"ambda (form _) (apply error (cdr form)))) (define-macro define-auxiliary-syntax ",
|
|
|
|
"(lambda (form _) (define message (string-append \"invalid use of auxiliary syntax",
|
|
|
|
": '\" (symbol->string (cadr form)) \"'\")) (list the-define-macro (cadr form) (list",
|
|
|
|
" the-lambda '_ (list (the 'error) message))))) (define-auxiliary-syntax else) (d",
|
|
|
|
"efine-auxiliary-syntax =>) (define-auxiliary-syntax unquote) (define-auxiliary-s",
|
|
|
|
"yntax unquote-splicing) (define-auxiliary-syntax syntax-unquote) (define-auxilia",
|
|
|
|
"ry-syntax syntax-unquote-splicing) (define-macro let (lambda (form env) (if (ide",
|
|
|
|
"ntifier? (cadr form)) (list (list the-lambda '() (list the-define (cadr form) (c",
|
|
|
|
"ons the-lambda (cons (map car (car (cddr form))) (cdr (cddr form))))) (cons (cad",
|
|
|
|
"r form) (map cadr (car (cddr form)))))) (cons (cons the-lambda (cons (map car (c",
|
|
|
|
"adr form)) (cddr form))) (map cadr (cadr form)))))) (define-macro and (lambda (f",
|
|
|
|
"orm env) (if (null? (cdr form)) #t (if (null? (cddr form)) (cadr form) (list the",
|
|
|
|
"-if (cadr form) (cons (the 'and) (cddr form)) #f))))) (define-macro or (lambda (",
|
|
|
|
"form env) (if (null? (cdr form)) #f (let ((tmp (make-identifier 'it env))) (list",
|
|
|
|
" (the 'let) (list (list tmp (cadr form))) (list the-if tmp tmp (cons (the 'or) (",
|
|
|
|
"cddr form)))))))) (define-macro cond (lambda (form env) (let ((clauses (cdr form",
|
|
|
|
"))) (if (null? clauses) #undefined (let ((clause (car clauses))) (if (and (ident",
|
|
|
|
"ifier? (car clause)) (identifier=? (the 'else) (make-identifier (car clause) env",
|
|
|
|
"))) (cons the-begin (cdr clause)) (if (null? (cdr clause)) (let ((tmp (make-iden",
|
|
|
|
"tifier 'tmp here))) (list (the 'let) (list (list tmp (car clause))) (list the-if",
|
|
|
|
" tmp tmp (cons (the 'cond) (cdr clauses))))) (if (and (identifier? (cadr clause)",
|
|
|
|
") (identifier=? (the '=>) (make-identifier (cadr clause) env))) (let ((tmp (make",
|
|
|
|
"-identifier 'tmp here))) (list (the 'let) (list (list tmp (car clause))) (list t",
|
|
|
|
"he-if tmp (list (car (cddr clause)) tmp) (cons (the 'cond) (cdr clauses))))) (li",
|
|
|
|
"st the-if (car clause) (cons the-begin (cdr clause)) (cons (the 'cond) (cdr clau",
|
|
|
|
"ses))))))))))) (define-macro quasiquote (lambda (form env) (define (quasiquote? ",
|
|
|
|
"form) (and (pair? form) (identifier? (car form)) (identifier=? (the 'quasiquote)",
|
|
|
|
" (make-identifier (car form) env)))) (define (unquote? form) (and (pair? form) (",
|
|
|
|
"identifier? (car form)) (identifier=? (the 'unquote) (make-identifier (car form)",
|
|
|
|
" env)))) (define (unquote-splicing? form) (and (pair? form) (pair? (car form)) (",
|
|
|
|
"identifier? (caar form)) (identifier=? (the 'unquote-splicing) (make-identifier ",
|
|
|
|
"(caar form) env)))) (define (qq depth expr) (cond ((unquote? expr) (if (= depth ",
|
|
|
|
"1) (car (cdr expr)) (list (the 'list) (list (the 'quote) (the 'unquote)) (qq (- ",
|
|
|
|
"depth 1) (car (cdr expr)))))) ((unquote-splicing? expr) (if (= depth 1) (list (t",
|
|
|
|
"he 'append) (car (cdr (car expr))) (qq depth (cdr expr))) (list (the 'cons) (lis",
|
|
|
|
"t (the 'list) (list (the 'quote) (the 'unquote-splicing)) (qq (- depth 1) (car (",
|
|
|
|
"cdr (car expr))))) (qq depth (cdr expr))))) ((quasiquote? expr) (list (the 'list",
|
|
|
|
") (list (the 'quote) (the 'quasiquote)) (qq (+ depth 1) (car (cdr expr))))) ((pa",
|
|
|
|
"ir? expr) (list (the 'cons) (qq depth (car expr)) (qq depth (cdr expr)))) ((vect",
|
|
|
|
"or? expr) (list (the 'list->vector) (qq depth (vector->list expr)))) (else (list",
|
|
|
|
" (the 'quote) expr)))) (let ((x (cadr form))) (qq 1 x)))) (define-macro let* (la",
|
|
|
|
"mbda (form env) (let ((bindings (car (cdr form))) (body (cdr (cdr form)))) (if (",
|
|
|
|
"null? bindings) `(,(the 'let) () ,@body) `(,(the 'let) ((,(car (car bindings)) ,",
|
|
|
|
"@(cdr (car bindings)))) (,(the 'let*) (,@(cdr bindings)) ,@body)))))) (define-ma",
|
|
|
|
"cro letrec (lambda (form env) `(,(the 'letrec*) ,@(cdr form)))) (define-macro le",
|
|
|
|
"trec* (lambda (form env) (let ((bindings (car (cdr form))) (body (cdr (cdr form)",
|
|
|
|
"))) (let ((variables (map (lambda (v) `(,v #f)) (map car bindings))) (initials (",
|
|
|
|
"map (lambda (v) `(,(the 'set!) ,@v)) bindings))) `(,(the 'let) (,@variables) ,@i",
|
|
|
|
"nitials ,@body))))) (define-macro let-values (lambda (form env) `(,(the 'let*-va",
|
|
|
|
"lues) ,@(cdr form)))) (define-macro let*-values (lambda (form env) (let ((formal",
|
|
|
|
" (car (cdr form))) (body (cdr (cdr form)))) (if (null? formal) `(,(the 'let) () ",
|
|
|
|
",@body) `(,(the 'call-with-values) (,the-lambda () ,@(cdr (car formal))) (,(the ",
|
|
|
|
"'lambda) (,@(car (car formal))) (,(the 'let*-values) (,@(cdr formal)) ,@body))))",
|
|
|
|
"))) (define-macro define-values (lambda (form env) (let ((formal (car (cdr form)",
|
|
|
|
")) (body (cdr (cdr form)))) (let ((arguments (make-identifier 'arguments here)))",
|
|
|
|
" `(,the-begin ,@(let loop ((formal formal)) (if (pair? formal) `((,the-define ,(",
|
|
|
|
"car formal) #undefined) ,@(loop (cdr formal))) (if (identifier? formal) `((,the-",
|
|
|
|
"define ,formal #undefined)) '()))) (,(the 'call-with-values) (,the-lambda () ,@b",
|
|
|
|
"ody) (,the-lambda ,arguments ,@(let loop ((formal formal) (args arguments)) (if ",
|
|
|
|
"(pair? formal) `((,the-set! ,(car formal) (,(the 'car) ,args)) ,@(loop (cdr form",
|
|
|
|
"al) `(,(the 'cdr) ,args))) (if (identifier? formal) `((,the-set! ,formal ,args))",
|
|
|
|
" '())))))))))) (define-macro do (lambda (form env) (let ((bindings (car (cdr for",
|
|
|
|
"m))) (test (car (car (cdr (cdr form))))) (cleanup (cdr (car (cdr (cdr form))))) ",
|
|
|
|
"(body (cdr (cdr (cdr form))))) (let ((loop (make-identifier 'loop here))) `(,(th",
|
|
|
|
"e 'let) ,loop ,(map (lambda (x) `(,(car x) ,(cadr x))) bindings) (,the-if ,test ",
|
|
|
|
"(,the-begin ,@cleanup) (,the-begin ,@body (,loop ,@(map (lambda (x) (if (null? (",
|
|
|
|
"cdr (cdr x))) (car x) (car (cdr (cdr x))))) bindings))))))))) (define-macro when",
|
|
|
|
" (lambda (form env) (let ((test (car (cdr form))) (body (cdr (cdr form)))) `(,th",
|
|
|
|
"e-if ,test (,the-begin ,@body) #undefined)))) (define-macro unless (lambda (form",
|
|
|
|
" env) (let ((test (car (cdr form))) (body (cdr (cdr form)))) `(,the-if ,test #un",
|
|
|
|
"defined (,the-begin ,@body))))) (define-macro case (lambda (form env) (let ((key",
|
|
|
|
" (car (cdr form))) (clauses (cdr (cdr form)))) (let ((the-key (make-identifier '",
|
|
|
|
"key here))) `(,(the 'let) ((,the-key ,key)) ,(let loop ((clauses clauses)) (if (",
|
|
|
|
"null? clauses) #undefined (let ((clause (car clauses))) `(,the-if ,(if (and (ide",
|
|
|
|
"ntifier? (car clause)) (identifier=? (the 'else) (make-identifier (car clause) e",
|
|
|
|
"nv))) #t `(,(the 'or) ,@(map (lambda (x) `(,(the 'eqv?) ,the-key (,the-quote ,x)",
|
|
|
|
")) (car clause)))) ,(if (and (identifier? (cadr clause)) (identifier=? (the '=>)",
|
|
|
|
" (make-identifier (cadr clause) env))) `(,(car (cdr (cdr clause))) ,the-key) `(,",
|
|
|
|
"the-begin ,@(cdr clause))) ,(loop (cdr clauses))))))))))) (define-macro paramete",
|
|
|
|
"rize (lambda (form env) (let ((formal (car (cdr form))) (body (cdr (cdr form))))",
|
|
|
|
" `(,(the 'with-dynamic-environment) (,(the 'list) ,@(map (lambda (x) `(,(the 'co",
|
|
|
|
"ns) ,(car x) ,(cadr x))) formal)) (,the-lambda () ,@body))))) (define-macro synt",
|
|
|
|
"ax-quote (lambda (form env) (let ((renames '())) (letrec ((rename (lambda (var) ",
|
|
|
|
"(let ((x (assq var renames))) (if x (cadr x) (begin (set! renames `((,var ,(make",
|
|
|
|
"-identifier var env) (,(the 'make-identifier) ',var ',env)) unquote renames)) (r",
|
|
|
|
"ename var)))))) (walk (lambda (f form) (cond ((identifier? form) (f form)) ((pai",
|
|
|
|
"r? form) `(,(the 'cons) (walk f (car form)) (walk f (cdr form)))) ((vector? form",
|
|
|
|
") `(,(the 'list->vector) (walk f (vector->list form)))) (else `(,(the 'quote) ,f",
|
|
|
|
"orm)))))) (let ((form (walk rename (cadr form)))) `(,(the 'let) ,(map cdr rename",
|
|
|
|
"s) ,form)))))) (define-macro syntax-quasiquote (lambda (form env) (let ((renames",
|
|
|
|
" '())) (letrec ((rename (lambda (var) (let ((x (assq var renames))) (if x (cadr ",
|
|
|
|
"x) (begin (set! renames `((,var ,(make-identifier var env) (,(the 'make-identifi",
|
|
|
|
"er) ',var ',env)) unquote renames)) (rename var))))))) (define (syntax-quasiquot",
|
|
|
|
"e? form) (and (pair? form) (identifier? (car form)) (identifier=? (the 'syntax-q",
|
|
|
|
"uasiquote) (make-identifier (car form) env)))) (define (syntax-unquote? form) (a",
|
|
|
|
"nd (pair? form) (identifier? (car form)) (identifier=? (the 'syntax-unquote) (ma",
|
|
|
|
"ke-identifier (car form) env)))) (define (syntax-unquote-splicing? form) (and (p",
|
|
|
|
"air? form) (pair? (car form)) (identifier? (caar form)) (identifier=? (the 'synt",
|
|
|
|
"ax-unquote-splicing) (make-identifier (caar form) env)))) (define (qq depth expr",
|
|
|
|
") (cond ((syntax-unquote? expr) (if (= depth 1) (car (cdr expr)) (list (the 'lis",
|
|
|
|
"t) (list (the 'quote) (the 'syntax-unquote)) (qq (- depth 1) (car (cdr expr)))))",
|
|
|
|
") ((syntax-unquote-splicing? expr) (if (= depth 1) (list (the 'append) (car (cdr",
|
|
|
|
" (car expr))) (qq depth (cdr expr))) (list (the 'cons) (list (the 'list) (list (",
|
|
|
|
"the 'quote) (the 'syntax-unquote-splicing)) (qq (- depth 1) (car (cdr (car expr)",
|
|
|
|
")))) (qq depth (cdr expr))))) ((syntax-quasiquote? expr) (list (the 'list) (list",
|
|
|
|
" (the 'quote) (the 'quasiquote)) (qq (+ depth 1) (car (cdr expr))))) ((pair? exp",
|
|
|
|
"r) (list (the 'cons) (qq depth (car expr)) (qq depth (cdr expr)))) ((vector? exp",
|
|
|
|
"r) (list (the 'list->vector) (qq depth (vector->list expr)))) ((identifier? expr",
|
|
|
|
") (rename expr)) (else (list (the 'quote) expr)))) (let ((body (qq 1 (cadr form)",
|
|
|
|
"))) `(,(the 'let) ,(map cdr renames) ,body)))))) (define (transformer f) (lambda",
|
|
|
|
" (form env) (let ((ephemeron1 (make-ephemeron-table)) (ephemeron2 (make-ephemero",
|
|
|
|
"n-table))) (letrec ((wrap (lambda (var1) (let ((var2 (ephemeron1 var1))) (if var",
|
|
|
|
"2 (cdr var2) (let ((var2 (make-identifier var1 env))) (ephemeron1 var1 var2) (ep",
|
|
|
|
"hemeron2 var2 var1) var2))))) (unwrap (lambda (var2) (let ((var1 (ephemeron2 var",
|
|
|
|
"2))) (if var1 (cdr var1) var2)))) (walk (lambda (f form) (cond ((identifier? for",
|
|
|
|
"m) (f form)) ((pair? form) (cons (walk f (car form)) (walk f (cdr form)))) ((vec",
|
|
|
|
"tor? form) (list->vector (walk f (vector->list form)))) (else form))))) (let ((f",
|
|
|
|
"orm (cdr form))) (walk unwrap (apply f (walk wrap form)))))))) (define-macro def",
|
|
|
|
"ine-syntax (lambda (form env) (let ((formal (car (cdr form))) (body (cdr (cdr fo",
|
|
|
|
"rm)))) (if (pair? formal) `(,(the 'define-syntax) ,(car formal) (,the-lambda ,(c",
|
|
|
|
"dr formal) ,@body)) `(,the-define-macro ,formal (,(the 'transformer) (,the-begin",
|
|
|
|
" ,@body))))))) (define-macro letrec-syntax (lambda (form env) (let ((formal (car",
|
|
|
|
" (cdr form))) (body (cdr (cdr form)))) `(let () ,@(map (lambda (x) `(,(the 'defi",
|
|
|
|
"ne-syntax) ,(car x) ,(cadr x))) formal) ,@body)))) (define-macro let-syntax (lam",
|
|
|
|
"bda (form env) `(,(the 'letrec-syntax) ,@(cdr form)))) ",
|
2015-01-31 07:14:14 -05:00
|
|
|
};
|
2014-09-08 10:31:04 -04:00
|
|
|
|
2016-03-03 04:59:07 -05:00
|
|
|
void
|
|
|
|
pic_boot(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_load_cstr(pic, &boot_rom[0][0]);
|
|
|
|
}
|