Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions mesonbuild/ast/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def node_to_runtime_value(self, node: T.Union[UnknownValue, BaseNode, TYPE_var])
return [left] + right
if isinstance(left, UnknownValue) or isinstance(right, UnknownValue):
return UnknownValue()
if node.operation == 'add':
if node.operation == '+':
if isinstance(left, dict) and isinstance(right, dict):
ret = left.copy()
for k, v in right.items():
Expand All @@ -582,16 +582,16 @@ def node_to_runtime_value(self, node: T.Union[UnknownValue, BaseNode, TYPE_var])
right = [right]
return left + right
return left + right
elif node.operation == 'sub':
elif node.operation == '-':
return left - right
elif node.operation == 'mul':
elif node.operation == '*':
return left * right
elif node.operation == 'div':
elif node.operation == '/':
if isinstance(left, int) and isinstance(right, int):
return left // right
elif isinstance(left, str) and isinstance(right, str):
return os.path.join(left, right).replace('\\', '/')
elif node.operation == 'mod':
elif node.operation == '%':
if isinstance(left, int) and isinstance(right, int):
return left % right
elif isinstance(node, (UnknownValue, IntrospectionBuildTarget, IntrospectionFile, IntrospectionDependency, str, bool, int)):
Expand All @@ -613,7 +613,7 @@ def node_to_runtime_value(self, node: T.Union[UnknownValue, BaseNode, TYPE_var])
return left != right
elif node.ctype == 'in':
return left in right
elif node.ctype == 'notin':
elif node.ctype == 'not in':
return left not in right
elif isinstance(node, mparser.TernaryNode):
cond = self.node_to_runtime_value(node.condition)
Expand Down Expand Up @@ -665,7 +665,7 @@ def evaluate_plusassign(self, node: PlusAssignmentNode) -> None:
if isinstance(lhs, UnknownValue):
newval = UnknownValue()
else:
newval = mparser.ArithmeticNode(operation='add', left=lhs, operator=_symbol('+'), right=node.value)
newval = mparser.ArithmeticNode(operation='+', left=lhs, operator=_symbol('+'), right=node.value)
self.cur_assignments[node.var_name.value].append((self.nesting.copy(), newval))
self.all_assignment_nodes[node.var_name.value].append(node)

Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/ast/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def precedence_level(node: mparser.BaseNode) -> int:
elif isinstance(node, mparser.ComparisonNode):
return 4
elif isinstance(node, mparser.ArithmeticNode):
if node.operation in {'add', 'sub'}:
if node.operation in {'+', '-'}:
return 5
elif node.operation in {'mod', 'mul', 'div'}:
elif node.operation in {'%', '*', '/'}:
return 6
elif isinstance(node, (mparser.NotNode, mparser.UMinusNode)):
return 7
Expand Down Expand Up @@ -138,7 +138,7 @@ def visit_AndNode(self, node: mparser.AndNode) -> None:

def visit_ComparisonNode(self, node: mparser.ComparisonNode) -> None:
node.left.accept(self)
self.append_padded(node.ctype if node.ctype != 'notin' else 'not in', node)
self.append_padded(node.ctype, node)
node.lineno = self.curr_line or node.lineno
node.right.accept(self)

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/cargo/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def not_in(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.Compar
:param rhs: the right hand side of the "not in"
:return: A comparison node
"""
return mparser.ComparisonNode('notin', lhs, self._symbol('not in'), rhs)
return mparser.ComparisonNode('not in', lhs, self._symbol('not in'), rhs)

def or_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.OrNode:
"""Create and OrNode
Expand Down Expand Up @@ -202,7 +202,7 @@ def plus(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.Arithmet
:param rhs: The right of the addition
:return: The ArithmeticNode
"""
return mparser.ArithmeticNode('add', lhs, self._symbol('+'), rhs)
return mparser.ArithmeticNode('+', lhs, self._symbol('+'), rhs)

def plusassign(self, value: mparser.BaseNode, varname: str) -> mparser.PlusAssignmentNode:
"""Create a "+=" node
Expand Down
26 changes: 5 additions & 21 deletions mesonbuild/interpreterbase/interpreterbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from .. import mlog
from . import operator
from .decorators import FeatureNew
from .disabler import Disabler, is_disabled
from .helpers import default_resolve_key, flatten, resolve_second_level_holders, stringifyUserArguments
Expand Down Expand Up @@ -344,24 +345,14 @@ def evaluate_comparison(self, node: mparser.ComparisonNode) -> InterpreterObject
if isinstance(val2, Disabler):
return val2

# New code based on InterpreterObjects
operator = {
'in': MesonOperator.IN,
'notin': MesonOperator.NOT_IN,
'==': MesonOperator.EQUALS,
'!=': MesonOperator.NOT_EQUALS,
'>': MesonOperator.GREATER,
'<': MesonOperator.LESS,
'>=': MesonOperator.GREATER_EQUALS,
'<=': MesonOperator.LESS_EQUALS,
}[node.ctype]
op = operator.MAPPING[node.ctype]

# Check if the arguments should be reversed for simplicity (this essentially converts `in` to `contains`)
if operator in (MesonOperator.IN, MesonOperator.NOT_IN):
if op in (MesonOperator.IN, MesonOperator.NOT_IN):
val1, val2 = val2, val1

val1.current_node = node
return self._holderify(val1.operator_call(operator, _unholder(val2)))
return self._holderify(val1.operator_call(op, _unholder(val2)))

def evaluate_andstatement(self, cur: mparser.AndNode) -> InterpreterObject:
l = self.evaluate_statement(cur.left)
Expand Down Expand Up @@ -414,15 +405,8 @@ def evaluate_arithmeticstatement(self, cur: mparser.ArithmeticNode) -> Interpret
if l is None or r is None:
raise InvalidCodeOnVoid(cur.operation)

mapping: T.Dict[str, MesonOperator] = {
'add': MesonOperator.PLUS,
'sub': MesonOperator.MINUS,
'mul': MesonOperator.TIMES,
'div': MesonOperator.DIV,
'mod': MesonOperator.MOD,
}
l.current_node = cur
res = l.operator_call(mapping[cur.operation], _unholder(r))
res = l.operator_call(operator.MAPPING[cur.operation], _unholder(r))
return self._holderify(res)

def evaluate_ternary(self, node: mparser.TernaryNode) -> T.Optional[InterpreterObject]:
Expand Down
5 changes: 5 additions & 0 deletions mesonbuild/interpreterbase/operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

from enum import Enum
import typing as T

class MesonOperator(Enum):
# Arithmetic
Expand Down Expand Up @@ -30,3 +31,7 @@ class MesonOperator(Enum):
IN = 'in'
NOT_IN = 'not in'
INDEX = '[]'

# Accessing this directly is about 9x faster than calling MesonOperator(s),
# and about 3 times faster than a staticmethod
MAPPING: T.Mapping[str, MesonOperator] = {x.value: x for x in MesonOperator}
4 changes: 2 additions & 2 deletions mesonbuild/machinefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ def _evaluate_statement(self, node: mparser.BaseNode) -> ElementaryOptionValues:
elif isinstance(node, mparser.ArithmeticNode):
l = self._evaluate_statement(node.left)
r = self._evaluate_statement(node.right)
if node.operation == 'add':
if node.operation == '+':
if isinstance(l, str) and isinstance(r, str):
return l + r
if isinstance(l, list) and isinstance(r, list):
return l + r
elif node.operation == 'div':
elif node.operation == '/':
if isinstance(l, str) and isinstance(r, str):
return os.path.join(l, r)
raise MesonException('Unsupported node type')
Expand Down
Loading
Loading