Skip to content

Commit

Permalink
Added variadic params with params [name] parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMisirian committed Aug 27, 2016
1 parent 8e9a843 commit ef49e2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Hassium/Compiler/Parser/Ast/FuncNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions src/Hassium/Compiler/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/Hassium/Runtime/Objects/HassiumMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit ef49e2d

Please sign in to comment.