Commit c0d586fa authored by Juergen Nickelsen's avatar Juergen Nickelsen

read from string (and a read builtin at all)

parent 52e15687
......@@ -11,7 +11,7 @@ all:
@echo
test:
./pyle -e tests.l
./pyle -e tests.l < data.l
tags: *.py
etags *.py *.l
......
Things to do in Pyle
====================
* read from string
* destructuring bind like lingo
* eliminate the nargs parameter from builtins
+ read from string (and a read builtin at all)
+ unify parameter binding for functions and let
+ finally check if let isn't secretly (and wrongly) a let*; alas,
it was, but that is fixed and now we have a proper let *and*
......
......@@ -27,6 +27,7 @@ function.
# Python modules
import re
import sys
import copy
import inspect
......@@ -37,6 +38,7 @@ from object import *
from stdsyms import *
from macro import *
import dep
import reader
# convert our list to python list
def ll2pl(l):
......@@ -487,6 +489,22 @@ def Pyle_quote(arglist, nargs):
"""
return arglist.car()
def Pyle_read(arglist, nargs):
"""@desc args: [STRING]; result: object
Read an s-expression from the specified string (or stdin) and return it.
"""
s = arglist.car()
if s:
port = reader.StringPort(stringArg(s, "read: "))
fname = "*string*"
else:
port = sys.stdin
fname = "*stdin*"
rdr = reader.Reader(port, fname)
obj = rdr.read()
rdr.close()
return obj
def Pyle_rplacd(arglist, nargs):
"""@desc args: PAIR NEW-CDR; result: pair
Replace the cdr of PAIR with NEW-CDR and return PAIR.
......
;;; test input data
;; read0
a
;; read1
12
;; read2
"333333r"
;; read3
'(3 4 5)
;; read4
(let ((a 5) (b (* 2 3)) (c 'nargl) d (e "foo mich auch"))
(let ((e b) (g e))
(list e g)))
......@@ -170,9 +170,9 @@ class StringPort():
left = self.length - self.position
if n > left:
n = left
newpos = position + n
newpos = self.position + n
substr = self.string[self.position:newpos]
position = newpos
self.position = newpos
return substr
class Reader():
......
......@@ -191,6 +191,25 @@
(list foo bar bletch))
(nil nil quux))
(test read0 (read) a)
(test read1 (read) 12)
(test read2 (read) "333333r")
(test read3 (read) '(3 4 5))
(test read4 (read)
(let ((a 5) (b (* 2 3)) (c 'nargl) d (e "foo mich auch"))
(let ((e b) (g e))
(list e g))))
(test read5 (read "a") a)
(test read6 (read "12") 12)
(test read7 (read "\"333333r\"") "333333r")
(test read8 (read "'(3 4 5)") '(3 4 5))
(test read9 (read "(let ((a 5) (b (* 2 3)) (c 'nargl) d (e \"foo mich auch\"))
(let ((e b) (g e))
(list e g)))")
(let ((a 5) (b (* 2 3)) (c 'nargl) d (e "foo mich auch"))
(let ((e b) (g e))
(list e g))))
;; End Of Tests ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(format t "\ndone testing\n")
(if fails
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment