Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,7 +1,7 @@
|
|||
grammar S-Exp {
|
||||
rule TOP {^ <s-list> $};
|
||||
|
||||
token s-list { '(' ~ ')' [ <in_list> ** [\s+] | '' ] }
|
||||
token s-list { '(' ~ ')' [ <in_list>+ % [\s+] | '' ] }
|
||||
token in_list { <s-token> | <s-list> }
|
||||
|
||||
proto token s-token {*}
|
||||
|
|
|
|||
|
|
@ -62,14 +62,12 @@ class SExpr
|
|||
end
|
||||
|
||||
def symbol_or_number(word)
|
||||
Integer(word)
|
||||
rescue ArgumentError
|
||||
begin
|
||||
Integer(word)
|
||||
Float(word)
|
||||
rescue ArgumentError
|
||||
begin
|
||||
Float(word)
|
||||
rescue ArgumentError
|
||||
word.to_sym
|
||||
end
|
||||
word.to_sym
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -120,5 +118,5 @@ sexpr = SExpr.new <<END
|
|||
END
|
||||
|
||||
puts "original sexpr:\n#{sexpr.original}"
|
||||
puts "\nruby data structure:\n#{sexpr.data.inspect}"
|
||||
puts "\nruby data structure:\n#{sexpr.data}"
|
||||
puts "\nand back to S-Expr:\n#{sexpr.to_sexpr}"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,44 @@
|
|||
(define input "((data \"quoted data\" 123 4.5)
|
||||
(data (!@# (4.5) \"(more\" \"data)\")))")
|
||||
(define data (read (open-input-string input)))
|
||||
(define output (let ((out (open-output-string)))
|
||||
(write data out)
|
||||
(get-output-string out)))
|
||||
(write input) (newline)
|
||||
(write data) (newline)
|
||||
(write output) (newline)
|
||||
(define (sexpr-read port)
|
||||
(define (help port)
|
||||
(let ((char (read-char port)))
|
||||
(cond
|
||||
((or (eof-object? char) (eq? char #\) )) '())
|
||||
((eq? char #\( ) (cons (help port) (help port)))
|
||||
((char-whitespace? char) (help port))
|
||||
((eq? char #\"") (cons (quote-read port) (help port)))
|
||||
(#t (unread-char char port) (cons (string-read port) (help port))))))
|
||||
; This is needed because the function conses all parsed sexprs onto something,
|
||||
; so the top expression is one level too deep.
|
||||
(car (help port)))
|
||||
|
||||
(define (quote-read port)
|
||||
(define (help port)
|
||||
(let ((char (read-char port)))
|
||||
(if
|
||||
(or (eof-object? char) (eq? char #\""))
|
||||
'()
|
||||
(cons char (help port)))))
|
||||
(list->string (help port)))
|
||||
|
||||
(define (string-read port)
|
||||
(define (help port)
|
||||
(let ((char (read-char port)))
|
||||
(cond
|
||||
((or (eof-object? char) (char-whitespace? char)) '())
|
||||
((eq? char #\) ) (unread-char char port) '())
|
||||
(#t (cons char (help port))))))
|
||||
(list->string (help port)))
|
||||
|
||||
(define (format-sexpr expr)
|
||||
(define (help expr pad)
|
||||
(if
|
||||
(list? expr)
|
||||
(begin
|
||||
(format #t "~a(~%" (make-string pad #\tab))
|
||||
(for-each (lambda (x) (help x (1+ pad))) expr)
|
||||
(format #t "~a)~%" (make-string pad #\tab)))
|
||||
(format #t "~a~a~%" (make-string pad #\tab) expr)))
|
||||
(help expr 0))
|
||||
|
||||
(format-sexpr (sexpr-read
|
||||
(open-input-string "((data \"quoted data\" 123 4.5) (data (!@# (4.5) \"(more\" \"data)\")))")))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue