aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjpakkane <jpakkane@gmail.com>2015-05-08 00:53:29 +0300
committerjpakkane <jpakkane@gmail.com>2015-05-08 00:53:29 +0300
commitdad5cdbb3c53ce7b036404b5fecbe7b255e6df66 (patch)
tree2ec5203a002b5c07afb66b52a47261b0bb62d4bd
parent5401a98fbf4abc385b9e3b3b0fae62013532501e (diff)
parent6cdfb6a42529cc3309362dd3486de557427521ff (diff)
downloadmeson-dad5cdbb3c53ce7b036404b5fecbe7b255e6df66.zip
meson-dad5cdbb3c53ce7b036404b5fecbe7b255e6df66.tar.gz
meson-dad5cdbb3c53ce7b036404b5fecbe7b255e6df66.tar.bz2
Merge pull request #107 from afiefh/better_message
Make the message() function able to print out user defined types.
-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):