typing&engines: implement default arguments
authorJean Privat <jean@pryen.org>
Sat, 18 Apr 2015 13:40:26 +0000 (20:40 +0700)
committerJean Privat <jean@pryen.org>
Sat, 18 Apr 2015 16:05:04 +0000 (23:05 +0700)
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/semantize/typing.nit

index 1b8bf6a..f8cbee8 100644 (file)
@@ -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
index c1bd59b..9b821e4 100644 (file)
@@ -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
index 97e200c..e798ab2 100644 (file)
@@ -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