Skip to content

Commit

Permalink
Hoisted functions and vars in CompositeCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Jun 21, 2013
1 parent 022269b commit 461cfef
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Src/Mass.Core.Tests/MachineFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public void ExecuteLocalFile()
Assert.AreEqual(1, a);
}

[TestMethod]
public void ExecuteHoistedLocalFile()
{
var result = this.machine.ExecuteFile("MachineFiles\\HoistedLocal.ms", this.machine.RootContext);

var a = this.machine.RootContext.Get("a");

Assert.IsNotNull(result);
Assert.AreEqual(2, result);
Assert.IsNotNull(a);
Assert.AreEqual(1, a);
}

[TestMethod]
public void ExecuteGlobalFile()
{
Expand Down
14 changes: 14 additions & 0 deletions Src/Mass.Core.Tests/MachineFiles/HoistedLocal.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# file variable
var a
a = 1

result = foo() # invoking the function before reach definition

define foo()
a = 2
var a # local variable
end

result

1 change: 0 additions & 1 deletion Src/Mass.Core.Tests/MachineFiles/Local.ms
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


# file variable
var a
a = 1
Expand Down
5 changes: 5 additions & 0 deletions Src/Mass.Core.Tests/Mass.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="MachineFiles\HoistedLocal.ms">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
30 changes: 28 additions & 2 deletions Src/Mass.Core/Commands/CompositeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,43 @@

public class CompositeCommand : ICommand
{
private IList<ICommand> commands;
private IList<ICommand> commands = new List<ICommand>();
private IList<string> varnames = new List<string>();
private IList<ICommand> defines = new List<ICommand>();

public CompositeCommand(IList<ICommand> commands)
{
this.commands = commands;
if (commands == null)
return;

foreach (var command in commands)
{
if (command is DefineCommand)
{
defines.Add(command);
continue;
}

if (command is VarCommand)
{
varnames.Add(((VarCommand)command).Name);
continue;
}

this.commands.Add(command);
}
}

public object Execute(Context context)
{
object result = null;

foreach (var varname in this.varnames)
context.Set(varname, null, true);

foreach (var define in this.defines)
result = define.Execute(context);

foreach (var command in this.commands)
{
result = command.Execute(context);
Expand Down

0 comments on commit 461cfef

Please sign in to comment.