Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Feb 20, 2016
2 parents 52d3419 + 5b7fe6e commit e15fa63
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 14 deletions.
42 changes: 42 additions & 0 deletions Tests/RandomOptions.node
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Randomizing options

These options should be shuffled:

<<shuffleNextOptions>>
-> One
-> Two
-> Three
-> Four

These options should NOT be shuffled:

-> One
-> Two
-> Three
-> Four

These options should NOT be shuffled

-> One: Select this option to see shuffled options.
<<shuffleNextOptions>>
-> One
-> Two
-> Three
-> Four
-> Two: Select this option to do nothing.


These options should NOT be shuffled:

-> One
-> Two
-> Three
-> Four

These options should be shuffled:

[[One|One]]
[[Two|One]]
[[Three|One]]
[[Four|One]]

4 changes: 4 additions & 0 deletions Tests/TestCases/Commands.node
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Testing commands

<<expect_command("flip Harley3 +1")>>
<<flip Harley3 +1>>
47 changes: 42 additions & 5 deletions YarnSpinner/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,19 @@ internal enum ByteCode {
}



internal class Compiler
{


struct CompileFlags {
// should we emit code that turns (VAR_SHUFFLE_OPTIONS) off
// after the next RunOptions bytecode?
public bool DisableShuffleOptionsAfterNextSet;
}

CompileFlags flags;

internal Program program { get; private set; }

internal Compiler ()
Expand Down Expand Up @@ -268,7 +278,16 @@ internal void CompileNode(Parser.Node node) {
Emit (compiledNode, ByteCode.Stop);
} else {
// Otherwise, show the accumulated nodes and then jump to the selected node

Emit (compiledNode, ByteCode.ShowOptions);

if (flags.DisableShuffleOptionsAfterNextSet == true) {
Emit (compiledNode, ByteCode.PushBool, false);
Emit (compiledNode, ByteCode.StoreVariable, VirtualMachine.SpecialVariables.ShuffleOptions);
Emit (compiledNode, ByteCode.Pop);
flags.DisableShuffleOptionsAfterNextSet = false;
}

Emit (compiledNode, ByteCode.RunNode);
}

Expand Down Expand Up @@ -339,12 +358,23 @@ void GenerateCode(Node node, Parser.CustomCommand statement) {
// If this command is an evaluable expression, evaluate it
if (statement.expression != null) {
GenerateCode (node, statement.expression);
} else if (statement.clientCommand == "stop") {
// If it's the special command "stop", emit the Stop bytecode
Emit (node, ByteCode.Stop);
} else {
// Emit the code that passes the command to the client
Emit (node, ByteCode.RunCommand, statement.clientCommand);
switch (statement.clientCommand) {
case "stop":
Emit (node, ByteCode.Stop);
break;
case "shuffleNextOptions":
// Emit code that sets "VAR_SHUFFLE_OPTIONS" to true
Emit (node, ByteCode.PushBool, true);
Emit (node, ByteCode.StoreVariable, VirtualMachine.SpecialVariables.ShuffleOptions);
Emit (node, ByteCode.Pop);
flags.DisableShuffleOptionsAfterNextSet = true;
break;

default:
Emit (node, ByteCode.RunCommand, statement.clientCommand);
break;
}
}

}
Expand Down Expand Up @@ -391,6 +421,13 @@ void GenerateCode(Node node, Parser.ShortcutOptionGroup statement) {

Emit (node, ByteCode.ShowOptions);

if (flags.DisableShuffleOptionsAfterNextSet == true) {
Emit (node, ByteCode.PushBool, false);
Emit (node, ByteCode.StoreVariable, VirtualMachine.SpecialVariables.ShuffleOptions);
Emit (node, ByteCode.Pop);
flags.DisableShuffleOptionsAfterNextSet = false;
}

Emit (node, ByteCode.Jump);

optionCount = 0;
Expand Down
2 changes: 0 additions & 2 deletions YarnSpinner/Dialogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ public int LoadString(string text, bool showTokens=false, bool showParseTree=fal

} while (vm.executionState != VirtualMachine.ExecutionState.Stopped && stopExecuting == false);



}

public void Stop() {
Expand Down
31 changes: 31 additions & 0 deletions YarnSpinner/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ internal class Token {
public int columnNumber;
public string context;

// For <<, we record everything up to the next >> on the line,
// so that the parser can get it back for custom commands
public string associatedRawText;

// If this is a function in an expression, this is the number
// of parameters that were encountered
public int parameterCount;
Expand Down Expand Up @@ -528,6 +532,33 @@ private TokenList TokeniseLine(string context, string input, out int lineIndenta
}
}

// Attach text between << and >> to the << token
for (int i = 0; i < tokensToReturn.Count; i++) {
if (i == tokensToReturn.Count - 1) {
// don't bother checking if we're the last token in the line
continue;
}
var startToken = tokensToReturn[i];
if (startToken.type == TokenType.BeginCommand) {
int startIndex = tokensToReturn[i+1].columnNumber;
int endIndex = -1;
// Find the next >> token
for (int j = i; j < tokensToReturn.Count; j++) {
var endToken = tokensToReturn [j];
if (endToken.type == TokenType.EndCommand) {
endIndex = endToken.columnNumber;
break;
}
}

if (endIndex != -1) {
var text = input.Substring (startIndex, endIndex - startIndex);
startToken.associatedRawText = text;
}

}
}

// Return the list of tokens we found
return tokensToReturn;
}
Expand Down
9 changes: 2 additions & 7 deletions YarnSpinner/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ internal CustomCommand(ParseNode parent, Parser p) : base(parent, p) {

// Custom commands can have ANY token in them. Read them all until we hit the
// end command token.
p.ExpectSymbol(TokenType.BeginCommand);
var beginSymbol = p.ExpectSymbol(TokenType.BeginCommand);
var commandTokens = new List<Token>();
do {
commandTokens.Add(p.ExpectSymbol());
Expand All @@ -434,12 +434,7 @@ internal CustomCommand(ParseNode parent, Parser p) : base(parent, p) {
// Otherwise, evaluate it as a command
type = Type.ClientCommand;

var tokenStrings = new List<string>();
foreach (var token in commandTokens) {
tokenStrings.Add(token.value as string);
}
this.clientCommand = string.Join(" ", tokenStrings.ToArray());

this.clientCommand = beginSymbol.associatedRawText;
}


Expand Down
18 changes: 18 additions & 0 deletions YarnSpinner/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace Yarn
internal class VirtualMachine
{

internal static class SpecialVariables {
public const string ShuffleOptions = "$Yarn.ShuffleOptions";
}

internal VirtualMachine (Dialogue d, Program p)
{
program = p;
Expand Down Expand Up @@ -276,12 +280,26 @@ internal void RunInstruction(Instruction i) {
break;
}

if (dialogue.continuity.GetNumber(SpecialVariables.ShuffleOptions) != 0.0f) {
// Shuffle the dialog options if needed
var r = new Random();
for (int opt1 = state.currentOptions.Count-1; opt1 >= 0; opt1--) {
int opt2 = r.Next(0, state.currentOptions.Count-1);
var temp = state.currentOptions [opt2];
state.currentOptions [opt2] = state.currentOptions [opt1];
state.currentOptions [opt1] = temp;
}
}

// Otherwise, present the list of options to the user and let them pick
var optionStrings = new List<string> ();

foreach (var option in state.currentOptions) {
optionStrings.Add (program.GetString (option.Key));
}



// We can't continue until our client tell us which option to pick
executionState = ExecutionState.WaitingOnOptionSelection;

Expand Down
14 changes: 14 additions & 0 deletions YarnSpinnerConsole/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ public static void Main (string[] args)
impl.expectedNextLine = parameters[0].AsString;
});

dialogue.library.RegisterFunction ("expect_command", 1, delegate(Value[] parameters) {
impl.expectedNextCommand = parameters[0].AsString;
});

// If debugging is enabled, log debug messages; otherwise, ignore them
if (showDebugging) {
dialogue.LogDebugMessage = delegate(string message) {
Expand Down Expand Up @@ -295,6 +299,8 @@ private class ConsoleRunnerImplementation : Yarn.VariableStorage {

public string expectedNextLine = null;

public string expectedNextCommand = null;

public ConsoleRunnerImplementation(bool waitForLines = false) {
this.variableStore = new MemoryVariableStore();
this.waitForLines = waitForLines;
Expand Down Expand Up @@ -373,6 +379,14 @@ public void RunOptions (Options optionsGroup, OptionChooser optionChooser)

public void RunCommand (string command)
{

if (expectedNextCommand != null && expectedNextCommand != command) {
// TODO: Output diagnostic info here
Console.WriteLine(string.Format("Unexpected line.\nExpected: {0}\nReceived: {1}",
expectedNextCommand, command));
Environment.Exit (1);
}

Console.WriteLine("Command: <<"+command+">>");
}

Expand Down
12 changes: 12 additions & 0 deletions YarnSpinnerTests/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public void TestParsingSmileys()
dialogue.LoadFile (path);
dialogue.Compile ();
}

[Test()]
public void TestCommands()
{
var path = System.IO.Path.Combine ("TestCases", "Commands.node");
dialogue.LoadFile (path);
dialogue.Compile ();

foreach (var result in dialogue.Run()) {
Console.WriteLine (result);
}
}
}

}
Expand Down

0 comments on commit e15fa63

Please # to comment.