Skip to content

Commit d143e7e

Browse files
committed
Interop seamlessly with racket
1 parent 2f42517 commit d143e7e

File tree

5 files changed

+188
-21
lines changed

5 files changed

+188
-21
lines changed

ac.rkt

Lines changed: 155 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#lang racket/base
1+
#lang racket/load
22

33
; Arc Compiler.
44

5-
65
(require
7-
86
; This defines names like _list, so it would conflict with the
97
; naming convention for Arc global variables if we didn't prefix it.
108
(prefix-in ffi: ffi/unsafe)
@@ -28,10 +26,12 @@
2826

2927
(only-in "brackets.rkt" bracket-readtable)
3028

31-
(for-syntax racket/base))
29+
(for-syntax racket/base)
30+
)
3231

33-
(provide (all-defined-out))
32+
; (provide (all-defined-out))
3433

34+
(read-accept-bar-quote #f)
3535

3636
(define-runtime-path ac-rkt-path "ac.rkt")
3737
(define-runtime-path arc-arc-path "arc.arc")
@@ -41,8 +41,112 @@
4141
(define main-namespace
4242
(namespace-anchor->namespace main-namespace-anchor))
4343

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+
44143
(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))
46150

47151
(define init-steps-reversed* (list))
48152

@@ -111,14 +215,33 @@
111215
; env is a list of lexically bound variables, which we
112216
; need in order to decide whether set should create a global.
113217

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+
114229
(defarc (ac s env)
115230
(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))
117234
[(ssyntax? s) (ac (expand-ssyntax s) env)]
118235
[(symbol? s) (ac-var-ref s env)]
119236
[(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)))
120243
[(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))
122245
((eq? (xcar s) 'lexenv) (ac-lenv (cdr s) env))
123246
[(and (eq? (xcar s) 'quasiquote)
124247
(not (ac-macro? 'quasiquote)))
@@ -165,6 +288,7 @@
165288

166289
(define (ssyntax? x)
167290
(and (symbol? x)
291+
(eqv? (lang*) 'arc)
168292
(not (or (eqv? x '+) (eqv? x '++) (eqv? x '_)))
169293
(let ([name (symbol->string x)])
170294
(has-ssyntax-char? name (- (string-length name) 2)))))
@@ -644,18 +768,32 @@
644768
; and it's bound to a function, generate (foo bar) instead of
645769
; (ar-funcall1 foo bar)
646770

771+
(define (stx? expr)
772+
(and (symbol? expr) (eqv? (bound? expr) 'syntax)))
773+
647774
(define (ac-call fn args env)
775+
(if (not (eqv? (lang*) 'arc))
776+
`(,(ac fn env) ,@(map (lambda (x) (ac x env)) args))
648777
(let ([macfn (ac-macro? fn)])
649778
(cond [macfn
650779
(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)))
651789
[(and (pair? fn) (eqv? (car fn) 'fn))
652790
`(,(ac fn env) ,@(ac-args (cadr fn) args env))]
653791
[(and (ar-bflag 'direct-calls) (symbol? fn) (not (lex? fn env)) (bound? fn)
654792
(procedure? (arc-eval fn)))
655793
(ac-global-call fn args env)]
656794
[#t
657795
`((ar-coerce ,(ac fn env) 'fn)
658-
,@(map (lambda (x) (ac x env)) args))])))
796+
,@(map (lambda (x) (ac x env)) args))]))))
659797

660798
(define (ac-mac-call m args env)
661799
(let ([x1 (apply m args)])
@@ -666,7 +804,7 @@
666804

667805
(define (ac-macro? fn)
668806
(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))])
670808
(if (and v
671809
(ar-tagged? v)
672810
(eq? (ar-type v) 'mac))
@@ -801,11 +939,11 @@
801939
; reduce?
802940

803941
(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]
806944
[(not (ar-nil? (pred (car lst) (cadr lst))))
807945
(pairwise pred (cdr lst))]
808-
[#t ar-nil]))
946+
[#t #f]))
809947

810948
; not quite right, because behavior of underlying eqv unspecified
811949
; in many cases according to r5rs
@@ -933,7 +1071,8 @@
9331071
((async-channel? x) 'channel)
9341072
((evt? x) 'event)
9351073
[(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)]))
9371076
(xdef type ar-type)
9381077

9391078
(define (ar-rep x)
@@ -1603,10 +1742,9 @@ Arc 3.1 documentation: https://arclanguage.github.io/ref.
16031742
val))
16041743

16051744
(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))]
16071746
[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))))
16101748

16111749
(xdef bound (lambda (x) (tnil (bound? x))))
16121750

arc.arc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,12 +1031,15 @@ Incompatibility alert: 'for' is different in Anarki from Arc 3.1. For Arc
10311031
`(up ,var 0 (- (len ,s) 1)
10321032
,@body))
10331033

1034+
(|require| racket/generator)
1035+
10341036
(def walk (seq f)
10351037
"Calls function 'f' on each element of 'seq'. See also [[map]]."
10361038
(loop (l seq)
1037-
(when acons.l
1038-
(f car.l)
1039-
(recur cdr.l))))
1039+
(if acons.l
1040+
(do (f car.l) (recur cdr.l))
1041+
(generator? l) (let x (l) (unless (void? x) (f x) (recur l)))
1042+
(sequence? l) (walk (sequence->generator l) f))))
10401043

10411044
(mac accum (accfn . body)
10421045
"Runs 'body' (usually containing a loop) and then returns in order all the

arc.scm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#lang racket/load
2+
3+
(compile-allow-set!-undefined #t)
4+
(compile-enforce-module-constants #f)
5+
(require racket/base)
6+
7+
(load "ac.rkt")
8+
(require 'ac)
9+
10+
(require "brackets.rkt")
11+
;(use-bracket-readtable)
12+
13+
(anarki-init-in-main-namespace-verbose)
14+
; (aload "arc.arc")
15+
(aload "libs.arc")
16+
17+

arc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ if [[ $REPL == definitely || ( $REPL == maybe && $# -eq 0 ) ]]; then
117117
repl="(tl-with-main-settings)"
118118
fi
119119

120-
$rl racket -t "$arc_dir/boot.rkt" -e "(anarki-init-in-main-namespace-verbose) $load $repl" "$@"
120+
$rl racket -t "$arc_dir/as.scm" #-e "(anarki-init-in-main-namespace-verbose) $load $repl" "$@"

as.scm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#lang racket/load
2+
; racket -f as.scm
3+
; (asv)
4+
; http://localhost:8080
5+
6+
(require "arc.scm")
7+
(tl-with-main-settings)
8+
9+

0 commit comments

Comments
 (0)