| 1 |
|
| 2 |
(import (ikarus) (ikarus.debugger)) |
| 3 |
|
| 4 |
(define (operator? x) |
| 5 |
(and (pair? x) |
| 6 |
(eq? (car x) 'primitive) |
| 7 |
(guard (con [(assertion-violation? con) #t]) |
| 8 |
(system-value (cadr x)) |
| 9 |
#f))) |
| 10 |
|
| 11 |
(define (get-src/expr ae) |
| 12 |
(if (annotation? ae) |
| 13 |
(cons (annotation-source ae) (annotation-stripped ae)) |
| 14 |
(cons #f (syntax->datum ae)))) |
| 15 |
|
| 16 |
(define (add-debug-calls expr) |
| 17 |
(define who 'add-debug-calls) |
| 18 |
(define (direct-call? op rands) |
| 19 |
(define n (length rands)) |
| 20 |
(define (test cls*) |
| 21 |
(and (pair? cls*) |
| 22 |
(or |
| 23 |
(let ([fmls (caar cls*)]) |
| 24 |
(and (list? fmls) (= (length fmls) n))) |
| 25 |
(test (cdr cls*))))) |
| 26 |
(and (pair? op) |
| 27 |
(case (car op) |
| 28 |
[(lambda) (test (list (cdr op)))] |
| 29 |
[(case-lambda) (test (cdr op))] |
| 30 |
[(annotated-case-lambda) (test (cddr op))] |
| 31 |
[else #f]))) |
| 32 |
(define (E-call src/expr op rands) |
| 33 |
(cond |
| 34 |
[(or (operator? op) (direct-call? op rands)) |
| 35 |
`(,(E op) ,@(map E rands))] |
| 36 |
[else |
| 37 |
`(',debug-call ',src/expr ,(E op) ,@(map E rands))])) |
| 38 |
(define (E expr) |
| 39 |
(cond |
| 40 |
[(symbol? expr) expr] |
| 41 |
[(and (pair? expr) (list? expr)) |
| 42 |
(let ([a (car expr)] [d (cdr expr)]) |
| 43 |
(case a |
| 44 |
[(quote) expr] |
| 45 |
[(primitive) expr] |
| 46 |
[(set!) `(set! ,(car d) ,(E (cadr d)))] |
| 47 |
[(if) `(if ,(E (car d)) ,(E (cadr d)) ,(E (caddr d)))] |
| 48 |
[(begin) (cons 'begin (map E d))] |
| 49 |
[(lambda) (list 'lambda (car d) (E (cadr d)))] |
| 50 |
[(case-lambda) |
| 51 |
(cons 'case-lambda |
| 52 |
(map (lambda (x) (list (car x) (E (cadr x)))) d))] |
| 53 |
[(annotated-case-lambda) |
| 54 |
(cons* 'annotated-case-lambda (car d) |
| 55 |
(map (lambda (x) (list (car x) (E (cadr x)))) (cdr d)))] |
| 56 |
[(letrec letrec*) |
| 57 |
(list a |
| 58 |
(map (lambda (x) (list (car x) (E (cadr x)))) (car d)) |
| 59 |
(E (cadr d)))] |
| 60 |
[(foreign-call) |
| 61 |
(cons* 'foreign-call (car d) (map E (cdr d)))] |
| 62 |
[(library-letrec*) |
| 63 |
(list a |
| 64 |
(map (lambda (x) (list (car x) (cadr x) (E (caddr x)))) (car d)) |
| 65 |
(E (cadr d)))] |
| 66 |
[(annotated-call) |
| 67 |
(E-call (get-src/expr (car d)) (cadr d) (cddr d))] |
| 68 |
[else (E-call #f a d)]))] |
| 69 |
[else |
| 70 |
(die who "invalid expression" expr)])) |
| 71 |
(E expr)) |
| 72 |
|
| 73 |
(define (start-repl) |
| 74 |
(display "Ikarus Interpreter\n\n") |
| 75 |
(new-cafe |
| 76 |
(lambda (x) |
| 77 |
(guarded-start |
| 78 |
(lambda () |
| 79 |
(eval x (interaction-environment))))))) |
| 80 |
|
| 81 |
(define (start-script script-name args) |
| 82 |
(command-line-arguments (cons script-name args)) |
| 83 |
(guarded-start |
| 84 |
(lambda () |
| 85 |
(load-r6rs-script script-name #f #t)))) |
| 86 |
|
| 87 |
(current-core-eval |
| 88 |
(let ([ev (current-core-eval)]) |
| 89 |
(lambda (x) |
| 90 |
(let ([x (add-debug-calls x)]) |
| 91 |
(ev x))))) |
| 92 |
|
| 93 |
(apply |
| 94 |
(case-lambda |
| 95 |
[(interpreter flag script-name . rest) |
| 96 |
(if (string=? flag "--r6rs-script") |
| 97 |
(start-script script-name rest) |
| 98 |
(error interpreter "invalid args" (cons* flag script-name rest)))] |
| 99 |
[(interpreter) (start-repl)] |
| 100 |
[(interpreter . rest) |
| 101 |
(error interpreter "invalid args" rest)]) |
| 102 |
(command-line-arguments)) |