summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/panelize/am_expression.py
blob: 9e19f71433d883800de9a362c5be38986847c907 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com>

from ..utils import *
from ..am_eval import OpCode
from ..am_statements import *

class AMExpression(object):
    CONSTANT = 1
    VARIABLE = 2
    OPERATOR = 3

    def __init__(self, kind):
        self.kind = kind

    @property
    def value(self):
        return self

    def optimize(self):
        pass
    
    def to_inch(self):
        return AMOperatorExpression(AMOperatorExpression.DIV, self, 
                                    AMConstantExpression(MILLIMETERS_PER_INCH))

    def to_metric(self):
        return AMOperatorExpression(AMOperatorExpression.MUL, self,
                                    AMConstantExpression(MILLIMETERS_PER_INCH))

    def to_gerber(self, settings=None):
        pass

    def to_instructions(self):
        pass
    
class AMConstantExpression(AMExpression):
    def __init__(self, value):
        super(AMConstantExpression, self).__init__(AMExpression.CONSTANT)
        self._value = value

    @property
    def value(self):
        return self._value

    def optimize(self):
        return self
    
    def to_gerber(self, settings=None):
        if isinstance(self._value, str):
            return self._value
        gerber = '%.6g' % self._value
        return '%.6f' % self._value if 'e' in gerber else gerber

    def to_instructions(self):
        return [(OpCode.PUSH, self._value)]
    
class AMVariableExpression(AMExpression):
    def __init__(self, number):
        super(AMVariableExpression, self).__init__(AMExpression.VARIABLE)
        self.number = number

    def optimize(self):
        return self
    
    def to_gerber(self, settings=None):
        return '$%d' % self.number
    
    def to_instructions(self):
        return (OpCode.LOAD, self.number)

class AMOperatorExpression(AMExpression):
    ADD = '+'
    SUB = '-'
    MUL = 'X'
    DIV = '/'

    def __init__(self, op, lvalue, rvalue):
        super(AMOperatorExpression, self).__init__(AMExpression.OPERATOR)
        self.op = op
        self.lvalue = lvalue
        self.rvalue = rvalue
    
    def optimize(self):
        self.lvalue = self.lvalue.optimize()
        self.rvalue = self.rvalue.optimize()

        if isinstance(self.lvalue, AMConstantExpression) and isinstance(self.rvalue, AMConstantExpression):
            lvalue = float(self.lvalue.value)
            rvalue = float(self.rvalue.value)
            value = lvalue + rvalue if self.op == self.ADD else \
                lvalue - rvalue if self.op == self.SUB else \
                lvalue * rvalue if self.op == self.MUL else \
                lvalue / rvalue if self.op == self.DIV else None
            return AMConstantExpression(value)
        elif self.op == self.ADD:
            if self.rvalue.value == 0:
                return self.lvalue
            elif self.lvalue.value == 0:
                return self.rvalue
        elif self.op == self.SUB:
            if self.rvalue.value == 0:
                return self.lvalue
            elif self.lvalue.value == 0 and isinstance(self.rvalue, AMConstantExpression):
                return AMConstantExpression(-self.rvalue.value)
        elif self.op == self.MUL:
            if self.rvalue.value == 1:
                return self.lvalue
            elif self.lvalue.value == 1:
                return self.rvalue
            elif self.lvalue == 0 or self.rvalue == 0:
                return AMConstantExpression(0)
        elif self.op == self.DIV:
            if self.rvalue.value == 1:
                return self.lvalue
            elif self.lvalue.value == 0:
                return AMConstantExpression(0)
        
        return self
        
    def to_gerber(self, settings=None):
        return '(%s)%s(%s)' % (self.lvalue.to_gerber(settings), self.op, self.rvalue.to_gerber(settings))

    def to_instructions(self):
        for i in self.lvalue.to_instructions():
            yield i
        for i in self.rvalue.to_instructions():
            yield i
        op = OpCode.ADD if self.op == self.ADD else\
             OpCode.SUB if self.op == self.SUB else\
             OpCode.MUL if self.op == self.MUL else\
             OpCode.DIV
        yield (op, None)

def eval_macro(instructions):
    stack = []

    def pop():
        return stack.pop()

    def push(op):
        stack.append(op)

    def top():
        return stack[-1]

    def empty():
        return len(stack) == 0

    for opcode, argument in instructions:
        if opcode == OpCode.PUSH:
            push(AMConstantExpression(argument))

        elif opcode == OpCode.LOAD:
            push(AMVariableExpression(argument))

        elif opcode == OpCode.STORE:
            yield (-argument, [pop()])

        elif opcode == OpCode.ADD:
            op1 = pop()
            op2 = pop()
            push(AMOperatorExpression(AMOperatorExpression.ADD, op2, op1))

        elif opcode == OpCode.SUB:
            op1 = pop()
            op2 = pop()
            push(AMOperatorExpression(AMOperatorExpression.SUB, op2, op1))

        elif opcode == OpCode.MUL:
            op1 = pop()
            op2 = pop()
            push(AMOperatorExpression(AMOperatorExpression.MUL, op2, op1))

        elif opcode == OpCode.DIV:
            op1 = pop()
            op2 = pop()
            push(AMOperatorExpression(AMOperatorExpression.DIV, op2, op1))

        elif opcode == OpCode.PRIM:
            yield (argument, stack)
            stack = []