summaryrefslogtreecommitdiff
path: root/cad/src/experimental/pyrex-atoms-bonds/iguana/procops.py
blob: f5b49265a192f3a4025c1e58aaa27ca7458c4898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
# $Id$

import string, os, sys, re

variableName = re.compile("^[_a-zA-Z][_a-zA-Z0-9]*$")

class Op:

    def __init__(self, str):
        lst = string.split(str, '#')

        self.name = string.split(lst.pop(0))
        self.cname = self.name[0]
        self.name = self.name[len(self.name) - 1]

        self.inputs = string.split(lst.pop(0))
        self.inputs.reverse()   # because we're popping them
        self.outputs = string.split(lst.pop(0))
        self.tests = ""
        if lst:
            blanks = ' \t\r\n'
            tests = lst.pop(0)
            while tests[:1] in blanks:
                tests = tests[1:]
            while tests[-1:] in blanks:
                tests = tests[:-1]
            self.tests = tests
        assert len(lst) == 0

    def cfunction(self):
        r = (("static int igverb_%s (iguana_thread_object" +
              " *self, int pc)\n{\n") % self.cname)
        vars = [ ]
        for x in self.inputs + self.outputs:
            if variableName.search(x) and x not in vars:
                vars.append(x)
        for x in vars:
            r = r + "    double %s;\n" % x
        if self.inputs:
            r = r + "    CHECK_UNDERFLOW(%d);\n" % len(self.inputs)
        if self.outputs:
            r = r + "    CHECK_OVERFLOW(%d);\n" % len(self.outputs)
        for x in self.inputs:
            r = r + "    self->dspointer--;\n"
            r = r + "    %s = self->data_stack[self->dspointer];\n" % x
        if self.tests:
            r = r + "    " + self.tests + "\n"
        for x in self.outputs:
            r = r + "    self->data_stack[self->dspointer] = %s;\n" % x
            r = r + "    self->dspointer++;\n"
        return r + "    return pc;\n}\n\n"

print """#include "Python.h"
#include "ighelp.h"
#define EVILRETURN -1
extern PyObject *IguanaError;
"""

oplist = [ ]

for L in open("ops.b").readlines():
    if len(string.split(L, '#')) > 1:
        op = Op(L)
        oplist.append(op)
        print op.cfunction()

print """
void add_more_verbs(PyObject *verb_dict)
{"""

for op in oplist:
    print ("""    PyDict_SetItemString(verb_dict, "%s",
                         PyInt_FromLong((long) igverb_%s));"""
           % (op.name, op.cname))
    
print "}"