Browse Source

Add more methods, more tests passes

Yann Weber 6 years ago
parent
commit
10af037382
4 changed files with 77 additions and 7 deletions
  1. 25
    0
      libs/cturmit.c
  2. 37
    0
      libs/cturmit.h
  3. 1
    1
      libs/setup.py
  4. 14
    6
      libs/turmit.c

+ 25
- 0
libs/cturmit.c View File

@@ -195,8 +195,33 @@ but %ld found", sz);
195 195
 	Py_RETURN_NONE;
196 196
 }
197 197
 
198
+PyObject* CTurmit__pop(CTurmit* self)
199
+{
200
+	return PyLong_FromUnsignedLongLong(SPOP(self->turmit));
201
+}
202
+
203
+PyObject* CTurmit_get_shead(CTurmit *self, void *ref)
204
+{
205
+	return PyLong_FromUnsignedLongLong(SCUR(self->turmit));
206
+}
207
+
198 208
 PyObject* CTurmit_get__cur(CTurmit *self, void *ref)
199 209
 {
200 210
 	return PyLong_FromSsize_t(self->turmit->stack_cur);
201 211
 }
202 212
 
213
+PyObject* CTurmit_get__stack(CTurmit *self, void *ref)
214
+{
215
+	size_t i;
216
+	PyObject *res;
217
+
218
+	res = PyList_New(self->turmit->stack_sz);
219
+
220
+	for(i=0; i<self->turmit->stack_sz;i++)
221
+	{
222
+		PyList_SET_ITEM(res, i,
223
+			PyLong_FromUnsignedLongLong(self->turmit->stack[i]));
224
+	}
225
+	return res;
226
+}
227
+

+ 37
- 0
libs/cturmit.h View File

@@ -50,19 +50,56 @@ static PyMappingMethods CTurmit_asmapping[] = {{
50 50
 	METH_NOARGS, NULL}
51 51
 
52 52
 PyObject* CTurmit__push(CTurmit* self, PyObject *args);
53
+PyObject* CTurmit__pop(CTurmit* self);
54
+TURMIT_PYOP(mem_sz)
53 55
 TURMIT_PYOP(add)
56
+TURMIT_PYOP(sub)
57
+TURMIT_PYOP(bin_and)
58
+TURMIT_PYOP(dup)
59
+TURMIT_PYOP(lshift)
60
+TURMIT_PYOP(mod)
61
+TURMIT_PYOP(mul)
62
+TURMIT_PYOP(div)
63
+TURMIT_PYOP(bin_or)
64
+TURMIT_PYOP(bin_xor)
65
+TURMIT_PYOP(pop)
66
+TURMIT_PYOP(swp)
67
+TURMIT_PYOP(jmp)
68
+TURMIT_PYOP(jz)
69
+TURMIT_PYOP(jcmp)
54 70
 
55 71
 static PyMethodDef CTurmit_methods[] = {
56 72
 	{"_push", (PyCFunction)CTurmit__push, METH_KEYWORDS | METH_VARARGS,
57 73
 		"Push a value on the stack"},
74
+	{"_pop", (PyCFunction)CTurmit__pop, METH_NOARGS,
75
+		"Pop a value from the stack"},
76
+	TURMIT_PYOPDEF(mem_sz),
58 77
 	TURMIT_PYOPDEF(add),
78
+	TURMIT_PYOPDEF(sub),
79
+	TURMIT_PYOPDEF(bin_and),
80
+	TURMIT_PYOPDEF(dup),
81
+	TURMIT_PYOPDEF(lshift),
82
+	TURMIT_PYOPDEF(mod),
83
+	TURMIT_PYOPDEF(mul),
84
+	TURMIT_PYOPDEF(div),
85
+	TURMIT_PYOPDEF(bin_or),
86
+	TURMIT_PYOPDEF(bin_xor),
87
+	TURMIT_PYOPDEF(pop),
88
+	TURMIT_PYOPDEF(swp),
89
+	TURMIT_PYOPDEF(jmp),
90
+	TURMIT_PYOPDEF(jz),
91
+	TURMIT_PYOPDEF(jcmp),
59 92
 	{NULL}  /* Sentinel */
60 93
 };
61 94
 
95
+PyObject* CTurmit_get_shead(CTurmit *self, void *ref);
62 96
 PyObject* CTurmit_get__cur(CTurmit *self, void *ref);
97
+PyObject* CTurmit_get__stack(CTurmit *self, void *ref);
63 98
 
64 99
 static struct PyGetSetDef CTurmit_getset[] = {
100
+	{"shead", (getter)CTurmit_get_shead, NULL, NULL, NULL},
65 101
 	{"_cur", (getter)CTurmit_get__cur, NULL, NULL, NULL},
102
+	{"_stack", (getter)CTurmit_get__stack, NULL, NULL, NULL},
66 103
 	{NULL}  /* Sentinel */
67 104
 };
68 105
 

+ 1
- 1
libs/setup.py View File

@@ -1,7 +1,7 @@
1 1
 from distutils.core import setup, Extension
2 2
 
3 3
 module1 = Extension('cturmit',
4
-    sources = ['cturmit.c'])
4
+    sources = ['cturmit.c', 'turmit.c'])
5 5
 
