Selaa lähdekoodia

Start implementing a c library for turmits

Yann Weber 5 vuotta sitten
vanhempi
commit
f2f8341291
7 muutettua tiedostoa jossa 192 lisäystä ja 46 poistoa
  1. 9
    0
      Makefile
  2. 3
    0
      gte.sh
  3. 4
    1
      libs/Makefile
  4. 2
    2
      libs/cturmit.c
  5. 44
    43
      libs/cturmit.h
  6. 43
    0
      libs/turmit.c
  7. 87
    0
      libs/turmit.h

+ 9
- 0
Makefile Näytä tiedosto

@@ -0,0 +1,9 @@
1
+all: libs
2
+
3
+libs:
4
+	make -C libs
5
+
6
+.PHONY: libs clean
7
+
8
+clean:
9
+	make -C libs clean

+ 3
- 0
gte.sh Näytä tiedosto

@@ -0,0 +1,3 @@
1
+#!/bin/sh
2
+
3
+PYTHONPATH=":`pwd`/libs" /usr/bin/env python3 -m gte $*

+ 4
- 1
libs/Makefile Näytä tiedosto

@@ -2,7 +2,10 @@ PYTHON=/usr/bin/python3
2 2
 
3 3
 all: cturmit*.so
4 4
 
5
-cturmit*.so: setup.py build/lib.*/cturmit*.so
5
+turmit.o: turmit.c turmit.h
6
+	gcc -c turmit.c
7
+
8
+cturmit*.so: setup.py build/lib.*/cturmit*.so turmit.o
6 9
 	cp build/lib.*/cturmit*.so ./
7 10
 
8 11
 build/lib.*/cturmit*.so: cturmit.c cturmit.h

+ 2
- 2
libs/cturmit.c Näytä tiedosto

@@ -18,13 +18,13 @@ PyInit_cturmit(void)
18 18
 	return m;
19 19
 }
20 20
 
21
-void
21
+	void
22 22
 CTurmit_dealloc(CTurmit *self)
23 23
 {
24 24
 	Py_TYPE(self)->tp_free((PyObject*)self);
25 25
 }
26 26
 
27
-Py_ssize_t
27
+	Py_ssize_t
28 28
 CTurmit_len(CTurmit *self)
