Handle then unary negation operator.

pull/68/merge
Jussi Pakkanen 10 years ago
parent 8be84a4a7c
commit 3f7f984a39
  1. 10
      interpreter.py
  2. 11
      mparser.py
  3. 2
      test cases/common/59 object generator/meson.build

@ -945,6 +945,8 @@ class Interpreter():
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):
@ -1791,6 +1793,14 @@ class Interpreter():
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))

@ -41,7 +41,7 @@ class Lexer:
# 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)),
@ -114,6 +114,7 @@ class Lexer:
else:
value = match_text
yield Token(tid, curline, col, value)
break
if not matched:
raise ParseException('lexer', lineno, col)
@ -245,6 +246,12 @@ class IfClauseNode():
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
@ -392,6 +399,8 @@ class Parser:
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):

@ -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,

Loading…
Cancel
Save