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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2019 Hiroshi Murayama <opiopan@gmail.com>
import operator
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):
return self
def to_inch(self):
return AMOperatorExpression.div(self, MILLIMETERS_PER_INCH)
def to_metric(self):
return AMOperatorExpression.mul(self, 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 __float__(self):
return float(self._value)
@staticmethod
def _amex_val(other):
return float(other) if isinstance(other, AMConstantExpression) else other
def __eq__(self, val):
return self._value == AMConstantExpression._amex_val(val)
def __ne__(self, val):
return self._value != AMConstantExpression._amex_val(val)
def __lt__(self, val):
return self._value < AMConstantExpression._amex_val(val)
def __gt__(self, val):
return self._value > AMConstantExpression._amex_val(val)
def __le__(self, val):
return self._value <= AMConstantExpression._amex_val(val)
def __ge__(self, val):
return self._value >= AMConstantExpression._amex_val(val)
def to_gerber(self, settings=None):
if isinstance(self._value, str):
return self._value
return f'{self.value:.6f}'.rstrip('0').rstrip('.')
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 to_gerber(self, settings=None):
return f'${self.number}'
def to_instructions(self):
return (OpCode.LOAD, self.number)
class AMOperatorExpression(AMExpression):
def __init__(self, op, lvalue, rvalue):
super(AMOperatorExpression, self).__init__(AMExpression.OPERATOR)
self.op = op
self.lvalue = AMConstantExpression(lvalue) if isinstance(lvalue, (int, float)) else lvalue
self.rvalue = AMConstantExpression(rvalue) if isinstance(rvalue, (int, float)) else rvalue
@classmethod
def add(kls, lvalue, rvalue):
return kls(operator.add, lvalue, rvalue)
@classmethod
def sub(kls, lvalue, rvalue):
return kls(operator.sub, lvalue, rvalue)
@classmethod
def mul(kls, lvalue, rvalue):
return kls(operator.mul, lvalue, rvalue)
@classmethod
def div(kls, lvalue, rvalue):
return kls(operator.truediv, lvalue, rvalue)
def optimize(self):
l = self.lvalue = self.lvalue.optimize()
r = self.rvalue = self.rvalue.optimize()
if isinstance(l, AMConstantExpression) and isinstance(r, AMConstantExpression):
return AMConstantExpression(self.op(float(r), float(l)))
elif self.op == operator.ADD:
if r == 0:
return l
elif l == 0:
return r
elif self.op == operator.SUB:
if r == 0:
return l
elif l == 0 and isinstance(r, AMConstantExpression):
return AMConstantExpression(-float(r))
elif self.op == operator.MUL:
if r == 1:
return l
elif l == 1:
return r
elif l == 0 or r == 0:
return AMConstantExpression(0)
elif self.op == operator.TRUEDIV:
if r == 1:
return self.lvalue
elif l == 0:
return AMConstantExpression(0)
return self
def to_gerber(self, settings=None):
lval = self.lvalue.to_gerber(settings)
rval = self.rvalue.to_gerber(settings))
op = {AMOperatorExpression.ADD: '+',
AMOperatorExpression.SUB: '-',
AMOperatorExpression.MUL: 'x',
AMOperatorExpression.DIV: '/'} [self.op]
return '(' + lval + op + rval + ')'
def to_instructions(self):
for i in self.lvalue.to_instructions():
yield i
for i in self.rvalue.to_instructions():
yield i
op = {AMOperatorExpression.ADD: OpCode.ADD,
AMOperatorExpression.SUB: OpCode.SUB,
AMOperatorExpression.MUL: OpCode.MUL,
AMOperatorExpression.DIV: OpCode.DIV} [self.op]
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 = []
|