ast: resolve simple arithmetic nodes

pull/5292/head
Daniel Mensinger 6 years ago
parent 2bb4ec19f2
commit feff2630ae
No known key found for this signature in database
GPG Key ID: 54DD94C131E277D4
  1. 33
      mesonbuild/ast/interpreter.py
  2. 5
      test cases/unit/55 introspection/meson.build

@ -20,7 +20,7 @@ from .. import interpreterbase, mparser, mesonlib
from .. import environment
from ..interpreterbase import InvalidArguments, BreakRequest, ContinueRequest
from ..mparser import BaseNode, ElementaryNode, IdNode, ArgumentNode, ArrayNode, AssignmentNode, PlusAssignmentNode, TernaryNode, EmptyNode
from ..mparser import BaseNode, ElementaryNode, IdNode, ArgumentNode, ArrayNode, ArithmeticNode, AssignmentNode, PlusAssignmentNode, TernaryNode, EmptyNode
import os, sys
from typing import List, Any, Optional
@ -230,18 +230,43 @@ class AstInterpreter(interpreterbase.InterpreterBase):
self.assign_vals[node.var_name] = [self.evaluate_statement(node.value)] # Evaluate the value just in case
def flatten_args(self, args: Any, include_unknown_args: bool = False) -> List[str]:
# Resolve ArrayNode if needed
def quick_resolve(n: BaseNode) -> Any:
if isinstance(n, IdNode):
if n.value not in self.assignments:
return []
return quick_resolve(self.assignments[n.value][0])
elif isinstance(n, ElementaryNode):
return n.value
else:
return n
flattend_args = []
if isinstance(args, ArrayNode):
args = [x for x in args.args.arguments]
elif isinstance(args, ArgumentNode):
args = [x for x in args.arguments]
elif isinstance(args, ArithmeticNode):
if args.operation != 'add':
return [] # Only handle string and array concats
l = quick_resolve(args.left)
r = quick_resolve(args.right)
if isinstance(l, str) and isinstance(r, str):
args = [l + r] # String concatination detected
else:
args = self.flatten_args(l) + self.flatten_args(r)
# Make sure we are always dealing with lists
if not isinstance(args, list):
args = [args]
# Resolve the contents of args
for i in args:
if isinstance(i, IdNode):
flattend_args += self.flatten_args(self.assignments.get(i.value, []), include_unknown_args)
elif isinstance(i, (ArrayNode, ArgumentNode)):
flattend_args += self.flatten_args(quick_resolve(i), include_unknown_args)
elif isinstance(i, (ArrayNode, ArgumentNode, ArithmeticNode)):
flattend_args += self.flatten_args(i, include_unknown_args)
elif isinstance(i, mparser.ElementaryNode):
flattend_args += [i.value]

@ -20,9 +20,12 @@ endif
subdir('sharedlib')
subdir('staticlib')
var1 = '1'
var2 = '2'
var3 = 'test3'
t1 = executable('test1', 't1.cpp', link_with: [sharedlib], install: true, build_by_default: get_option('test_opt2'))
t1 = executable('test' + var1, ['t1.cpp'], link_with: [sharedlib], install: true, build_by_default: get_option('test_opt2'))
#t2 = executable('test@0@'.format('' + '@0@'.format(var2)), 't2.cpp', link_with: [staticlib])
t2 = executable('test2', 't2.cpp', link_with: [staticlib])
t3 = executable(var3, 't3.cpp', link_with: [sharedlib, staticlib], dependencies: [dep1])

Loading…
Cancel
Save