|
1 |
| -#lang racket/base |
| 1 | +#lang racket/load |
2 | 2 |
|
3 | 3 | ; Arc Compiler.
|
4 | 4 |
|
5 |
| - |
6 | 5 | (require
|
7 |
| - |
8 | 6 | ; This defines names like _list, so it would conflict with the
|
9 | 7 | ; naming convention for Arc global variables if we didn't prefix it.
|
10 | 8 | (prefix-in ffi: ffi/unsafe)
|
|
28 | 26 |
|
29 | 27 | (only-in "brackets.rkt" bracket-readtable)
|
30 | 28 |
|
31 |
| - (for-syntax racket/base)) |
| 29 | + (for-syntax racket/base) |
| 30 | + ) |
32 | 31 |
|
33 |
| -(provide (all-defined-out)) |
| 32 | +; (provide (all-defined-out)) |
34 | 33 |
|
| 34 | +(read-accept-bar-quote #f) |
35 | 35 |
|
36 | 36 | (define-runtime-path ac-rkt-path "ac.rkt")
|
37 | 37 | (define-runtime-path arc-arc-path "arc.arc")
|
|
41 | 41 | (define main-namespace
|
42 | 42 | (namespace-anchor->namespace main-namespace-anchor))
|
43 | 43 |
|
| 44 | +(define lang* (make-parameter 'arc)) |
| 45 | + |
| 46 | +(define ac-global-names '( |
| 47 | + (do %do) |
| 48 | + (cons %cons) |
| 49 | + (car %car) |
| 50 | + (cdr %cdr) |
| 51 | + (caar %caar) |
| 52 | + (cadr %cadr) |
| 53 | + (cdar %cdar) |
| 54 | + (cadar %cadar) |
| 55 | + ; (_ %_) |
| 56 | + (new %new) |
| 57 | + (parameterize %parameterize) |
| 58 | + (split-at %split-at) |
| 59 | + (nand %nand) |
| 60 | + (kill-thread %kill-thread) |
| 61 | + (break-thread %break-thread) |
| 62 | + (thread-send %thread-send) |
| 63 | + (thread-receive %thread-receive) |
| 64 | + (thread-try-receive %thread-try-receive) |
| 65 | + (thread-rewind-receive %thread-rewind-receive) |
| 66 | + (write %write) |
| 67 | + (require %require) |
| 68 | + (normalize-path %normalize-path) |
| 69 | + (base64-encode %base64-encode) |
| 70 | + (base64-decode %base64-decode) |
| 71 | + (print %print) |
| 72 | + (uuid %uuid) |
| 73 | + (file-size %file-size) |
| 74 | + (cddr %cddr) |
| 75 | + (list %list) |
| 76 | + (and %and) |
| 77 | + (or %or) |
| 78 | + (assoc %assoc) |
| 79 | + (let %let) |
| 80 | + (for %for) |
| 81 | + (when %when) |
| 82 | + (unless %unless) |
| 83 | + (+ %add) |
| 84 | + (- %sub) |
| 85 | + (* %mul) |
| 86 | + (/ %divide) |
| 87 | + (% %mod) |
| 88 | + (= %eql) |
| 89 | + (< %lt) |
| 90 | + (<= %le) |
| 91 | + (>= %ge) |
| 92 | + (> %gt) |
| 93 | + (empty %empty) |
| 94 | + (map %map) |
| 95 | + (map1 %map1) |
| 96 | + (map2 %map2) |
| 97 | + (all %all) |
| 98 | + (any %any) |
| 99 | + (apply %apply) |
| 100 | + (eval %eval) |
| 101 | + (uniq %uniq) |
| 102 | + (system %system) |
| 103 | + (fill-table %fill-table) |
| 104 | + (tokens %tokens) |
| 105 | + (tag %tag) |
| 106 | + (link %link) |
| 107 | + (private %private) |
| 108 | + (member %member) |
| 109 | + (concat %concat) |
| 110 | + (compose %compose) |
| 111 | + (last %last) |
| 112 | + (keep %keep) |
| 113 | + (case %case) |
| 114 | + (set %set) |
| 115 | + (eof %eof) |
| 116 | + (string %string) |
| 117 | + (read %read) |
| 118 | + (max %max) |
| 119 | + (min %min) |
| 120 | + (abs %abs) |
| 121 | + (round %round) |
| 122 | + (sort %sort) |
| 123 | + (quasiquote %quasiquote) |
| 124 | + (time %time) |
| 125 | + (date %date) |
| 126 | + (count %count) |
| 127 | + (nor %nor) |
| 128 | + (only %only) |
| 129 | + (load %load) |
| 130 | + (range %range) |
| 131 | + (thread %thread) |
| 132 | + (foldl %foldl) |
| 133 | + (foldr %foldr) |
| 134 | + (partition %parition) |
| 135 | + (curry %curry) |
| 136 | + (const %const) |
| 137 | + (force %force) |
| 138 | + (delay %delay) |
| 139 | + (read-json %read-json) |
| 140 | + (write-json %write-json) |
| 141 | + )) |
| 142 | + |
44 | 143 | (define (ac-global-name s)
|
45 |
| - (string->symbol (string-append "_" (symbol->string s)))) |
| 144 | + (if (eqv? (lang*) 'arc) |
| 145 | + (let ((x (assoc s ac-global-names))) |
| 146 | + (if ;#f |
| 147 | + x |
| 148 | + (cadr x) (string->symbol (string-append "" (symbol->string s))))) |
| 149 | + s)) |
46 | 150 |
|
47 | 151 | (define init-steps-reversed* (list))
|
48 | 152 |
|
|
111 | 215 | ; env is a list of lexically bound variables, which we
|
112 | 216 | ; need in order to decide whether set should create a global.
|
113 | 217 |
|
| 218 | +(define (id-literal? x) |
| 219 | + (and (symbol? x) (> (string-length (symbol->string x)) 0) (eqv? #\| (string-ref (symbol->string x) 0)))) |
| 220 | + |
| 221 | +(define (id-literal x) |
| 222 | + (let ((s (substring (symbol->string x) 1 (- (string-length (symbol->string x)) 1)))) |
| 223 | + (when (> (string-length s) 0) |
| 224 | + (string->symbol s)))) |
| 225 | + |
| 226 | +(define (ac-do body env) |
| 227 | + (cons 'begin (ac-body* body env))) |
| 228 | + |
114 | 229 | (defarc (ac s env)
|
115 | 230 | (cond [(string? s) (ac-string s env)]
|
116 |
| - [(literal? s) (list 'quote s)] |
| 231 | + ((keyword? s) s) |
| 232 | + ((literal? s) (if (eqv? (lang*) 'arc) (list 'quote (ac-quoted s)) s)) |
| 233 | + ((id-literal? s) (id-literal s)) |
117 | 234 | [(ssyntax? s) (ac (expand-ssyntax s) env)]
|
118 | 235 | [(symbol? s) (ac-var-ref s env)]
|
119 | 236 | [(ssyntax? (xcar s)) (ac (cons (expand-ssyntax (car s)) (cdr s)) env)]
|
| 237 | + ((eq? (xcar s) '%id) (cadr s)) |
| 238 | + ((eq? (xcar s) '%arc) (parameterize ((lang* 'arc)) (ac-do (cdr s) env))) |
| 239 | + ((eq? (xcar s) '%do) (parameterize ((lang* 'arc)) (ac-do (cdr s) env))) |
| 240 | + ; ((eq? (xcar s) 'begin)(parameterize ((lang* 'rkt)) (ac-do (cadr s) env))) |
| 241 | + ((eq? (xcar s) '%scm) (parameterize ((lang* 'scm)) (ac-do (cadr s) env))) |
| 242 | + ((eq? (xcar s) '%rkt) (parameterize ((lang* 'rkt)) (ac-do (cadr s) env))) |
120 | 243 | [(eq? (xcar s) '$) (ac-$ (cadr s) env)]
|
121 |
| - [(eq? (xcar s) 'quote) (list 'quote (ac-quoted (cadr s)))] |
| 244 | + ((eq? (xcar s) 'quote) (if (eqv? (lang*) 'arc) (list 'quote (ac-quoted (cadr s))) s)) |
122 | 245 | ((eq? (xcar s) 'lexenv) (ac-lenv (cdr s) env))
|
123 | 246 | [(and (eq? (xcar s) 'quasiquote)
|
124 | 247 | (not (ac-macro? 'quasiquote)))
|
|
165 | 288 |
|
166 | 289 | (define (ssyntax? x)
|
167 | 290 | (and (symbol? x)
|
| 291 | + (eqv? (lang*) 'arc) |
168 | 292 | (not (or (eqv? x '+) (eqv? x '++) (eqv? x '_)))
|
169 | 293 | (let ([name (symbol->string x)])
|
170 | 294 | (has-ssyntax-char? name (- (string-length name) 2)))))
|
|
644 | 768 | ; and it's bound to a function, generate (foo bar) instead of
|
645 | 769 | ; (ar-funcall1 foo bar)
|
646 | 770 |
|
| 771 | +(define (stx? expr) |
| 772 | + (and (symbol? expr) (eqv? (bound? expr) 'syntax))) |
| 773 | + |
647 | 774 | (define (ac-call fn args env)
|
| 775 | + (if (not (eqv? (lang*) 'arc)) |
| 776 | + `(,(ac fn env) ,@(map (lambda (x) (ac x env)) args)) |
648 | 777 | (let ([macfn (ac-macro? fn)])
|
649 | 778 | (cond [macfn
|
650 | 779 | (ac-mac-call macfn args env)]
|
| 780 | + [(stx? fn) |
| 781 | + (parameterize ((lang* 'rkt)) |
| 782 | + `(,(ac fn env) ,@(map (lambda (x) (ac x env)) args)))] |
| 783 | + ((and (id-literal? fn) |
| 784 | + (void? (id-literal fn))) |
| 785 | + (map (lambda (x) (ac x env)) args)) |
| 786 | + ((or (memf keyword? args) |
| 787 | + (id-literal? fn)) |
| 788 | + `(,(ac fn env) ,@(map (lambda (x) (ac x env)) args))) |
651 | 789 | [(and (pair? fn) (eqv? (car fn) 'fn))
|
652 | 790 | `(,(ac fn env) ,@(ac-args (cadr fn) args env))]
|
653 | 791 | [(and (ar-bflag 'direct-calls) (symbol? fn) (not (lex? fn env)) (bound? fn)
|
654 | 792 | (procedure? (arc-eval fn)))
|
655 | 793 | (ac-global-call fn args env)]
|
656 | 794 | [#t
|
657 | 795 | `((ar-coerce ,(ac fn env) 'fn)
|
658 |
| - ,@(map (lambda (x) (ac x env)) args))]))) |
| 796 | + ,@(map (lambda (x) (ac x env)) args))])))) |
659 | 797 |
|
660 | 798 | (define (ac-mac-call m args env)
|
661 | 799 | (let ([x1 (apply m args)])
|
|
666 | 804 |
|
667 | 805 | (define (ac-macro? fn)
|
668 | 806 | (if (symbol? fn)
|
669 |
| - (let ([v (and (bound? fn) (arc-eval fn))]) |
| 807 | + (let ([v (and (bound? fn) (not (eqv? (bound? fn) 'syntax)) (arc-eval fn))]) |
670 | 808 | (if (and v
|
671 | 809 | (ar-tagged? v)
|
672 | 810 | (eq? (ar-type v) 'mac))
|
|
801 | 939 | ; reduce?
|
802 | 940 |
|
803 | 941 | (define (pairwise pred lst)
|
804 |
| - (cond [(null? lst) ar-t] |
805 |
| - [(null? (cdr lst)) ar-t] |
| 942 | + (cond [(null? lst) #t] |
| 943 | + [(null? (cdr lst)) #t] |
806 | 944 | [(not (ar-nil? (pred (car lst) (cadr lst))))
|
807 | 945 | (pairwise pred (cdr lst))]
|
808 |
| - [#t ar-nil])) |
| 946 | + [#t #f])) |
809 | 947 |
|
810 | 948 | ; not quite right, because behavior of underlying eqv unspecified
|
811 | 949 | ; in many cases according to r5rs
|
|
933 | 1071 | ((async-channel? x) 'channel)
|
934 | 1072 | ((evt? x) 'event)
|
935 | 1073 | [(keyword? x) 'keyword]
|
936 |
| - [#t (err "Type: unknown type" x)])) |
| 1074 | + ; [#t (err "Type: unknown type" x)])) |
| 1075 | + [#t (vector-ref (struct->vector x) 0)])) |
937 | 1076 | (xdef type ar-type)
|
938 | 1077 |
|
939 | 1078 | (define (ar-rep x)
|
@@ -1603,10 +1742,9 @@ Arc 3.1 documentation: https://arclanguage.github.io/ref.
|
1603 | 1742 | val))
|
1604 | 1743 |
|
1605 | 1744 | (define (bound? arcname)
|
1606 |
| - (with-handlers ([exn:fail:syntax? (lambda (e) #t)] |
| 1745 | + (with-handlers ([exn:fail:syntax? (lambda (e) (if (eqv? arcname '_) #f 'syntax))] |
1607 | 1746 | [exn:fail:contract:variable? (lambda (e) #f)])
|
1608 |
| - (namespace-variable-value (ac-global-name arcname)) |
1609 |
| - #t)) |
| 1747 | + (namespace-variable-value (ac-global-name arcname)))) |
1610 | 1748 |
|
1611 | 1749 | (xdef bound (lambda (x) (tnil (bound? x))))
|
1612 | 1750 |
|
|
0 commit comments