29 29
 {
30 30
 	return self->len;

+ 44
- 43
libs/cturmit.h Näytä tiedosto

@@ -1,6 +1,8 @@
1 1
 #include<Python.h>
2 2
 #include"structmember.h"
3 3
 
4
+#include "turmit.h"
5
+
4 6
 typedef struct
5 7
 {
6 8
 	PyObject VAR_HEAD;
@@ -18,10 +20,10 @@ static PyModuleDef cturmitmodule = {
18 20
 
19 21
 static PyMemberDef CTurmit_members[] = {
20 22
 	{"len", T_PYSSIZET, offsetof(CTurmit, len), 0,
21
-	 "Expression len"},
23
+		"Expression len"},
22 24
 	/*
23
-	{"heap_sz", T_PYSSIZET, offsetof(CTurmit, heap_sz), 0,
24
-	 "Item heap counter"},
25
+	   {"heap_sz", T_PYSSIZET, offsetof(CTurmit, heap_sz), 0,
26
+	   "Item heap counter"},
25 27
 	 */
26 28
 	{NULL}  /* Sentinel */
27 29
 };
@@ -37,7 +39,7 @@ static PyMethodDef CTurmit_methods[] = {
37 39
 };
38 40
 
39 41
 
40
-static PyObject *
42
+	static PyObject *
41 43
 CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42 44
 {
43 45
 	CTurmit *self;
@@ -51,7 +53,7 @@ CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
51 53
 void
52 54
 CTurmit_dealloc(CTurmit *self);
53 55
 
54
-static int
56
+	static int
55 57
 CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
56 58
 {
57 59
 	//Init attributes
@@ -61,44 +63,43 @@ CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds)
61 63
 
62 64
 static PyTypeObject CTurmitType = {
63 65
 	PyVarObject_HEAD_INIT(NULL, 0)
64
-	"cturmit.CTurmit",			 /* tp_name */
65
-	sizeof(CTurmit),			 /* tp_basicsize */
66
-	0,						 /* tp_itemsize */
67
-	(destructor)CTurmit_dealloc, /* tp_dealloc */
68
-	0,						 /* tp_print */
69
-	0,						 /* tp_getattr */
70
-	0,						 /* tp_setattr */
71
-	0,						 /* tp_reserved */
72
-	0,						 /* tp_repr */
73
-	0,						 /* tp_as_number */
74
-	0,						 /* tp_as_sequence */
75
-	CTurmit_asmapping,				 /* tp_as_mapping */
76
-	0,						 /* tp_hash  */
77
-	0,						 /* tp_call */
78
-	0,						 /* tp_str */
79
-	0,						 /* tp_getattro */
80
-	0,						 /* tp_setattro */
81
-	0,						 /* tp_as_buffer */
82
-	Py_TPFLAGS_DEFAULT |
83
-		Py_TPFLAGS_BASETYPE,   /* tp_flags */
84
-	"CTurmit objects",		   /* tp_doc */
85
-	0,						 /* tp_traverse */
86
-	0,						 /* tp_clear */
87
-	0,						 /* tp_richcompare */
88
-	0,						 /* tp_weaklistoffset */
89
-	0,						 /* tp_iter */
90
-	0,						 /* tp_iternext */
91
-	CTurmit_methods,			 /* tp_methods */
92
-	CTurmit_members,			 /* tp_members */
93
-	0,						 /* tp_getset */
94
-	0,						 /* tp_base */
95
-	0,						 /* tp_dict */
96
-	0,						 /* tp_descr_get */
97
-	0,						 /* tp_descr_set */
98
-	0,						 /* tp_dictoffset */
99
-	(initproc)CTurmit_init,	  /* tp_init */
100
-	0,						 /* tp_alloc */
101
-	CTurmit_new,				 /* tp_new */
66
+		"cturmit.CTurmit",			/* tp_name */
67
+	sizeof(CTurmit),				/* tp_basicsize */
68
+	0,						/* tp_itemsize */
69
+	(destructor)CTurmit_dealloc,			/* tp_dealloc */
70
+	0,						/* tp_print */
71
+	0,						/* tp_getattr */
72
+	0,						/* tp_setattr */
73
+	0,						/* tp_reserved */
74
+	0,						/* tp_repr */
75
+	0,						/* tp_as_number */
76
+	0,						/* tp_as_sequence */
77
+	CTurmit_asmapping,				/* tp_as_mapping */
78
+	0,						/* tp_hash  */
79
+	0,						/* tp_call */
80
+	0,						/* tp_str */
81
+	0,						/* tp_getattro */
82
+	0,						/* tp_setattro */
83
+	0,						/* tp_as_buffer */
84
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  	/* tp_flags */
85
+	"CTurmit objects",				/* tp_doc */
86
+	0,						/* tp_traverse */
87
+	0,						/* tp_clear */
88
+	0,						/* tp_richcompare */
89
+	0,						/* tp_weaklistoffset */
90
+	0,						/* tp_iter */
91
+	0,						/* tp_iternext */
92
+	CTurmit_methods,				/* tp_methods */
93
+	CTurmit_members,				/* tp_members */
94
+	0,						/* tp_getset */
95
+	0,						/* tp_base */
96
+	0,						/* tp_dict */
97
+	0,						/* tp_descr_get */
98
+	0,						/* tp_descr_set */
99
+	0,						/* tp_dictoffset */
100
+	(initproc)CTurmit_init,				/* tp_init */
101
+	0,						/* tp_alloc */
102
+	CTurmit_new,					/* tp_new */
102 103
 };
103 104
 
104 105
 

+ 43
- 0
libs/turmit.c Näytä tiedosto

@@ -0,0 +1,43 @@
1
+#include "turmit.h"
2
+
3
+TURMIT_OP(mem_sz)
4
+{
5
+	turmit_int *new;
6
+	turmit_int new_sz, old_sz;
7
+	
8
+	old_sz = turmit->stack_sz;
9
+	new_sz = SPOP(turmit);
10
+	new_sz = new_sz<2?2:new_sz;
11
+
12
+	new = realloc(turmit->stack, sizeof(turmit_int) * new_sz);
13
+	if(old_sz < new_sz)
14
+	{
15
+		bzero(turmit->stack + old_sz, new_sz - old_sz);
16
+	}
17
+}
18
+
19
+turmit_t *turmit_alloc(ssize_t stack_sz, ssize_t int_max, const char *expr,
20
+	unsigned char flags)
21
+{
22
+	turmit_t *res;
23
+
24
+	res = malloc(sizeof(struct turmit_s));
25
+	if(!res)
26
+	{
27
+		perror("Unable to allocate memory for turmit");
28
+		return NULL;
29
+	}
30
+	return turmit_init(res, stack_sz, int_max, expr, flags);
31
+}
32
+
33
+turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
34
+	const char *expr, unsigned char flags)
35
+{
36
+	int ret;
37
+
38
+	turmit->flags = flags;
39
+	turmit->stack_sz = stack_sz;
40
+	turmit->int_max = int_max;
41
+	turmit->expr = strndup(expr, TURMIT_EXPR_MAX_SZ);
42
+}
43
+

+ 87
- 0
libs/turmit.h Näytä tiedosto

@@ -0,0 +1,87 @@
1
+#include <stdlib.h>
2
+#include <stdio.h>
3
+#include <string.h>
4
+#include <errno.h>
5
+
6
+typedef unsigned long long int turmit_int;
7
+typedef struct turmit_op_s turmit_op_t;
8
+typedef struct turmit_s turmit_t;
9
+
10
+#define SCUR(t) (t->stack[t->stack_cur])
11
+#define SDEC(t) (t->stack_cur = (t->stack_cur>0?t->stack_cur:t->stack_sz) -1)
12
+#define SINC(t) (t->stack_cur = (t->stack_cur + 1) % t->stack_sz)
13
+#define SPOP(t) (__SPOP(t))
14
+#define SPUSH(t, v) (t->stack[SINC(t)] = v)
15
+
16
+
17
+#define TURMIT_EXPR_MAX_SZ	0xFFFFFFFF
18
+
19
+#define TURMIT_LAZY		0
20
+#define TURMIT_AUTOCOMP		1
21
+
22
+#define TURMIT_OK		0
23
+#define TURMIT_ERROR		-1
24
+
25
+
26
+union turmit_op_u
27
+{
28
+	void (*op)(turmit_t*, turmit_int[5]);
29
+	turmit_int val;
30
+};
31
+
32
+struct turmit_op_s
33
+{
34
+	union turmit_op_u op;
35
+	short value; /*!< 0 if op is an operation pointer */
36
+};
37
+
38
+struct turmit_s
39
+{
40
+	char *expr;
41
+	unsigned char flags;
42
+	ssize_t int_max;
43
+
44
+	turmit_op_t *op_expr;
45
+	ssize_t op_expr_sz;
46
+
47
+	ssize_t stack_sz;
48
+	turmit_int *stack;
49
+	ssize_t stack_cur;
50
+};
51
+
52
+inline static turmit_int __SPOP(turmit_t *t) {
53
+	turmit_int r = t->stack[t->stack_cur];
54
+	SDEC(t);
55
+	return r;
56
+}
57
+
58
+
59
+#define TURMIT_OP(NAME) static void NAME (turmit_t *turmit, turmit_int args[5])
60
+
61
+/**@brief Allocate memory and initialise a turmit
62
+ * @param const char* The turmit expression
63
+ * @param unsigned char One of TURMIT_LAZY or TURMIT_AUTOCOMP.
64
+ * @return A pointer on the new turmit
65
+ */
66
+turmit_t *turmit_alloc(ssize_t stack_sz, ssize_t int_max, const char *expr,
67
+	unsigned char flags);
68
+
69
+/**@brief Initialize a turmit
70
+ * @param turmit_t* The turmit to initialize
71
+ * @param const char* The turmit expression
72
+ * @param unsigned char One of TURMIT_LAZY or TURMIT_AUTOCOMP.
73
+ * @return The pointer on the turmit
74
+ */
75
+turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
76
+	const char *expr, unsigned char flags);
77
+
78
+/**@brief Free memory of a turmit
79
+ * @param turmit the turmit to free
80
+ */
81
+void turmit_free(turmit_t *turmit);
82
+
83
+/**@brief Populate turmit_t.op_expr and set op_expr_sz
84
+ * @return 0 if no errors
85
+ */
86
+int turmit_compile(turmit_t *turmit);
87
+

Loading…
Peruuta
Tallenna