Skip to content

Commit

Permalink
able to load a (slightly modified) version of arc.arc
Browse files Browse the repository at this point in the history
  • Loading branch information
stefano committed Jun 26, 2009
1 parent ed7d82c commit f79d4de
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
6 changes: 3 additions & 3 deletions arc/qq.arc
Expand Up @@ -21,10 +21,10 @@
(eval-qq (cadr x) (- level 1))
(is (car x) 'unquote)
(list 'unquote (eval-qq (cadr x) (- level 1)))
(and (is level 1) (is (car x) 'splice))
(and (is level 1) (is (car x) 'unquote-splicing))
(list '__to-splice (eval-qq (cadr x) (- level 1)))
(is (car x) 'splice)
(list 'splice (eval-qq (cadr x) (- level 1)))
(is (car x) 'unquote-splicing)
(list 'unquote-splicing (eval-qq (cadr x) (- level 1)))
(is (car x) 'quasiquote)
(list 'quasiquote (eval-qq (cadr x) (+ level 1)))
(cons 'list (map1 [eval-qq _ level] x)))))
Expand Down
21 changes: 12 additions & 9 deletions compiler/boot.arc
Expand Up @@ -27,7 +27,10 @@
(def cddr (x) (cdr (cdr x)))

(def map1 (f l)
(if l (cons (f (car l)) (map1 f (cdr l)))))
((afn (l acc)
(if l
(self (cdr l) (cons (f (car l)) acc))
(rev acc))) l nil))

(def mem (x l)
(if (no l) l
Expand Down Expand Up @@ -97,21 +100,21 @@
x nil))

; not the official some
(def some (test l)
(def at-least-one (test l)
(if l
(or (test (car l)) (some test (cdr l)))
(or (test (car l)) (at-least-one test (cdr l)))
nil))

; from arc.arc (minus string stuff)
(def map (f . seqs)
(if (no (cdr seqs))
(map1 f (car seqs))
((afn (seqs)
(if (some no seqs)
nil
(cons (apply f (map1 car seqs))
(self (map1 cdr seqs)))))
seqs)))
((afn (seqs acc)
(if (at-least-one no seqs)
(rev acc)
(self (map1 cdr seqs)
(cons (apply f (map1 car seqs)) acc))))
seqs nil)))

; from arc.arc
(def listtab (al)
Expand Down
37 changes: 27 additions & 10 deletions compiler/comp.arc
Expand Up @@ -76,6 +76,7 @@
(compile-fn (empty-state) f))))

(let escape-table (listtab '((#\newline "\\n")
(#\return "\\r")
(#\\ "\\\\")
(#\tab "\\t")
(#\" "\\\"")))
Expand Down Expand Up @@ -163,10 +164,13 @@
(emit-const const!expr)
(prn "set_hll_global '" const!name "', " (c-pop cs))))

(def emit-fn-head (cs name outer)
(if (iso outer "")
(prn ".sub " name)
(prn ".sub " name " :outer('" outer "')")))
(def emit-fn-head (cs name dbg-name outer)
(pr ".sub '" (or dbg-name name) "' :nsentry('" name "')")
(if dbg-name
(pr " :subid('" name "')"))
(if (no (iso outer ""))
(pr " :outer('" outer "')"))
(prn))

; emit a sequence of operations
; if sequence is empty, emit code to return nil
Expand All @@ -193,7 +197,7 @@
body (cdddr e))
(unless (isa name 'sym)
(err:string "not a symbol: " name))
(emit-fn-head cs name f!outer)
(emit-fn-head cs name f!dbg-name f!outer)
(emit-args args)
; emit the body
(reset-reg cs)
Expand Down Expand Up @@ -260,11 +264,15 @@
(def a-fn (e)
(and (acons e) (is (car e) 'fn)))

(def a-fn-assign (e)
(and (acons e) (is (car e) 'assign) (a-fn (caddr e))))

(def mk-const (name expr)
(listtab (list (list 'name name) (list 'expr expr))))

(def mk-fn (expr outer lex)
(listtab (list (list 'expr expr) (list 'outer outer) (list 'lex lex))))
(def mk-fn (expr outer lex dbg-name)
(listtab (list (list 'expr expr) (list 'outer outer)
(list 'lex lex) (list 'dbg-name dbg-name))))

(def arg-names (args)
; consider destructuring too
Expand All @@ -273,7 +281,8 @@
(list args)
(flat (map [if (is-opt _) (cadr _) _] (makeproper args)))))

(def collect-fns-and-consts (expr lex outer is-seq consts)
(def collect-fns-and-consts (expr lex outer is-seq consts (o have-name nil))
; (ero (string "collect: " expr))
(if
(in (e-type expr) 'sym 't 'nil)
(list nil nil expr)
Expand All @@ -282,19 +291,27 @@
(list nil nil (consts expr))
(let name (uniq)
(= (consts expr) name)
;(ero (string " name: " name " -> " expr))
(list nil (list (mk-const name expr)) name)))
(a-fn-assign expr)
(let (f c e) (collect-fns-and-consts (caddr expr) lex outer
is-seq consts (cadr expr))
(list f c (list 'assign (cadr expr) e)))
(a-fn expr)
(withs (name (uniq)
args expr.1
body (cddr expr)
new-lex (join (arg-names args) lex))
(let (fns consts expr) (collect-fns-and-consts body new-lex name t consts)
(let (fns consts expr) (collect-fns-and-consts body new-lex
name t consts)
(list (cons (mk-fn (cons '$fn (cons name (cons args expr)))
outer new-lex)
outer new-lex have-name)
fns)
consts (list (if (iso outer "") '$function '$closure) name))))
(let res (map [collect-fns-and-consts _ lex outer nil consts] expr)
;(ero "res: " res)
(let res (apply map list res)
;(ero " res2: " res)
(list (apply join res.0) (apply join res.1) res.2)))))

(def mac-ex (e)
Expand Down
13 changes: 10 additions & 3 deletions read.pir
Expand Up @@ -52,13 +52,15 @@
$P0["t"] = "\t"
$P0["\\"] = "\\"
$P0["\""] = "\""
$P0["r"] = "\r"
set_hll_global 'escape-table*', $P0

## Global character names table
$P0 = new 'ArcHash'
$P0["newline"] = "\n"
$P0["space"] = " "
$P0["tab"] = "\t"
$P0["return"] = "\r"
set_hll_global 'char-table*', $P0

## Global ssyntax table
Expand Down Expand Up @@ -181,7 +183,7 @@ keep_going:
.tailcall $P0(rs)
default:
## if the character isn't present in the read table, read a symbol
.tailcall _read_symbol(rs)
.tailcall _read_symbol(rs, 1)
eof_found:
$P0 = new 'Eof'
.return ($P0)
Expand Down Expand Up @@ -214,6 +216,8 @@ end:
.sub '_read_symbol'
.param pmc rs
.param int do_ssexpand
.local string result
result = ""
Expand All @@ -235,6 +239,9 @@ end:
if result == "t" goto ret_t
if result == "nil" goto ret_nil
$P0 = 'intern'(result)
if do_ssexpand goto expand
.return ($P0)
expand:
.tailcall 'ssexpand'($P0)
ret_t:
$P0 = get_hll_global 't'
Expand Down Expand Up @@ -319,7 +326,7 @@ mk_symbol:
rs.'get1'() # skip #
$S0 = rs.'get1'()
unless $S0 == "\\" goto error
$P0 = _read_symbol(rs)
$P0 = _read_symbol(rs, 0)
$S0 = $P0.'to_string'() # get char name
$S1 = ct[$S0]
unless $S1 == "" goto ret_it
Expand Down Expand Up @@ -460,7 +467,7 @@ error:
rs.'get1'() # skip ,
$S0 = rs.'peek1'()
unless $S0 == '@' goto go_on
type = "splice"
type = "unquote-splicing"
rs.'get1'() # throw away '@'
go_on:
.tailcall _read_next_with_head(rs, type)
Expand Down
4 changes: 4 additions & 0 deletions types.pir
Expand Up @@ -67,6 +67,10 @@
.return ("hash")
.end

.sub 'get_bool' :vtable :method
.return (1)
.end

.sub 'pr_repr' :method
.return ("#hash()")
.end
Expand Down

0 comments on commit f79d4de

Please sign in to comment.