aboutsummaryrefslogtreecommitdiff
path: root/interpreter.py
diff options
context:
space:
mode:
authorAfief Halumi <afief.h@gmail.com>2015-05-07 23:40:50 +0300
committerAfief Halumi <afief.h@gmail.com>2015-05-07 23:51:09 +0300
commit6cdfb6a42529cc3309362dd3486de557427521ff (patch)
tree2ec5203a002b5c07afb66b52a47261b0bb62d4bd /interpreter.py
parent5401a98fbf4abc385b9e3b3b0fae62013532501e (diff)
downloadmeson-6cdfb6a42529cc3309362dd3486de557427521ff.zip
meson-6cdfb6a42529cc3309362dd3486de557427521ff.tar.gz
meson-6cdfb6a42529cc3309362dd3486de557427521ff.tar.bz2
Make the message() function able to print out user defined types.
E.g. Strings, integers, arrays and arrays thereof.
Diffstat (limited to 'interpreter.py')
-rw-r--r--interpreter.py34
1 files changed, 29 insertions, 5 deletions
diff --git a/interpreter.py b/interpreter.py
index 7e1f887..4297633 100644
--- a/interpreter.py
+++ b/interpreter.py
@@ -41,9 +41,8 @@ class InvalidArguments(InterpreterException):
def check_stringlist(a, msg='Arguments must be strings.'):
if not isinstance(a, list):
raise InvalidArguments('Argument not a list.')
- for s in a:
- if not isinstance(s, str):
- raise InvalidArguments(msg)
+ if not all(isinstance(s, str) for s in a):
+ raise InvalidArguments(msg)
def noKwargs(f):
@wraps(f)
@@ -61,6 +60,15 @@ def stringArgs(f):
return f(self, node, args, kwargs)
return wrapped
+def stringifyUserArguments(args):
+ if isinstance(args, list):
+ return '[%s]' % ', '.join([stringifyUserArguments(x) for x in args])
+ elif isinstance(args, int):
+ return str(args)
+ elif isinstance(args, str):
+ return "'%s'" % args
+ raise InvalidArguments('Function accepts only strings, integers, lists and lists thereof.')
+
class InterpreterObject():
def __init__(self):
self.methods = {}
@@ -1096,8 +1104,24 @@ class Interpreter():
@noKwargs
def func_message(self, node, args, kwargs):
- self.validate_arguments(args, 1, [str])
- mlog.log(mlog.bold('Message:'), args[0])
+ # reduce arguments again to avoid flattening posargs
+ (posargs, kwargs) = self.reduce_arguments(node.args)
+ if len(posargs) != 1:
+ raise InvalidArguments('Expected 1 argument, got %d' % len(posargs))
+
+ arg = posargs[0]
+ if isinstance(arg, list):
+ argstr = stringifyUserArguments(arg)
+ elif isinstance(arg, str):
+ argstr = arg
+ elif isinstance(arg, int):
+ argstr = str(arg)
+ else:
+ raise InvalidArguments('Function accepts only strings, integers, lists and lists thereof.')
+
+ mlog.log(mlog.bold('Message:'), argstr)
+ return
+
@noKwargs
def func_error(self, node, args, kwargs):