6 6
 
7 7
 setup(

+ 14
- 6
libs/turmit.c View File

@@ -12,6 +12,8 @@ TURMIT_OP(mem_sz)
12 12
 	new = realloc(turmit->stack, sizeof(turmit_int) * new_sz);
13 13
 	if(!new) { return; }
14 14
 	turmit->stack = new;
15
+	turmit->stack_sz = new_sz;
16
+	turmit->stack_cur %= new_sz;
15 17
 	if(old_sz < new_sz)
16 18
 	{
17 19
 		bzero(turmit->stack + old_sz, new_sz - old_sz);
@@ -20,17 +22,20 @@ TURMIT_OP(mem_sz)
20 22
 
21 23
 TURMIT_OP(add)
22 24
 {
23
-	SPUSH(turmit, SPOP(turmit) + SPOP(turmit));
25
+	turmit_int a = SPOP(turmit), b = SPOP(turmit);
26
+	SPUSH(turmit, a+b);
24 27
 }
25 28
 
26 29
 TURMIT_OP(sub)
27 30
 {
28
-	SPUSH(turmit, SPOP(turmit) - SPOP(turmit));
31
+	turmit_int a = SPOP(turmit), b = SPOP(turmit);
32
+	SPUSH(turmit, b - a);
29 33
 }
30 34
 
31 35
 TURMIT_OP(bin_and)
32 36
 {
33
-	SPUSH(turmit, SPOP(turmit) & SPOP(turmit));
37
+	turmit_int a = SPOP(turmit), b = SPOP(turmit);
38
+	SPUSH(turmit, b & a);
34 39
 }
35 40
 
36 41
 TURMIT_OP(dup)
@@ -55,7 +60,8 @@ TURMIT_OP(mod)
55 60
 
56 61
 TURMIT_OP(mul)
57 62
 {
58
-	SPUSH(turmit, SPOP(turmit) * SPOP(turmit));
63
+	turmit_int a = SPOP(turmit), b = SPOP(turmit);
64
+	SPUSH(turmit, a*b);
59 65
 }
60 66
 
61 67
 TURMIT_OP(div)
@@ -68,12 +74,14 @@ TURMIT_OP(div)
68 74
 
69 75
 TURMIT_OP(bin_or)
70 76
 {
71
-	SPUSH(turmit, SPOP(turmit) | SPOP(turmit));
77
+	turmit_int a = SPOP(turmit), b = SPOP(turmit);
78
+	SPUSH(turmit, a | b);
72 79
 }
73 80
 
74 81
 TURMIT_OP(bin_xor)
75 82
 {
76
-	SPUSH(turmit, SPOP(turmit) ^ SPOP(turmit));
83
+	turmit_int a = SPOP(turmit), b = SPOP(turmit);
84
+	SPUSH(turmit, a ^ b);
77 85
 }
78 86
 
79 87
 TURMIT_OP(pop)

Loading…
Cancel
Save