Skip to content

Commit

Permalink
Merge pull request inkle#642 from chromy/master
Browse files Browse the repository at this point in the history
Support playing .json files with inklecate
  • Loading branch information
joethephish authored Feb 21, 2021
2 parents 410c49f + 7dbc21b commit 60402fb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 58 deletions.
100 changes: 44 additions & 56 deletions compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,19 @@ public class CommandLineInputResult {
public string divertedPath;
public string output;
}
public CommandLineInputResult ReadCommandLineInput (string userInput)
public CommandLineInputResult HandleInput (CommandLineInput inputResult)
{
var inputParser = new InkParser (userInput);
var inputResult = inputParser.CommandLineUserInput ();

var result = new CommandLineInputResult ();

// Choice
if (inputResult.choiceInput != null) {
result.choiceIdx = ((int)inputResult.choiceInput) - 1;
}

// Help
else if (inputResult.isHelp) {
result.output = "Type a choice number, a divert (e.g. '-> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')";
}

// Quit
else if (inputResult.isExit) {
result.requestsExit = true;
}

// Request for debug source line number
else if (inputResult.debugSource != null) {
if (inputResult.debugSource != null) {
var offset = (int)inputResult.debugSource;
var dm = DebugMetadataForContentAtOffset (offset);
if (dm != null)
result.output = "DebugSource: " + dm.ToString ();
else
result.output = "DebugSource: Unknown source";
}
}

// Request for runtime path lookup (to line number)
else if (inputResult.debugPathLookup != null) {
Expand All @@ -109,49 +91,55 @@ public CommandLineInputResult ReadCommandLineInput (string userInput)

// User entered some ink
else if (inputResult.userImmediateModeStatement != null) {

var parsedObj = inputResult.userImmediateModeStatement as Parsed.Object;

// Variable assignment: create in Parsed.Story as well as the Runtime.Story
// so that we don't get an error message during reference resolution
if (parsedObj is Parsed.VariableAssignment) {
var varAssign = (Parsed.VariableAssignment)parsedObj;
if (varAssign.isNewTemporaryDeclaration) {
_parsedStory.TryAddNewVariableDeclaration (varAssign);
}
}

parsedObj.parent = _parsedStory;
var runtimeObj = parsedObj.runtimeObject;

parsedObj.ResolveReferences (_parsedStory);

if (!_parsedStory.hadError) {

// Divert
if (parsedObj is Parsed.Divert) {
var parsedDivert = parsedObj as Parsed.Divert;
result.divertedPath = parsedDivert.runtimeDivert.targetPath.ToString();
}

// Expression or variable assignment
else if (parsedObj is Parsed.Expression || parsedObj is Parsed.VariableAssignment) {
var evalResult = _runtimeStory.EvaluateExpression ((Runtime.Container)runtimeObj);
if (evalResult != null) {
result.output = evalResult.ToString ();
}
}
} else {
_parsedStory.ResetError ();
}
return ExecuteImmediateStatement(parsedObj);

} else {
result.output = "Unexpected input. Type 'help' or a choice number.";
return null;
}

return result;
}

CommandLineInputResult ExecuteImmediateStatement(Parsed.Object parsedObj) {
var result = new CommandLineInputResult ();

// Variable assignment: create in Parsed.Story as well as the Runtime.Story
// so that we don't get an error message during reference resolution
if (parsedObj is Parsed.VariableAssignment) {
var varAssign = (Parsed.VariableAssignment)parsedObj;
if (varAssign.isNewTemporaryDeclaration) {
_parsedStory.TryAddNewVariableDeclaration (varAssign);
}
}

parsedObj.parent = _parsedStory;
var runtimeObj = parsedObj.runtimeObject;

parsedObj.ResolveReferences (_parsedStory);

if (!_parsedStory.hadError) {

// Divert
if (parsedObj is Parsed.Divert) {
var parsedDivert = parsedObj as Parsed.Divert;
result.divertedPath = parsedDivert.runtimeDivert.targetPath.ToString();
}

// Expression or variable assignment
else if (parsedObj is Parsed.Expression || parsedObj is Parsed.VariableAssignment) {
var evalResult = _runtimeStory.EvaluateExpression ((Runtime.Container)runtimeObj);
if (evalResult != null) {
result.output = evalResult.ToString ();
}
}
} else {
_parsedStory.ResetError ();
}

return result;
}

public void RetrieveDebugSourceForLatestContent ()
{
foreach (var outputObj in _runtimeStory.state.outputStream) {
Expand Down
41 changes: 39 additions & 2 deletions inklecate/CommandLinePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void Begin()
return;
}

var result = _compiler.ReadCommandLineInput (userInput);
var result = ReadCommandLineInput (userInput);

if (result.output != null) {
if( _jsonOutput ) {
Expand Down Expand Up @@ -151,7 +151,8 @@ void EvaluateStory ()

story.Continue ();

_compiler.RetrieveDebugSourceForLatestContent ();
if (_compiler != null)
_compiler.RetrieveDebugSourceForLatestContent ();

if( _jsonOutput ) {
var writer = new Runtime.SimpleJson.Writer();
Expand Down Expand Up @@ -239,6 +240,42 @@ void OnStoryError(string msg, ErrorType type)
_warnings.Add(msg);
}

Compiler.CommandLineInputResult ReadCommandLineInput (string userInput) {
var inputParser = new InkParser (userInput);
var inputResult = inputParser.CommandLineUserInput ();
var result = new Compiler.CommandLineInputResult ();

// Choice
if (inputResult.choiceInput != null) {
result.choiceIdx = ((int)inputResult.choiceInput) - 1;
return result;
}

// Help
if (inputResult.isHelp) {
result.output = "Type a choice number, a divert (e.g. '-> myKnot'), an expression, or a variable assignment (e.g. 'x = 5')";
return result;
}

// Quit
if (inputResult.isExit) {
result.requestsExit = true;
return result;
}

// If the compiler is available give it a chance to handle
// the input.
if (_compiler != null) {
var compilerResult = _compiler.HandleInput(inputResult);
if (compilerResult != null) {
return compilerResult;
}
}

result.output = "Unexpected input. Type 'help' or a choice number.";
return result;
}

Compiler _compiler;
bool _jsonOutput;
List<string> _errors = new List<string>();
Expand Down

0 comments on commit 60402fb

Please sign in to comment.