Skip to content

Commit

Permalink
Added multi assignment. a, b, c = 2;
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMisirian committed Sep 13, 2016
1 parent f92d75d commit 3dfb004
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Hassium/Compiler/CodeGen/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ public void Accept(ListDeclarationNode node)
val.Visit(this);
method.Emit(node.SourceLocation, InstructionType.BuildList, node.InitialValues.Count);
}
public void Accept(MultiAssignmentNode node)
{
foreach (var assign in node.Assignments)
assign.Visit(this);
}
public void Accept(PropertyNode node)
{
module.Attributes.Add(node.Variable, compileProperty(node, method.Parent));
Expand Down Expand Up @@ -775,6 +780,8 @@ private string locateFile(string path, string extension)
Environment.OSVersion.Platform == PlatformID.MacOSX)
? Environment.GetEnvironmentVariable("HOME")
: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
if (homePath == null)
return string.Empty;
string homeFilePath = Path.Combine(homePath, path);
if (File.Exists(homeFilePath))
return homeFilePath;
Expand Down
30 changes: 30 additions & 0 deletions src/Hassium/Compiler/Parser/Ast/MultiAssignmentNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hassium.Compiler.Parser.Ast
{
public class MultiAssignmentNode: AstNode
{
public List<BinaryOperationNode> Assignments { get; private set; }

public MultiAssignmentNode(List<AstNode> left, AstNode value)
{
Assignments = new List<BinaryOperationNode>();
foreach (var ast in left)
Assignments.Add(new BinaryOperationNode(value.SourceLocation, BinaryOperation.Assignment, ast, value));
}

public override void Visit(IVisitor visitor)
{
visitor.Accept(this);
}
public override void VisitChildren(IVisitor visitor)
{
foreach (AstNode child in Children)
child.Visit(visitor);
}
}
}
1 change: 1 addition & 0 deletions src/Hassium/Compiler/Parser/IVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public interface IVisitor
void Accept(LambdaNode node);
void Accept(ListAccessNode node);
void Accept(ListDeclarationNode node);
void Accept(MultiAssignmentNode node);
void Accept(PropertyNode node);
void Accept(RaiseNode node);
void Accept(ReturnNode node);
Expand Down
16 changes: 16 additions & 0 deletions src/Hassium/Compiler/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ private ListDeclarationNode parseListDeclaration()
}
return new ListDeclarationNode(Location, initial);
}
private bool scanCommas = true;
private MultiAssignmentNode parseMultiAssignment()
{
List<AstNode> left = new List<AstNode>();
scanCommas = false;
left.Add(new IdentifierNode(Location, ExpectToken(TokenType.Identifier).Value));
while (AcceptToken(TokenType.Comma))
left.Add(new IdentifierNode(Location, ExpectToken(TokenType.Identifier).Value));
scanCommas = true;
ExpectToken(TokenType.Assignment);
AstNode value = parseExpression();
value.SourceLocation = Location;
return new MultiAssignmentNode(left, value);
}
private FuncParameter parseParameter()
{
string name = ExpectToken(TokenType.Identifier).Value;
Expand Down Expand Up @@ -700,6 +714,8 @@ private AstNode parseTerm()
return new ThreadNode(Location, AcceptToken(TokenType.Identifier, "do"), parseStatement());
else if (MatchToken(TokenType.Identifier) && Tokens[Position + 1].TokenType == TokenType.Identifier)
return parseEnforcedAssignment();
else if (MatchToken(TokenType.Identifier) && Tokens[Position + 1].TokenType == TokenType.Comma && scanCommas)
return parseMultiAssignment();
else if (MatchToken(TokenType.OpenSquare))
return parseListDeclaration();
else if (AcceptToken(TokenType.OpenBracket))
Expand Down
1 change: 1 addition & 0 deletions src/Hassium/Compiler/SemanticAnalysis/SemanticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void Accept(LabelNode node) {}
public void Accept(LambdaNode node) {}
public void Accept(ListAccessNode node) {}
public void Accept(ListDeclarationNode node) {}
public void Accept(MultiAssignmentNode node) {}
public void Accept(PropertyNode node) {}
public void Accept(RaiseNode node) {}
public void Accept(ReturnNode node) {}
Expand Down
1 change: 1 addition & 0 deletions src/Hassium/Hassium.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="Compiler\Parser\Ast\DoWhileNode.cs" />
<Compile Include="Compiler\Parser\Ast\GotoNode.cs" />
<Compile Include="Compiler\Parser\Ast\LabelNode.cs" />
<Compile Include="Compiler\Parser\Ast\MultiAssignmentNode.cs" />
<Compile Include="Compiler\Parser\Ast\UsingNode.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
9 changes: 9 additions & 0 deletions src/Hassium/Runtime/Objects/HassiumFunction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;

using Hassium.Runtime.Objects.Types;

namespace Hassium.Runtime.Objects
{

Expand All @@ -23,6 +25,13 @@ public HassiumFunction(HassiumFunctionDelegate target, int[] paramLengths)
ParameterLengths = paramLengths;
}

public HassiumList get_parameterLengths(VirtualMachine vm, params HassiumObject[] args)
{
HassiumList result = new HassiumList(new HassiumObject[0]);

return result;
}

public override HassiumObject Invoke(VirtualMachine vm, params HassiumObject[] args)
{
if (ParameterLengths[0] != -1)
Expand Down
6 changes: 6 additions & 0 deletions src/Hassium/Runtime/Objects/HassiumMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class HassiumMethod: HassiumObject
public HassiumMethod()
{
AddType(TypeDefinition);
AddAttribute("parameterLengths", new HassiumProperty(get_parameterLengths));

BreakLabels = new Stack<int>();
ContinueLabels = new Stack<int>();
Expand All @@ -34,6 +35,11 @@ public HassiumMethod()
Parameters = new Dictionary<FuncParameter, int>();
}

public HassiumList get_parameterLengths(VirtualMachine vm, params HassiumObject[] args)
{
return new HassiumList(new HassiumObject[] { new HassiumInt(Parameters.Count) });
}

public void Emit(SourceLocation location, InstructionType instructionType, int argument = 0)
{
Instructions.Add(new Instruction(location, instructionType, argument));
Expand Down

0 comments on commit 3dfb004

Please sign in to comment.