[pypy-commit] lang-scheme default: Implementation of Vectors #(...)
boemmels
noreply at buildbot.pypy.org
Mon Nov 28 00:38:28 CET 2011
Author: Juergen Boemmels <boemmels at web.de>
Branch:
Changeset: r17:96e5eb8b8fab
Date: 2011-11-27 17:00 +0100
http://bitbucket.org/pypy/lang-scheme/changeset/96e5eb8b8fab/
Log: Implementation of Vectors #(...)
diff --git a/scheme/object.py b/scheme/object.py
--- a/scheme/object.py
+++ b/scheme/object.py
@@ -343,6 +343,39 @@
self.car.equal(w_obj.car) and \
self.cdr.equal(w_obj.cdr)
+class W_Vector(W_Root):
+ def __init__(self, vect):
+ self.vect = vect
+
+ def to_repr(self):
+ #strs = map(lambda item: item.to_repr(), self.vect)
+ strs = []
+ for w_item in self.vect:
+ strs.append(w_item.to_repr())
+ return "#(" + " ".join(strs) + ")"
+
+ def to_string(self):
+ #strs = map(lambda item: item.to_string(), self.vect)
+ strs = []
+ for w_item in self.vect:
+ strs.append(w_item.to_string())
+ return "#(" + " ".join(strs) + ")"
+
+ def __repr__(self):
+ return "<W_Vector [" + " ".join(self.vect) + "]>"
+
+ def eval_tr(self, ctx):
+ # vectors can not be evaluated
+ raise SchemeSyntaxError
+
+ def get_len(self):
+ return len(self.vect)
+
+ def get_item(self, offset):
+ if offset < 0 or offset >= len(self.vect):
+ raise SchemeException
+ return self.vect[offset]
+
class W_Callable(W_Eval):
def call_tr(self, ctx, lst):
#usually tail-recursive call is normal call
diff --git a/scheme/ssparser.py b/scheme/ssparser.py
--- a/scheme/ssparser.py
+++ b/scheme/ssparser.py
@@ -2,7 +2,7 @@
from pypy.rlib.parsing.makepackrat import BacktrackException, Status
from scheme.object import W_Pair, W_Integer, W_String, symbol, \
w_nil, W_Boolean, W_Real, quote, qq, unquote, unquote_splicing, \
- w_ellipsis, W_Character, SchemeSyntaxError
+ w_ellipsis, W_Character, SchemeSyntaxError, W_Vector
def str_unquote(s):
str_lst = []
@@ -96,6 +96,7 @@
sexpr:
list
+ | vector
| quote
| qq
| unquote_splicing
@@ -116,6 +117,14 @@
IGNORE*
return {p};
+ vector:
+ '#('
+ IGNORE*
+ v = sexpr*
+ ')'
+ IGNORE*
+ return {W_Vector(v)};
+
pair:
car = sexpr
'.'
diff --git a/scheme/test/test_object.py b/scheme/test/test_object.py
--- a/scheme/test/test_object.py
+++ b/scheme/test/test_object.py
@@ -66,6 +66,17 @@
assert w_sym.to_string() == "symb"
assert w_sym.to_boolean() is True
+def test_vector():
+ w_vect = W_Vector([W_Integer(1), W_Symbol("symb"), W_Integer(2)])
+ assert w_vect.to_boolean() is True
+ assert w_vect.to_repr() == "#(1 symb 2)"
+ assert w_vect.get_len() == 3
+ w_item = w_vect.get_item(2)
+ assert isinstance(w_item, W_Integer)
+ assert w_item.to_fixnum() == 2
+ with py.test.raises(SchemeException):
+ w_vect.get_item(3)
+
def test_ctx():
w_fnum = W_Integer(12)
w_symb = W_Symbol("symb")
diff --git a/scheme/test/test_parser.py b/scheme/test/test_parser.py
--- a/scheme/test/test_parser.py
+++ b/scheme/test/test_parser.py
@@ -1,7 +1,7 @@
import py
from scheme.ssparser import parse
from scheme.object import W_Boolean, W_Real, W_Integer, W_String
-from scheme.object import W_Pair, W_Nil, W_Symbol, W_Character
+from scheme.object import W_Pair, W_Nil, W_Symbol, W_Character, W_Vector
from pypy.rlib.parsing.makepackrat import BacktrackException
def parse_sexpr(expr):
@@ -201,3 +201,21 @@
assert isinstance(w_float, W_Symbol)
assert unwrap(w_float) == "..."
+def test_vector():
+ w_vect = parse_sexpr('#()')
+ assert isinstance(w_vect, W_Vector)
+ assert w_vect.get_len() == 0
+ assert w_vect.to_repr() == '#()'
+
+ w_vect = parse_sexpr('#(1 2 symb "string")')
+ assert isinstance(w_vect, W_Vector)
+ assert w_vect.get_len() == 4
+ assert w_vect.to_repr() == '#(1 2 symb "string")'
+
+ w_item0 = w_vect.get_item(0)
+ assert isinstance(w_item0, W_Integer)
+ assert w_item0.to_fixnum() == 1
+
+ w_item2 = w_vect.get_item(2)
+ assert isinstance(w_item2, W_Symbol)
+ assert w_item2.to_string() == "symb"
More information about the pypy-commit
mailing list