Skip to content

Commit

Permalink
Update for Yarn Spinner v3 features
Browse files Browse the repository at this point in the history
+semver: major
  • Loading branch information
desplesda committed Dec 16, 2024
1 parent b6219fa commit ddbb86d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 121 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Added

- Added a new command, `dump-code`, which compiles Yarn files or a Yarn Project and outputs a human-readable version of the compiled bytecode to stdout. This is intended for developers who are working on tools that consume compiled Yarn files.

### Changed

- The `--allow-preview-features` (`-p`) flag can now be used when compiling scripts to enable preview features.
- .ysls.json files referenced by Yarn Project files are now parsed and used when compiling Yarn code.

### Removed

## [2.5.0] 2024-12-16
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ Lists all of the yarn files that are associated with the `input.yarnproject` Yar
This reads both the includes and excludes of the Yarn Project and will work out what files match those filters.
You can use this to make sure you have set your globstar values correctly.
## Upgrading Scripts
```bash
$ ysc upgrade [--upgrade-type <1>] <input1.yarn> <input2.yarn> ...
```
`ysc` will upgrade the `yarn` files from one version of Yarn to another.
By specifying the `--upgrade-type` option you can configure from what version, to which version, of Yarn to convert.
Defaults to `1`, or upgrading a yarn v1 file to a yarn v2 file.
## Printing the Syntax Tree
```bash
Expand Down
17 changes: 10 additions & 7 deletions src/YarnSpinner.Console/Commands/CompileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void CompileFiles(FileInfo[] inputs, DirectoryInfo outputDirectory
}
}

public static CompilationJob GetCompilationJob(FileInfo[] inputs)
public static CompilationJob GetCompilationJob(FileInfo[] inputs, int languageVersion = Yarn.Compiler.Project.CurrentProjectFileVersion)
{
var anyFileIsProject = inputs.Any(i => i.Extension == ".yarnproject");

Expand All @@ -137,10 +137,10 @@ public static CompilationJob GetCompilationJob(FileInfo[] inputs)
{
static Yarn.IType GetType(string typeName) => typeName.ToLowerInvariant() switch
{
"string" => Yarn.BuiltinTypes.String,
"any" => Yarn.BuiltinTypes.Any,
"number" => Yarn.BuiltinTypes.Number,
"bool" => Yarn.BuiltinTypes.Boolean,
"string" => Yarn.Types.String,
"any" => Yarn.Types.Any,
"number" => Yarn.Types.Number,
"bool" => Yarn.Types.Boolean,

var other => throw new System.ArgumentOutOfRangeException("Invalid type " + other),
};
Expand Down Expand Up @@ -172,6 +172,7 @@ public static CompilationJob GetCompilationJob(FileInfo[] inputs)
else
{
var job = CompilationJob.CreateFromFiles(inputs.Select(i => i.FullName));
job.LanguageVersion = languageVersion;
return job;
}
}
Expand All @@ -183,14 +184,16 @@ private static void EmitCompilationResult(CompilationResult compiledResults, Tex
var compilerOutput = new Yarn.CompilerOutput();
compilerOutput.Program = program;

foreach (var entry in compiledResults.StringTable) {
foreach (var entry in compiledResults.StringTable)
{
var tableEntry = new Yarn.StringInfo();
tableEntry.Text = entry.Value.text;

compilerOutput.Strings.Add(entry.Key, tableEntry);
}

foreach (var diagnostic in compiledResults.Diagnostics) {
foreach (var diagnostic in compiledResults.Diagnostics)
{
var diag = new Yarn.Diagnostic();
diag.Message = diagnostic.Message;
diag.FileName = diagnostic.FileName;
Expand Down
35 changes: 35 additions & 0 deletions src/YarnSpinner.Console/Commands/DumpCompiledCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace YarnSpinnerConsole
{
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using Yarn.Compiler;

public static class DumpCompiledCodeCommand
{
public static void DumpCompiledCode(FileInfo[] inputs, bool allowPreviewFeatures)
{
if (inputs == null)
{
throw new ArgumentNullException(nameof(inputs));
}

var compiledResults = YarnSpinnerConsole.CompileProgram(inputs);

System.Func<string, string> stringLookupHelper = (input) =>
{
if (compiledResults.StringTable.TryGetValue(input, out var result))
{
return result.text;
}
else
{
return null;
}
};

Console.WriteLine(Yarn.Compiler.Utility.GetCompiledCodeAsString(compiledResults.Program, null, compiledResults));
}
}
}
2 changes: 1 addition & 1 deletion src/YarnSpinner.Console/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace YarnSpinnerConsole

public static class RunCommand
{
public static void RunFiles(FileInfo[] inputs, string startNode, bool autoAdvance)
public static void RunFiles(FileInfo[] inputs, string startNode, bool autoAdvance, bool allowPreviewFeatures)
{
// this will be a new interactive command for running yarn
// stories will compile and then run them
Expand Down
8 changes: 4 additions & 4 deletions src/YarnSpinner.Console/Commands/StringExtractCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void ExtractStrings(FileInfo[] inputs, string format, string[] col
location = output.FullName;
}

var lineBlocks = Yarn.Compiler.Utility.ExtractStringBlocks(compiledResults.Program.Nodes.Values);
var lineBlocks = Yarn.Compiler.Utility.ExtractStringBlocks(compiledResults.Program.Nodes.Values, compiledResults.ProjectDebugInfo);
var stringTable = compiledResults.StringTable;

bool includeCharacters = true;
Expand Down Expand Up @@ -143,7 +143,7 @@ public interface StringWriter
public void Format(HashSet<string> characters);
public void WriteFile(string location);
}
public class CSVStringWriter: StringWriter
public class CSVStringWriter : StringWriter

Check warning on line 146 in src/YarnSpinner.Console/Commands/StringExtractCommand.cs

View workflow job for this annotation

GitHub Actions / build

{
private string[] columns;
private StreamWriter stream;
Expand Down Expand Up @@ -191,7 +191,7 @@ public void WriteFile(string location)
}
}

public class ExcelStringWriter: StringWriter
public class ExcelStringWriter : StringWriter
{
private int rowIndex = 1;
private int columnIndex = 1;
Expand All @@ -212,7 +212,7 @@ public ExcelStringWriter(string[] columns)
sheet.Cell(rowIndex, j + 1).Value = columns[j];
}

sheet.Row(rowIndex).Style.Font.Bold = true;
sheet.Row(rowIndex).Style.Font.Bold = true;
sheet.Row(rowIndex).Style.Fill.BackgroundColor = XLColor.DarkGray;
sheet.Row(rowIndex).Style.Font.FontColor = XLColor.White;
sheet.SheetView.FreezeRows(1);
Expand Down
70 changes: 0 additions & 70 deletions src/YarnSpinner.Console/Commands/UpgradeCommand.cs

This file was deleted.

55 changes: 30 additions & 25 deletions src/YarnSpinner.Console/YarnSpinnerConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public static int Main(string[] args)

var stdoutOption = new Option<bool>("--stdout", "Output machine-readable compilation result to stdout instead of to files");
compileCommand.AddOption(stdoutOption);

var allowPreviewFeaturesOption = new Option<bool>("-p", "Allow using in-development compiler features.");
allowPreviewFeaturesOption.AddAlias("--allow-preview-features");
allowPreviewFeaturesOption.Argument.SetDefaultValue(false);
compileCommand.AddOption(allowPreviewFeaturesOption);
}

compileCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<FileInfo[], DirectoryInfo, string, string, string, bool>(CompileCommand.CompileFiles);
Expand Down Expand Up @@ -89,28 +94,14 @@ public static int Main(string[] args)
var autoAdvance = new Option<bool>("--auto-advance", "Auto-advance regular dialogue lines");
autoAdvance.AddAlias("-a");
runCommand.AddOption(autoAdvance);
}

runCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<FileInfo[], string, bool>(RunCommand.RunFiles);

var upgradeCommand = new System.CommandLine.Command("upgrade", "Upgrades Yarn scripts from one version of the language to another. Files will be modified in-place.");
{
Argument<FileInfo[]> inputsArgument = new Argument<FileInfo[]>("inputs", "The .yarnproject file to upgrade the files of, or a collection of .yarn files to upgrade.")
{
Arity = ArgumentArity.OneOrMore,
};

upgradeCommand.AddArgument(inputsArgument.ExistingOnly());

var upgradeTypeOption = new Option<int>("-t", "Upgrade type");
upgradeTypeOption.AddAlias("--upgrade-type");
upgradeTypeOption.Argument.SetDefaultValue(1);
upgradeTypeOption.FromAmong("1");
upgradeTypeOption.Argument.Arity = ArgumentArity.ExactlyOne;
upgradeCommand.AddOption(upgradeTypeOption);
var allowPreviewFeaturesOption = new Option<bool>("-p", "Allow using in-development compiler features.");
allowPreviewFeaturesOption.AddAlias("--allow-preview-features");
allowPreviewFeaturesOption.Argument.SetDefaultValue(false);
runCommand.AddOption(allowPreviewFeaturesOption);
}

upgradeCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<FileInfo[], Yarn.Compiler.Upgrader.UpgradeType>(UpgradeCommand.UpgradeFiles);
runCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<FileInfo[], string, bool, bool>(RunCommand.RunFiles);

var dumpTreeCommand = new System.CommandLine.Command("print-tree", "Parses a Yarn script and produces a human-readable syntax tree.");
{
Expand Down Expand Up @@ -246,21 +237,35 @@ public static int Main(string[] args)
}
createProjectFileCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<string, bool>(CreateProjectFileCommand.CreateProjFile);

var dumpCodeCommand = new Command("dump-code", "Compiles the specified input, and prints the disassembled code.");
{
Argument<FileInfo[]> inputsArgument = new Argument<FileInfo[]>("inputs", "The .yarnproject file to compile, or a collection of .yarn files to compile.");
inputsArgument.Arity = ArgumentArity.OneOrMore;
dumpCodeCommand.AddArgument(inputsArgument.ExistingOnly());

var allowPreviewFeaturesOption = new Option<bool>("-p", "Allow using in-development compiler features.");
allowPreviewFeaturesOption.AddAlias("--allow-preview-features");
allowPreviewFeaturesOption.Argument.SetDefaultValue(false);
dumpCodeCommand.AddOption(allowPreviewFeaturesOption);
}
dumpCodeCommand.Handler = System.CommandLine.Invocation.CommandHandler.Create<FileInfo[], bool>(DumpCompiledCodeCommand.DumpCompiledCode);

// Create a root command with our subcommands
var rootCommand = new RootCommand
{
runCommand,
compileCommand,
listSourcesCommand,
upgradeCommand,
dumpTreeCommand,
dumpTokensCommand,
dumpCodeCommand,
tagCommand,
extractCommand,
graphCommand,
browsebinaryCommand,
createProjectFileCommand,
versionCommand, };
versionCommand,
};

rootCommand.Description = "Compiles, runs and analyses Yarn code.";

Expand All @@ -285,17 +290,17 @@ public static CompilationResult CompileProgram(FileInfo[] inputs)
.WithName("visited")
.WithType(
new FunctionTypeBuilder()
.WithParameter(Yarn.BuiltinTypes.String)
.WithReturnType(Yarn.BuiltinTypes.Boolean)
.WithParameter(Yarn.Types.String)
.WithReturnType(Yarn.Types.Boolean)
.FunctionType)
.Declaration;

var visitedCountDecl = new DeclarationBuilder()
.WithName("visited_count")
.WithType(
new FunctionTypeBuilder()
.WithParameter(Yarn.BuiltinTypes.String)
.WithReturnType(Yarn.BuiltinTypes.Number)
.WithParameter(Yarn.Types.String)
.WithReturnType(Yarn.Types.Number)
.FunctionType)
.Declaration;

Expand Down
9 changes: 5 additions & 4 deletions src/YarnSpinner.Console/ysc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackAsTool>true</PackAsTool>
<ToolCommandName>ysc</ToolCommandName>
<PackageOutputPath>../../nupkg</PackageOutputPath>
<!-- <YarnSpinnerDevelopmentPath>../../../YarnSpinner</YarnSpinnerDevelopmentPath> -->
</PropertyGroup>

<ItemGroup>
Expand All @@ -24,10 +25,10 @@

<!-- If Yarn Spinner doesn't exist at YarnSpinnerDevelopmentPath, then pull
the most recent version from NuGet. -->
<ItemGroup Condition="!Exists('$(YarnSpinnerDevelopmentPath)')">
<PackageReference Include="YarnSpinner" Version="2.5.0" />
<PackageReference Include="YarnSpinner.Compiler" Version="2.5.0" />
</ItemGroup>
<ItemGroup Condition="!Exists('$(YarnSpinnerDevelopmentPath)')">
<PackageReference Include="YarnSpinner" Version="3.0.0-beta.1" />
<PackageReference Include="YarnSpinner.Compiler" Version="3.0.0-beta.1" />
</ItemGroup>

<!-- If Yarn Spinner DOES exist at YarnSpinnerDevelopmentPath, use that. -->
<ItemGroup Condition="Exists('$(YarnSpinnerDevelopmentPath)')">
Expand Down

0 comments on commit ddbb86d

Please sign in to comment.