Permalink
Browse files

Start building list class

  • Loading branch information...
mattgreen committed Feb 1, 2015
1 parent 4da3bee commit fb7ec1971383bc69fe4c535f1051764df353179b
Showing with 104 additions and 3 deletions.
  1. +42 −0 lib/builtins.py
  2. +35 −0 src/Hython/BuiltinTypes/List.hs
  3. +9 −3 src/Hython/Builtins.hs
  4. +1 −0 src/Hython/InterpreterState.hs
  5. +17 −0 test/builtins/list.py
View
@@ -13,3 +13,45 @@ def issubclass(cls, clsInfo):
def print(s):
__hython_primitive__("print", s)
class list(object):
def __init__(self):
self._list = __hython_primitive__("list-new")
def __len__(self):
return __hython_primitive__("list-length", self._list)
def append(self, obj):
__hython_primitive__("list-append", self._list, obj)
def clear(self):
__hython_primitive__("list-clear", self._list)
def extend(self, r):
if isinstance(r, list):
__hython_primitive__("list-concat", self._list, r._list)
else:
raise TypeError("not iterable")
class range(object):
def __init__(self, startOrStop, stop=None, step=None):
if stop is None:
self.start = 0
self.stop = startOrStop
else:
self.start = startOrStop
self.stop = stop
if step is None:
step = 1
self.step = 1
self.values = []
i = self.start
while i < self.stop:
self.values = self.values + [i]
i = i + self.step
print(self.values)
@@ -0,0 +1,35 @@
module Hython.BuiltinTypes.List
where
import Control.Monad.State
import Data.IORef
import Hython.InterpreterState
import Hython.Object
listNew :: PrimitiveFn
listNew _ = do
items <- liftIO $ newIORef []
return $ List items
listAppend :: PrimitiveFn
listAppend [l@(List ref), obj] = do
liftIO $ modifyIORef' ref (\l -> l ++ [obj])
return l
listConcat :: PrimitiveFn
listConcat [l@(List lRef), List rRef] = do
items <- liftIO $ readIORef rRef
liftIO $ modifyIORef' lRef (++ items)
return l
listClear :: PrimitiveFn
listClear [l@(List ref)] = do
liftIO $ modifyIORef' ref (const [])
return l
listLength :: PrimitiveFn
listLength [List ref] = do
items <- liftIO $ readIORef ref
return $ Int (fromIntegral (length items))
View
@@ -16,6 +16,8 @@ import Hython.Class
import Hython.InterpreterState
import Hython.Object
import Hython.BuiltinTypes.List
builtins :: IO [(String, Object)]
builtins = do
emptyDict <- AttributeDict.empty
@@ -87,15 +89,19 @@ pow [Float l, Float r, Float m] = do
pow _ = error "pow() takes at least two arguments"
type PrimitiveFn = (Objects -> Interpreter Object)
primitive :: Objects -> Interpreter Object
primitive (String name : extraArgs) = case lookup name primitiveFunctions of
Just f -> f extraArgs
Nothing -> fail "bad primitive"
where
primitiveFunctions :: [(String, PrimitiveFn)]
primitiveFunctions = [("issubclass", issubclass), ("print", print')]
primitiveFunctions = [("issubclass", issubclass),
("list-new", listNew),
("list-append", listAppend),
("list-clear", listClear),
("list-concat", listConcat),
("list-length", listLength),
("print", print')]
issubclass [cls1, cls2] = return $ Bool (isSubClass l r)
where
@@ -29,3 +29,4 @@ data Config = Config {
tracingEnabled :: Bool
}
type PrimitiveFn = (Objects -> Interpreter Object)
View
@@ -4,3 +4,20 @@
print([1,2,])
print([1,2,3][0])
print([1,2,3][1])
l = list()
print(l.__len__())
l.append(1)
print(l.__len__())
l.append(2)
print(l.__len__())
r = list()
r.append(1)
l.extend(r)
print(l.__len__())
l.clear()
print(l.__len__())

0 comments on commit fb7ec19

Please sign in to comment.