From ef49e2d6fda0c3be56990fda9d4bd94b564dc971 Mon Sep 17 00:00:00 2001 From: Jacob Misirian Date: Sat, 27 Aug 2016 00:55:55 -0500 Subject: [PATCH] Added variadic params with params [name] parameter. --- src/Hassium/Compiler/Parser/Ast/FuncNode.cs | 5 ++++- src/Hassium/Compiler/Parser/Parser.cs | 2 ++ src/Hassium/Runtime/Objects/HassiumMethod.cs | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Hassium/Compiler/Parser/Ast/FuncNode.cs b/src/Hassium/Compiler/Parser/Ast/FuncNode.cs index 89395cd..4ce058f 100644 --- a/src/Hassium/Compiler/Parser/Ast/FuncNode.cs +++ b/src/Hassium/Compiler/Parser/Ast/FuncNode.cs @@ -49,18 +49,21 @@ public string GetSourceRepresentation() public class FuncParameter { public bool IsEnforced { get { return Type != string.Empty; } } + public bool IsVariadic { get; private set; } public string Name { get; private set; } public string Type { get; private set; } - public FuncParameter(string name) + public FuncParameter(string name, bool isVariadic = false) { Name = name; Type = string.Empty; + IsVariadic = isVariadic; } public FuncParameter(string name, string type) { Name = name; Type = type; + IsVariadic = false; } public string GetSourceRepresentation() diff --git a/src/Hassium/Compiler/Parser/Parser.cs b/src/Hassium/Compiler/Parser/Parser.cs index 55461d1..7db369f 100644 --- a/src/Hassium/Compiler/Parser/Parser.cs +++ b/src/Hassium/Compiler/Parser/Parser.cs @@ -241,6 +241,8 @@ private ListDeclarationNode parseListDeclaration() private FuncParameter parseParameter() { string name = ExpectToken(TokenType.Identifier).Value; + if (name == "params") + return new FuncParameter(ExpectToken(TokenType.Identifier).Value, true); if (AcceptToken(TokenType.Colon)) return new FuncParameter(name, ExpectToken(TokenType.Identifier).Value); return new FuncParameter(name); diff --git a/src/Hassium/Runtime/Objects/HassiumMethod.cs b/src/Hassium/Runtime/Objects/HassiumMethod.cs index 72f5cc2..3270572 100644 --- a/src/Hassium/Runtime/Objects/HassiumMethod.cs +++ b/src/Hassium/Runtime/Objects/HassiumMethod.cs @@ -5,6 +5,7 @@ using Hassium.Compiler; using Hassium.Compiler.CodeGen; using Hassium.Compiler.Parser.Ast; +using Hassium.Runtime.Objects.Types; namespace Hassium.Runtime.Objects { @@ -64,6 +65,19 @@ public override HassiumObject Invoke(VirtualMachine vm, params HassiumObject[] a foreach (var param in Parameters) { var arg = args[i++]; + if (param.Key.IsVariadic) + { + if (arg is HassiumList) + vm.StackFrame.Add(param.Value, arg); + else + { + HassiumList list = new HassiumList(new HassiumObject[0]); + for (; i < args.Length; i++) + list.add(vm, args[i]); + vm.StackFrame.Add(param.Value, list); + } + break; + } if (param.Key.IsEnforced) if (!arg.Types.Contains((HassiumTypeDefinition)vm.Globals[param.Key.Type].Type())) throw new InternalException(vm, InternalException.PARAMETER_ERROR, param.Key.Type, arg.Type());