Skip to content

Commit

Permalink
Handle then unary negation operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed May 9, 2015
1 parent 8be84a4 commit 3f7f984
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions interpreter.py
Expand Up @@ -945,6 +945,8 @@ def evaluate_statement(self, cur):
return self.evaluate_orstatement(cur)
elif isinstance(cur, mparser.NotNode):
return self.evaluate_notstatement(cur)
elif isinstance(cur, mparser.UMinusNode):
return self.evaluate_uminusstatement(cur)
elif isinstance(cur, mparser.ArithmeticNode):
return self.evaluate_arithmeticstatement(cur)
elif isinstance(cur, mparser.ForeachClauseNode):
Expand Down Expand Up @@ -1791,6 +1793,14 @@ def evaluate_notstatement(self, cur):
raise InterpreterException('Argument to "not" is not a boolean.')
return not v

def evaluate_uminusstatement(self, cur):
v = self.evaluate_statement(cur.value)
if isinstance(v, mparser.NumberNode):
v = v.value
if not isinstance(v, int):
raise InterpreterException('Argument to negation is not an integer.')
return -v

def evaluate_arithmeticstatement(self, cur):
l = self.to_native(self.evaluate_statement(cur.left))
r = self.to_native(self.evaluate_statement(cur.right))
Expand Down
11 changes: 10 additions & 1 deletion mparser.py
Expand Up @@ -41,7 +41,7 @@ def __init__(self):
# Need to be sorted longest to shortest.
('ignore', re.compile(r'[ \t]')),
('id', re.compile('[_a-zA-Z][_0-9a-zA-Z]*')),
('number', re.compile(r'-?\d+')),
('number', re.compile(r'\d+')),
('eol_cont', re.compile(r'\\\n')),
('eol', re.compile(r'\n')),
('multiline_string', re.compile(r"'''(.|\n)*?'''", re.M)),
Expand Down Expand Up @@ -114,6 +114,7 @@ def lex(self, code):
else:
value = match_text
yield Token(tid, curline, col, value)
break
if not matched:
raise ParseException('lexer', lineno, col)

Expand Down Expand Up @@ -245,6 +246,12 @@ def __init__(self, lineno, colno):
self.ifs = []
self.elseblock = EmptyNode()

class UMinusNode():
def __init__(self, lineno, colno, value):
self.lineno = lineno
self.colno = colno
self.value = value

class IfNode():
def __init__(self, lineno, colno, condition, block):
self.lineno = lineno
Expand Down Expand Up @@ -392,6 +399,8 @@ def e5div(self):
def e6(self):
if self.accept('not'):
return NotNode(self.current.lineno, self.current.colno, self.e7())
if self.accept('dash'):
return UMinusNode(self.current.lineno, self.current.colno, self.e7())
return self.e7()

def e7(self):
Expand Down
2 changes: 1 addition & 1 deletion test cases/common/59 object generator/meson.build
Expand Up @@ -12,7 +12,7 @@ else
outputname = '@BASENAME@.o'
endif

cc = meson.get_compiler('c').cmd_array().get(0-1)
cc = meson.get_compiler('c').cmd_array().get(-1)
# Generate an object file manually.
gen = generator(python,
output : outputname,
Expand Down

0 comments on commit 3f7f984

Please sign in to comment.