From a128ea1b6c6d477f3408140bf2e1c3e4d1573b9d Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Sat, 18 Apr 2015 20:40:26 +0700 Subject: [PATCH] typing&engines: implement default arguments Signed-off-by: Jean Privat --- src/compiler/abstract_compiler.nit | 2 ++ src/interpreter/naive_interpreter.nit | 2 ++ src/semantize/typing.nit | 22 ++++++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 1b8bf6a..f8cbee8 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -1173,6 +1173,8 @@ abstract class AbstractCompilerVisitor var param = msignature.mparameters[i] var j = map.map.get_or_null(i) if j == null then + # default value + res.add(null_instance) continue end if param.is_vararg and map.vararg_decl > 0 then diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index c1bd59b..9b821e4 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -402,6 +402,8 @@ class NaiveInterpreter var param = msignature.mparameters[i] var j = map.map.get_or_null(i) if j == null then + # default value + res.add(null_instance) continue end if param.is_vararg and map.vararg_decl > 0 then diff --git a/src/semantize/typing.nit b/src/semantize/typing.nit index 97e200c..e798ab2 100644 --- a/src/semantize/typing.nit +++ b/src/semantize/typing.nit @@ -407,8 +407,18 @@ private class TypeVisitor return null end else if args.length != msignature.arity then - modelbuilder.error(node, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.") - return null + if msignature.arity == msignature.min_arity then + modelbuilder.error(node, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.") + return null + end + if args.length > msignature.arity then + modelbuilder.error(node, "Error: expected at most {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.") + return null + end + if args.length < msignature.min_arity then + modelbuilder.error(node, "Error: expected at least {msignature.min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.") + return null + end end #debug("CALL {unsafe_type}.{msignature}") @@ -416,10 +426,18 @@ private class TypeVisitor # Associate each parameter to a position in the arguments var map = new SignatureMap + var setted = args.length - msignature.min_arity var vararg_decl = args.length - msignature.arity var j = 0 for i in [0..msignature.arity[ do var param = msignature.mparameters[i] + if param.is_default then + if setted > 0 then + setted -= 1 + else + continue + end + end var arg = args[j] map.map[i] = j j += 1 -- 1.7.9.5