[pypy-svn] r63379 - in pypy/branch/io-lang/pypy/lang/io: . test
david at codespeak.net
david at codespeak.net
Thu Mar 26 18:16:27 CET 2009
Author: david
Date: Thu Mar 26 18:16:25 2009
New Revision: 63379
Added:
pypy/branch/io-lang/pypy/lang/io/model.py
pypy/branch/io-lang/pypy/lang/io/number.py
pypy/branch/io-lang/pypy/lang/io/objspace.py
pypy/branch/io-lang/pypy/lang/io/register.py
pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py
pypy/branch/io-lang/pypy/lang/io/test/test_model.py
Modified:
pypy/branch/io-lang/pypy/lang/io/parserhack.io
pypy/branch/io-lang/pypy/lang/io/parserhack.py
pypy/branch/io-lang/pypy/lang/io/test/test_parse.py
Log:
(cfbolz, david) tried to interpret really simple code involving integers
Added: pypy/branch/io-lang/pypy/lang/io/model.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/model.py Thu Mar 26 18:16:25 2009
@@ -0,0 +1,79 @@
+class W_Object(object):
+ """Base class for all io objects"""
+ def __init__(self, space, protos = []):
+ self.slots = {}
+ self.protos = list(protos)
+ self.space = space
+
+ def lookup(self, name):
+ try:
+ return self.slots[name]
+ except KeyError:
+ pass
+ for x in self.protos:
+ t = x.lookup(name)
+ if t is not None:
+ return t
+
+class W_Number(W_Object):
+ """Number"""
+ def __init__(self, space, value):
+ self.value = value
+ W_Object.__init__(self, space, [space.w_number])
+
+ def __eq__(self, other):
+ return (self.__class__ is other.__class__ and
+ self.__dict__ == other.__dict__)
+
+ def __ne__(self, other):
+ return not self == other
+
+class W_List(W_Object):
+ pass
+class W_Sequence(W_Object):
+ pass
+
+class W_CFunction(W_Object):
+ def __init__(self, space, function):
+ self.function = function
+ W_Object.__init__(self, space)
+
+ def apply(self, w_receiver, w_message):
+ return self.function(w_receiver, w_message, None)
+
+class W_Message(W_Object):
+ def __init__(self, space, name, arguments, next = None):
+ self.name = name
+ self.literal_value = parse_literal(space, name)
+ self.arguments = arguments
+ self.next = next
+ W_Object.__init__(self, space)
+
+ def __repr__(self):
+ return "Message(%r, %r, %r)" % (self.name, self.arguments, self.next)
+
+ def __eq__(self, other):
+ return (self.__class__ is other.__class__ and
+ self.__dict__ == other.__dict__)
+
+ def __ne__(self, other):
+ return not self == other
+ def eval(self, w_receiver):
+ if self.literal_value is not None:
+ w_result = self.literal_value
+ else:
+ w_method = w_receiver.lookup(self.name)
+ w_result = w_method.apply(w_receiver, self)
+ if self.next:
+ #TODO: optimize
+ return self.next.eval(w_result)
+ else:
+ return w_result
+
+def parse_literal(space, literal):
+ for t in [int, float, lambda x: int(x, 16)]:
+ try:
+ return W_Number(space, t(literal))
+ except ValueError:
+ pass
+
Added: pypy/branch/io-lang/pypy/lang/io/number.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/number.py Thu Mar 26 18:16:25 2009
@@ -0,0 +1,11 @@
+from pypy.lang.io.register import register_method
+from pypy.lang.io.model import W_Number
+ at register_method("Number", '+')
+def w_number_add(w_target, w_message, w_context):
+ w_arg = w_message.arguments[0].eval(w_context)
+ return W_Number(w_target.space, w_target.value + w_arg.value)
+
+ at register_method("Number", '-')
+def w_number_minus(w_target, w_message, w_context):
+ w_arg = w_message.arguments[0].eval(w_context)
+ return W_Number(w_target.space, w_target.value - w_arg.value)
\ No newline at end of file
Added: pypy/branch/io-lang/pypy/lang/io/objspace.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/objspace.py Thu Mar 26 18:16:25 2009
@@ -0,0 +1,18 @@
+from pypy.rlib.objectmodel import instantiate
+from pypy.lang.io.model import W_Number, W_Object, W_CFunction
+import pypy.lang.io.number
+from pypy.lang.io.register import cfunction_definitions
+
+class ObjSpace(object):
+ """docstring for ObjSpace"""
+ def __init__(self):
+ self.w_obj = W_Object(self)
+ self.w_lobby = W_Object(self)
+ self.init_w_number()
+
+ def init_w_number(self):
+ self.w_number = instantiate(W_Number)
+ W_Object.__init__(self.w_number, self)
+ self.w_number.value = 0
+ for key, function in cfunction_definitions['Number'].items():
+ self.w_number.slots[key] = W_CFunction(self, function)
\ No newline at end of file
Modified: pypy/branch/io-lang/pypy/lang/io/parserhack.io
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/parserhack.io (original)
+++ pypy/branch/io-lang/pypy/lang/io/parserhack.io Thu Mar 26 18:16:25 2009
@@ -1,22 +1,20 @@
-nil addAst := nil
-nil addTail := nil
-Message addAst := method(
- "Ast(" print
- next addAst
-)
+nil addArguments := nil
+nil pythonize := nil
+
Message pythonize := method(
- addAst
- "implicit" print
- addTail
+ "W_Message(space," print
+ addArguments
+ next pythonize
+ ")" print
+
)
-Message addTail := method(
- ", \"" print
+Message addArguments := method(
+ "\"" print
name print
"\"" print
", [" print
arguments foreach(i, argument, argument pythonize; ", " print)
- "])" print
- next addTail
+ "]," print
)
in := File standardInput
Modified: pypy/branch/io-lang/pypy/lang/io/parserhack.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/parserhack.py (original)
+++ pypy/branch/io-lang/pypy/lang/io/parserhack.py Thu Mar 26 18:16:25 2009
@@ -1,32 +1,20 @@
import py
import os
-
-implicit = "implicit"
+from pypy.lang.io.model import W_Number, parse_literal, W_Message
+from pypy.lang.io.objspace import ObjSpace
io_file = py.magic.autopath().dirpath().join("parserhack.io")
-class Ast(object):
- def __init__(self, receiver, name, arguments = None):
- self.receiver = receiver
- self.name = name
- if arguments is None:
- arguments = []
- self.arguments = arguments
-
- def __repr__(self):
- return "Ast(%r, %r, %r)" % (self.receiver, self.name, self.arguments)
-
- def __eq__(self, other):
- return (self.__class__ is other.__class__ and
- self.__dict__ == other.__dict__)
-
- def __ne__(self, other):
- return not self == other
-
-def parse(input):
+def parse(input, space=None):
child_in, child_out_err = os.popen4("osxvm %s" % io_file)
child_in.write(input)
child_in.close()
s = child_out_err.read().strip()
+ print s
return eval(s)
+
+def interpret(code):
+ space = ObjSpace()
+ ast = parse(code, space)
+ return ast.eval(None)
\ No newline at end of file
Added: pypy/branch/io-lang/pypy/lang/io/register.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/register.py Thu Mar 26 18:16:25 2009
@@ -0,0 +1,7 @@
+cfunction_definitions = {}
+def register_method(type_name, slot_name):
+ def register(function):
+ subdict = cfunction_definitions.setdefault(type_name, {})
+ subdict[slot_name] = function
+ return function
+ return register
\ No newline at end of file
Added: pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_interpreter.py Thu Mar 26 18:16:25 2009
@@ -0,0 +1,13 @@
+from pypy.lang.io.parserhack import interpret
+
+def test_even_simpler():
+ x = interpret("2")
+ assert x.value == 2
+
+def test_simple():
+ x = interpret("2 + 2")
+ assert x.value == 4
+
+def test_simple_minus():
+ x = interpret("2 - 2")
+ assert x.value == 0
\ No newline at end of file
Added: pypy/branch/io-lang/pypy/lang/io/test/test_model.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_model.py Thu Mar 26 18:16:25 2009
@@ -0,0 +1,26 @@
+from pypy.lang.io.model import parse_literal, W_Number, W_Object
+from pypy.lang.io.objspace import ObjSpace
+
+def test_parse_literal():
+ space = ObjSpace()
+ assert parse_literal(space, "2").value == 2
+ assert parse_literal(space, "0xFF").value == 255
+ assert parse_literal(space, "2.3").value == 2.3
+
+def test_lookup():
+ obj = W_Object(None, )
+ assert obj.lookup("fail") is None
+ a = obj.slots['fail'] = W_Object(None)
+ assert obj.lookup("fail") is a
+
+def test_lookup2():
+ obj1 = W_Object(None)
+ obj2 = W_Object(None)
+ a = obj2.slots['foo'] = W_Object(None)
+ obj1.protos.append(obj2)
+ assert obj1.lookup("foo") is a
+
+def test_protos():
+ space = ObjSpace()
+ x = W_Number(space, 2)
+ assert x.protos == [space.w_number]
\ No newline at end of file
Modified: pypy/branch/io-lang/pypy/lang/io/test/test_parse.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/test/test_parse.py (original)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_parse.py Thu Mar 26 18:16:25 2009
@@ -1,12 +1,16 @@
-from pypy.lang.io.parserhack import parse, implicit, Ast
+from pypy.lang.io.model import W_Message
+from pypy.lang.io.parserhack import parse
+from pypy.lang.io.objspace import ObjSpace
def test_simple():
+ space = ObjSpace()
input = "a b c"
- ast = parse(input)
- assert ast == Ast(Ast(Ast(implicit, "a"), "b"), "c")
+ ast = parse(input, space)
+ assert ast == W_Message(space, "a", [], W_Message(space, "b", [], W_Message(space, "c", [],)))
def test_simple_args():
+ space = ObjSpace()
input = "a + b c"
- ast = parse(input)
- assert ast == Ast(Ast(implicit, "a"), '+', [Ast(Ast(implicit, 'b'), 'c')])
+ ast = parse(input, space)
+ assert ast == W_Message(space, "a", [], W_Message(space, '+', [W_Message(space, "b", [], W_Message(space, 'c', [],))]))
More information about the Pypy-commit
mailing list