From f6dbe87c03c91cb4ba6672e7dee81475b86c1384 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Silva Date: Fri, 6 Mar 2015 15:00:16 -0300 Subject: Add support for unary minus operator on macro parsing --- gerber/am_read.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'gerber') diff --git a/gerber/am_read.py b/gerber/am_read.py index 05f3343..872c513 100644 --- a/gerber/am_read.py +++ b/gerber/am_read.py @@ -126,6 +126,9 @@ def read_macro(macro): equation_left_side = 0 primitive_code = 0 + unary_minus_allowed = False + unary_minus = False + if Token.EQUALS in block: is_equation = True else: @@ -159,7 +162,14 @@ def read_macro(macro): while not empty(): instructions.append((token_to_opcode(pop()), None)) + unary_minus_allowed = True + elif c in Token.OPERATORS: + if c == Token.SUB and unary_minus_allowed: + unary_minus = True + unary_minus_allowed = False + continue + while not empty() and is_op(top()) and precedence(top()) >= precedence(c): instructions.append((token_to_opcode(pop()), None)) @@ -204,8 +214,12 @@ def read_macro(macro): if is_primitive and not found_primitive_code: primitive_code = scanner.readint() else: - instructions.append((OpCode.PUSH, scanner.readfloat())) + n = scanner.readfloat() + if unary_minus: + unary_minus = False + n *= -1 + instructions.append((OpCode.PUSH, n)) else: # whitespace or unknown char pass -- cgit