This repository was archived by the owner on Jan 4, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''Roots of Lisp primitives in Python.
2
+ Let's see how far we can get embedding an Arc-like Lisp in Python.
3
+ '''
4
+
5
+ # We can't use tuples here because we need mutable cons cells.
6
+ # Objects are big and slow.
7
+ cons = lambda x , y : [x ,y ]
8
+ car = lambda x : x [0 ]
9
+ cdr = lambda x : x [1 ]
10
+
11
+ nil = None
12
+ # Atom is a problem: we're going to expose Python 2-tuples in our Lisp
13
+ # eventually, and those are probably atoms. Let's just get it working
14
+ # and then we'll worry about the distinction; the associated car/cdr
15
+ # implementations are bad too.
16
+ atom = lambda x : not (isinstance (x , tuple ) and len (x ) == 2 )
17
+
18
+ # Readable types:
19
+ # - lists
20
+ # - strings
21
+ # - numbers as in Python
22
+ # - symbols
23
+
24
+ def _topylist (L ):
25
+ acc = []
26
+ while L is not None :
27
+ acc .append (car (L ))
28
+ L = cdr (L )
29
+ return acc
30
+
31
+ def _toarclist (L ):
32
+ res = nil
33
+ for x in reversed (L ):
34
+ res = cons (x , res )
35
+ return res
36
+
37
+ def last (x ):
38
+ while cdr (x ) is not nil :
39
+ x = cdr (x )
40
+ return x
41
+
42
+ def nconc (x , y ):
43
+ if x is nil :
44
+ return y
45
+ else :
46
+ # XXX: representation dependent.
47
+ last (x )[1 ] = y
48
+ return x
49
+
50
+ def nconc1 (x , y ):
51
+ return nconc (x , list (y ))
52
+
53
+ def list (* args ):
54
+ return _toarclist (args )
Original file line number Diff line number Diff line change
1
+ import unittest
2
+
3
+ from roots import list
4
+ import sexpr
5
+
6
+ def interned (L ):
7
+ return [intern (x ) for x in L ]
8
+
9
+ class TestSexpr (unittest .TestCase ):
10
+ def testsymbol (self ):
11
+ self .assert_ (sexpr .str2sexpr ('a' )[0 ] is intern ('a' ))
12
+
13
+ def teststring (self ):
14
+ self .assertEqual (sexpr .str2sexpr ('"1"' )[0 ], '1' )
15
+
16
+ def testnum (self ):
17
+ self .assertEqual (sexpr .str2sexpr ('1' )[0 ], 1 )
18
+
19
+ def testfloat (self ):
20
+ self .assertEqual (sexpr .str2sexpr ('0.5' )[0 ], 0.5 )
21
+
22
+ def testescape (self ):
23
+ self .assertEqual (sexpr .str2sexpr ('\\ "string\\ "' )[0 ], '"string"' )
24
+
25
+ def testemptylist (self ):
26
+ self .assertEqual (sexpr .str2sexpr ('()' )[0 ], list ())
27
+
28
+ def testshortlist (self ):
29
+ self .assertEqual (sexpr .str2sexpr ('(1)' )[0 ], list (1 ))
30
+
31
+ def testlist (self ):
32
+ self .assertEqual (sexpr .str2sexpr ('(a b c)' )[0 ], list ('a' , 'b' , 'c' ))
33
+
34
+ if __name__ == '__main__' :
35
+ unittest .main ()
You can’t perform that action at this time.
0 commit comments