mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Special encoding for syntax - parens, quotes
This commit is contained in:
parent
62a54a50d5
commit
94f15d699e
2 changed files with 33 additions and 31 deletions
54
read.scm
54
read.scm
|
@ -697,12 +697,16 @@
|
|||
"(void *data, int argc, closure _, object k, object port)"
|
||||
" Cyc_io_read_token(data, k, port);")
|
||||
|
||||
(define-c Cyc-opaque-eq?
|
||||
"(void *data, int argc, closure _, object k, object opq, object obj)"
|
||||
" return_closcall1(data, k, equalp( opaque_ptr(opq), obj ));")
|
||||
|
||||
(write
|
||||
(parse2 (open-input-file "test.scm")))
|
||||
;(read-token (open-input-file "test.scm")))
|
||||
|
||||
TODO: getting there, but still not parsed correctly:
|
||||
(eq? c #\))
|
||||
;TODO: getting there, but still not parsed correctly:
|
||||
;(eq? c #\))
|
||||
|
||||
;; Notes on writing a fast parser:
|
||||
; - Interface to the user is (read). This needs to be fast
|
||||
|
@ -735,6 +739,27 @@ TODO: getting there, but still not parsed correctly:
|
|||
(let ((token (read-token fp))) ;; TODO: this will be a C call
|
||||
;(write `(token ,token))
|
||||
(cond
|
||||
((Cyc-opaque? token)
|
||||
(cond
|
||||
;; Open paren, start read loop
|
||||
((Cyc-opaque-eq? token #\()
|
||||
(let loop ((lis '())
|
||||
(t (parse2 fp)))
|
||||
(cond
|
||||
((eof-object? t)
|
||||
(error "missing closing parenthesis"))
|
||||
((and (Cyc-opaque? t) (Cyc-opaque-eq? t #\)))
|
||||
(reverse lis))
|
||||
(else
|
||||
(loop (cons t lis) (parse2 fp))))))
|
||||
((Cyc-opaque-eq? token #\')
|
||||
(list 'quote (parse2 fp)))
|
||||
((Cyc-opaque-eq? token #\`)
|
||||
(list 'quasiquote (parse2 fp)))
|
||||
((Cyc-opaque-eq? token #\,)
|
||||
(list 'unquote (parse2 fp)))
|
||||
(else
|
||||
token))) ;; TODO: error if this is returned to original caller of parse2
|
||||
((vector? token)
|
||||
(cond
|
||||
((= (vector-length token) 2) ;; Special case: number
|
||||
|
@ -744,33 +769,14 @@ TODO: getting there, but still not parsed correctly:
|
|||
(exact (string->number (vector-ref token 0) (vector-ref token 1)))
|
||||
(inexact (string->number (vector-ref token 0) (vector-ref token 1)))))
|
||||
((= (vector-length token) 1) ;; Special case: error
|
||||
(cond
|
||||
;; Open paren, start read loop
|
||||
((eq? (vector-ref token 0) #\()
|
||||
(let loop ((lis '())
|
||||
(t (parse2 fp)))
|
||||
(cond
|
||||
((eof-object? t)
|
||||
(error "missing closing parenthesis"))
|
||||
((eq? t #\)) TODO: here and below, need to wait for the vectorized close?
|
||||
(reverse lis))
|
||||
(else
|
||||
(loop (cons t lis) (parse2 fp))))))
|
||||
((eq? (vector-ref token 0) #\')
|
||||
(list 'quote (parse2 fp)))
|
||||
((eq? (vector-ref token 0) #\`)
|
||||
(list 'quasiquote (parse2 fp)))
|
||||
((eq? (vector-ref token 0) #\,)
|
||||
(list 'unquote (parse2 fp)))
|
||||
(else
|
||||
(error (vector-ref token 0)))))
|
||||
(error (vector-ref token 0)))
|
||||
(else
|
||||
(let loop ((lis '())
|
||||
(t (parse2 fp)))
|
||||
(cond
|
||||
((eof-object? t)
|
||||
(error "missing closing parenthesis"))
|
||||
((eq? t #\))
|
||||
((and (Cyc-opaque? t) (Cyc-opaque-eq? t #\)))
|
||||
(list->vector (reverse lis)))
|
||||
(else
|
||||
(loop (cons t lis) (parse2 fp))))))))
|
||||
|
@ -780,7 +786,7 @@ TODO: getting there, but still not parsed correctly:
|
|||
(cond
|
||||
((eof-object? t)
|
||||
(error "missing closing parenthesis"))
|
||||
((eq? t #\))
|
||||
((and (Cyc-opaque? t) (Cyc-opaque-eq? t #\)))
|
||||
(apply bytevector (reverse lis)))
|
||||
(else
|
||||
(loop (cons t lis) (parse2 fp))))))
|
||||
|
|
10
runtime.c
10
runtime.c
|
@ -6106,13 +6106,9 @@ void Cyc_io_read_token(void *data, object cont, object port)
|
|||
_read_whitespace(p);
|
||||
} else if (c == '(' || c == ')' || c == '\'' || c == '`') {
|
||||
if (p->tok_end) _read_return_atom(data, cont, p);
|
||||
//return_thread_runnable(data, obj_char2obj(c));
|
||||
// Encode within a vector so we can distinguish between these and chars such as #\(
|
||||
make_empty_vector(vec);
|
||||
vec.num_elements = 1;
|
||||
vec.elements = (object *) alloca(sizeof(object) * vec.num_elements);
|
||||
vec.elements[0] = obj_char2obj(c);
|
||||
return_thread_runnable(data, &vec);
|
||||
// Special encoding so we can distinguish from chars such as #\(
|
||||
make_c_opaque(opq, obj_char2obj(c));
|
||||
return_thread_runnable(data, &opq);
|
||||
} else if (c == ',') {
|
||||
if (p->tok_end) _read_return_atom(data, cont, p);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue