|
| 1 | +from easycoder import Handler, FatalError, RuntimeError, math |
| 2 | + |
| 3 | +class Points(Handler): |
| 4 | + |
| 5 | + def __init__(self, compiler): |
| 6 | + Handler.__init__(self, compiler) |
| 7 | + |
| 8 | + def getName(self): |
| 9 | + return 'points' |
| 10 | + |
| 11 | + ############################################################################# |
| 12 | + # Keyword handlers |
| 13 | + |
| 14 | + def k_point(self, command): |
| 15 | + return self.compileVariable(command, False) |
| 16 | + |
| 17 | + def r_point(self, command): |
| 18 | + return self.nextPC() |
| 19 | + |
| 20 | + # set {point} to {xvalue} {yvalue} |
| 21 | + def k_set(self, command): |
| 22 | + if self.nextIsSymbol(): |
| 23 | + pointRecord = self.getSymbolRecord() |
| 24 | + if pointRecord['keyword'] == 'point': |
| 25 | + if self.nextIs('to'): |
| 26 | + xValue = self.nextValue() |
| 27 | + yValue = self.nextValue() |
| 28 | + command['x'] = xValue |
| 29 | + command['y'] = yValue |
| 30 | + command['name'] = pointRecord['name'] |
| 31 | + self.add(command) |
| 32 | + return True |
| 33 | + |
| 34 | + def r_set(self, command): |
| 35 | + pointRecord = self.getVariable(command['name']) |
| 36 | + x = self.getRuntimeValue(command['x']) |
| 37 | + y = self.getRuntimeValue(command['y']) |
| 38 | + pointRecord['x'] = x |
| 39 | + pointRecord['y'] = y |
| 40 | + return self.nextPC() |
| 41 | + |
| 42 | + ############################################################################# |
| 43 | + # Compile a value in this domain |
| 44 | + def compileValue(self): |
| 45 | + value = {} |
| 46 | + value['domain'] = 'points' |
| 47 | + if self.tokenIs('the'): |
| 48 | + self.nextToken() |
| 49 | + token = self.getToken() |
| 50 | + if token == 'distance': |
| 51 | + if self.nextIs('between'): |
| 52 | + if self.nextIsSymbol(): |
| 53 | + point1Record = self.getSymbolRecord() |
| 54 | + if point1Record['keyword'] == 'point': |
| 55 | + if self.nextIs('and'): |
| 56 | + if self.nextIsSymbol(): |
| 57 | + point2Record = self.getSymbolRecord() |
| 58 | + if point2Record['keyword'] == 'point': |
| 59 | + value['type'] = 'difference' |
| 60 | + value['point1'] = point1Record['name'] |
| 61 | + value['point2'] = point2Record['name'] |
| 62 | + return value |
| 63 | + return None |
| 64 | + |
| 65 | + ############################################################################# |
| 66 | + # Modify a value or leave it unchanged. |
| 67 | + def modifyValue(self, value): |
| 68 | + return value |
| 69 | + |
| 70 | + ############################################################################# |
| 71 | + # Value handlers |
| 72 | + |
| 73 | + def v_difference(self, v): |
| 74 | + point1 = self.getVariable(v['point1']) |
| 75 | + point2 = self.getVariable(v['point2']) |
| 76 | + dx = point2['x'] - point1['x'] |
| 77 | + dy = point2['y'] - point1['y'] |
| 78 | + value = {} |
| 79 | + value['type'] = 'int' |
| 80 | + value['content'] = int(math.sqrt(dx*dx + dy*dy)) |
| 81 | + return value |
| 82 | + |
| 83 | + ############################################################################# |
| 84 | + # Compile a condition |
| 85 | + def compileCondition(self): |
| 86 | + condition = {} |
| 87 | + return condition |
| 88 | + |
| 89 | + ############################################################################# |
| 90 | + # Condition handlers |
0 commit comments