# YesNt Library API The `YesNt.Interpreter` project is a .NET 10 class library. You can reference it from any C# project to embed the YesNt interpreter and run scripts programmatically. --- ## Table of contents 1. [Adding the reference](#adding-the-reference) 2. [Running a script file](#running-a-script-file) 3. [Running an in-memory script](#running-an-in-memory-script) 4. [Stepwise and time-budgeted execution](#stepwise-and-time-budgeted-execution) 5. [Capturing output (debug mode)](#capturing-output-debug-mode) 6. [Giving the script a console and files](#giving-the-script-a-console-and-files) 7. [Adding custom statements](#adding-custom-statements) 8. [Removing and disabling built-in statements](#removing-and-disabling-built-in-statements) 9. [Tasks](#tasks) 10. [Stopping a script](#stopping-a-script) 11. [Reading registered statements](#reading-registered-statements) 12. [API reference](#api-reference) --- ## Adding the reference ### Via NuGet (recommended) Install the [`YesNt.Interpreter`](https://www.nuget.org/packages/YesNt.Interpreter/) package using one of the following methods. **.NET CLI:** ``` dotnet add package YesNt.Interpreter ``` **Package Manager Console:** ``` Install-Package YesNt.Interpreter ``` **`.csproj`:** ```xml ``` ### Via project reference If you have cloned the repository and want to reference the source directly, add a project reference in your `.csproj`: ```xml ``` Then add the using directive: ```csharp using YesNt.Interpreter.Runtime; ``` --- ## Running a script file ```csharp var interpreter = new YesNtInterpreter(); interpreter.Execute("path/to/script.ynt"); ``` `Execute` is synchronous - it returns when the script finishes (or terminates with an error). --- ## Running an in-memory script Supply a `List` instead of a file path: ```csharp var lines = new List { "var x = 42", "print_line The answer is ${x}", }; var interpreter = new YesNtInterpreter(); interpreter.Execute(lines); ``` --- ## Stepwise and time-budgeted execution For interactive environments, game loops, or time-sliced applications, you can prepare a script and run it incrementally. ### Preparing the interpreter Call `Prepare` instead of `Execute` to load the script without running it immediately: ```csharp var interpreter = new YesNtInterpreter(); interpreter.Prepare("path/to/script.ynt"); // or: interpreter.Prepare(lines); ``` ### Driving execution Use `IsRunning` to check if there are more lines to execute, and step by line count or run with a time budget: ```csharp // Execute 5 lines of the script StepResult result = interpreter.Step(5); if (result == StepResult.Paused) { // The line budget was exhausted; resume execution later } ``` Or run the interpreter with a wall-clock time limit (useful for preventing freezing in game loops): ```csharp // Run for up to 10 milliseconds StepResult result = interpreter.RunFor(TimeSpan.FromMilliseconds(10)); ``` You can also run the remaining script to completion: ```csharp interpreter.RunToCompletion(); ``` ### Waiting A script that waits does not block `Step` or `RunFor`: a `sleep`, a `%read_line` or `%read_key` before the input is there, or a custom statement that calls `WaitUntil`. They return `StepResult.Waiting` at once, and the script goes on when it is stepped after the wait is over. Step it again later (at the next frame, or after a short sleep) rather than at once in a tight loop: ```csharp while (interpreter.IsRunning) { if (interpreter.RunFor(TimeSpan.FromMilliseconds(5)) == StepResult.Waiting) { // Nothing to run yet: do other work, or wait for the next frame. } } ``` `RunToCompletion` and `Execute` sleep on the calling thread while the script waits, so a `sleep` in a script run with `Execute` behaves as it always did. --- ## Capturing output (debug mode) Pass `isDebugMode: true` to suppress direct console writes. Output is delivered through the `OnDebugOutput` event instead, and `OnLineExecuted` fires after executed lines. ```csharp var interpreter = new YesNtInterpreter(); var output = new System.Text.StringBuilder(); interpreter.OnDebugOutput += text => output.Append(text); interpreter.OnLineExecuted += args => { if (args is null) { // null means execution reached end-of-file (EOF) Console.WriteLine("Script reached EOF."); return; } Console.WriteLine($"Line {args.LineNumber}: {args.CurrentLine}"); }; interpreter.Execute(new List { "print_line hello" }, isDebugMode: true); Console.Write(output); ``` `OnLineExecuted` receives a `DebugEventArgs` with: | Property | Type | Description | | -------------- | -------- | ------------------------------------------- | | `LineNumber` | `int` | 1-based line number | | `OriginalLine` | `string` | Raw source text | | `CurrentLine` | `string` | Text after all substitutions | | `IsTask` | `bool` | `true` if executed inside a background task | | `TaskId` | `int` | Task identifier (0 for the main thread) | `OnLineExecuted` is invoked with `null` only when execution reaches end-of-file (EOF). If execution stops via `exit`, `throw`, `error`, or `Stop()`, no terminal `null` event is emitted. To show output somewhere else without debug mode, give the interpreter a host instead (next section). --- ## Giving the script a console and files Outside debug mode a script's console and files are its **host**, an `IYesNtHost`. The host decides: - where `print` and `print_line` write; - where `%read_line` and `%read_key` read from; - what `clear` clears; - how script files are read: the script given to `Execute`/`Prepare`, and every file it `import`s. The default, `ConsoleHost`, uses the process's console and file system. Set `Host` before the script starts; tasks the script starts share it. Derive from `ConsoleHost` to change part of it, for example to check that a script may read a file before it does: ```csharp sealed class SandboxedHost(string root) : ConsoleHost { public override string[] ReadScript(string path) { if (!Path.GetFullPath(path).StartsWith(root, StringComparison.OrdinalIgnoreCase)) throw new UnauthorizedAccessException(path); // import ends with "Could not load file" return base.ReadScript(path); } } var interpreter = new YesNtInterpreter { Host = new SandboxedHost(@"C:\scripts\") }; ``` Or implement `IYesNtHost` to put the script in a window. Input need not be there yet. Return `InputStatus.Pending` and the script waits without blocking (see [Waiting](#waiting)), then asks again when it is next stepped. `InputStatus.Closed` ends the script. ```csharp sealed class WindowHost(TextBox output, ConcurrentQueue typedLines) : IYesNtHost { public void Write(string text) => output.AppendText(text); public InputStatus TryReadLine(out string line) { return typedLines.TryDequeue(out line) ? InputStatus.Ready : InputStatus.Pending; } public InputStatus TryReadKey(out char key) { key = default; if (!typedLines.TryDequeue(out string line)) return InputStatus.Pending; key = line.Length > 0 ? line[0] : ' '; return InputStatus.Ready; } public void Clear() => output.Clear(); public string[] ReadScript(string path) => File.ReadAllLines(path); } ``` `ReadScript` throws: - `FileNotFoundException` for a file that is not there. A missing script does not start; a missing import ends the script with "Could not find file". - any other `IOException`, or `UnauthorizedAccessException`, for a file that cannot be read. The script ends with "Could not load file". --- ## Adding custom statements Use `AddStatement` to register keywords before calling `Execute`. ### Simple keyword at the start of a line ```csharp using YesNt.Interpreter.Enums; var interpreter = new YesNtInterpreter(); interpreter.AddStatement("log", SearchMode.StartOfLine, SpaceAround.End, args => { Console.WriteLine($"[LOG] {args}"); }); interpreter.Execute(new List { "log Hello from custom statement" }); ``` ### Accessing script state from a handler Pass an `Action` instead of `Action` to receive the current script state. `IStatementContext` exposes the variable tables, the current line, the line number, the output stack, waiting, and the ability to terminate execution. ```csharp using YesNt.Interpreter.Runtime; interpreter.AddStatement("set_var", SearchMode.StartOfLine, SpaceAround.End, (args, ctx) => { // args is e.g. "result 42" — parse however your syntax demands string[] parts = args.Split(' ', 2); if (parts.Length == 2) ctx.Variables[parts[0]] = parts[1]; else ctx.Exit("set_var requires: ", stopAllTasks: true); }); ``` `IStatementContext` provides: | Property | Type | Description | | ----------------------------- | --------------------------- | -------------------------------------------------------------------------- | | `Variables` | `Dictionary` | Local variable table for the current scope | | `GlobalVariables` | `Dictionary` | Global variable table shared across all scopes | | `CurrentLine` | `string` | The line text being processed; write here for inline-substitution handlers | | `LineNumber` | `int` | Zero-based index of the _next_ line to execute; set to implement jumps | | `SetOut(params values)` | `void` | Replace the output stack with `values`, the last on top, for `%out` | | `WaitUntil(ready)` | `void` | Hold the script before its next line until `ready()` is true | | `RetryLine(ready = null)` | `void` | Run the current line again once `ready()` is true (or at the next step) | | `Exit(message, stopAllTasks)` | `void` | Terminate execution; `stopAllTasks: true` also ends all tasks | ### Returning a value A statement that produces a value gives it to the script the way the built-in `length` and `list get` do: through the output stack, which the script reads with `%out`. `SetOut` empties the stack, then pushes the values in order, so the last is read first. Values are plain text: spaces and keywords in them stay part of the value. ```csharp interpreter.AddStatement("user_name", SearchMode.Exact, SpaceAround.None, (_, ctx) => ctx.SetOut(Environment.UserName)); ``` ```ynt user_name var name = %out print_line Hello ${name} ``` ### Waiting without blocking A statement that takes time should not block, since the host may be stepping the script from a game loop. `WaitUntil` holds the script before its next line until a condition is met. Meanwhile `Step` and `RunFor` return `StepResult.Waiting`. The condition is asked each time the script is stepped, on the stepping thread. ```csharp interpreter.AddStatement("wait_for_click", SearchMode.Exact, SpaceAround.None, (_, ctx) => { int clicksBefore = button.Clicks; ctx.WaitUntil(() => button.Clicks != clicksBefore); }); ``` Sometimes a line needs something that is not there yet, such as input. `RetryLine` runs the line again once the condition is met (or at the next step, if there is no condition), starting from `CurrentLine` as it is now: - Put what is already known into `CurrentLine` first. - Other statements do not run on the line until it is complete. This is how `%read_line` waits when a host returns `InputStatus.Pending`. ### With a syntax-highlight colour ```csharp interpreter.AddStatement("log", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Cyan, args => Console.WriteLine($"[LOG] {args}")); ``` ### Using a `StatementInformation` ```csharp using YesNt.Interpreter.Runtime; var attr = new StatementInformation("log", SearchMode.StartOfLine, SpaceAround.End) { Priority = Priority.VeryLow, }; interpreter.AddStatement(attr, args => Console.WriteLine($"[LOG] {args}")); ``` ### `SearchMode` values | Value | The keyword matches when… | | ------------- | -------------------------------------------- | | `StartOfLine` | the line **starts with** the keyword | | `EndOfLine` | the line **ends with** the keyword | | `Contains` | the keyword appears **anywhere** in the line | | `Exact` | the line is **exactly** the keyword | ### `SpaceAround` values | Value | Space requirement | | ---------- | -------------------------------- | | `None` | No surrounding spaces required | | `Start` | A space must precede the keyword | | `End` | A space must follow the keyword | | `StartEnd` | Spaces required on both sides | Custom statements run at `Priority.Normal` by default. Statements with a higher-ranking enum member (`PreProcessing` → `Highest` → … → `VeryLow`) run first; `VeryLow` runs last. Use `StatementInformation.Priority` to control ordering relative to built-in statements. --- ## Removing and disabling built-in statements Use these methods to restrict which built-in keywords are available, useful for sandboxing or replacing a built-in with a custom implementation. ### `RemoveStatement` - permanent removal Removes all handlers for the given keyword. Any script line that would have matched the keyword now triggers an **"Invalid statement"** error. ```csharp var interpreter = new YesNtInterpreter(); // Prevent scripts from launching external processes. interpreter.RemoveStatement("exec"); interpreter.Execute(new List { "exec notepad" }); // Terminates with: Invalid statement ``` ### `DisableStatement` - silent no-op Disables all handlers for the keyword. The keyword still **matches** (so no error is raised), but has no effect. Use `EnableStatement` to restore the original behaviour. ```csharp var interpreter = new YesNtInterpreter(); // Make sleep a no-op so tests don't actually wait. interpreter.DisableStatement("sleep"); interpreter.Execute(new List { "sleep 10000", // does nothing "var x = done", "print_line ${x}", // prints: done }); ``` ### `EnableStatement` - restore a disabled statement Restores the original handlers saved when `DisableStatement` was called. Has no effect if the statement is not currently disabled. Tasks the script starts (`task`) get every statement added, removed, disabled and enabled again, so removing `exec` keeps it out of tasks too. ```csharp interpreter.DisableStatement("sleep"); // ... configure other things ... interpreter.EnableStatement("sleep"); // sleep works normally again ``` ### Replacing a built-in statement Call `RemoveStatement` to remove the built-in handlers, then `AddStatement` to install your own. Simply calling `AddStatement` with the same keyword name will **not** replace the built-in statement, it will add a second handler that fires alongside the original. ```csharp // Replace the built-in 'exec' with a sandboxed version that only allows 'echo'. interpreter.RemoveStatement("exec"); interpreter.AddStatement("exec", SearchMode.StartOfLine, SpaceAround.End, args => { if (args.Trim() != "echo") throw new InvalidOperationException("exec is restricted"); System.Diagnostics.Process.Start("cmd", "/c echo (sandboxed)"); }); ``` --- ## Tasks A line ending in `task` runs in a task alongside the script (see the language reference), on a thread of its own. A task's interpreter gets these from the script's: - its statements: the built-in ones as the host left them, and the custom ones, whose `IStatementContext` is the task's own; - its host; - the functions the script has already passed, so a task can call a function defined above it. It also shares the script's global variables. A task goes on after the script ends. --- ## Stopping a script Call `Stop()` from any thread to request graceful termination. The script stops at the next line boundary. A script that waits (a `sleep`, or input a host has not got yet) stops at once. `ConsoleHost` reads a line by waiting for it, so a script reading from the console stops once the line is entered. ```csharp var interpreter = new YesNtInterpreter(); // Start the script on a background thread so we can stop it from this thread. var task = Task.Run(() => interpreter.Execute(new List { "while True:", "sleep 100", "end_while" })); await Task.Delay(500); interpreter.Stop(); // signals the script to terminate at the next line boundary await task; ``` --- ## Reading registered statements `StatementInformation` returns a read-only snapshot of every registered statement. This is useful for building syntax highlighters or tooling. ```csharp var interpreter = new YesNtInterpreter(); foreach (var info in interpreter.StatementInformation) { Console.WriteLine($"{info.Name,-20} {info.SearchMode,-12} color={info.Color}"); } ``` Each `StatementInformation` object exposes: | Property | Type | Description | | -------------------------- | -------------- | ---------------------------- | | `Name` | `string` | The keyword | | `SearchMode` | `SearchMode` | Where the keyword is matched | | `SpaceAround` | `SpaceAround` | Required surrounding spaces | | `Color` | `ConsoleColor` | Syntax-highlight colour | | `IgnoreSyntaxHighlighting` | `bool` | Whether to skip highlighting | | `Separator` | `string?` | Optional required sub-string | --- ## API reference ### `YesNtInterpreter` ```csharp public class YesNtInterpreter ``` #### Constructor ```csharp public YesNtInterpreter() ``` Creates a new interpreter instance and registers all built-in statements. #### Events ```csharp public event Action OnDebugOutput; public event Action OnLineExecuted; ``` `OnDebugOutput` and `OnLineExecuted` are only raised in debug mode (`isDebugMode: true`). `OnLineExecuted` receives `null` only on EOF completion. #### Methods ```csharp // Execute a .ynt file to completion public void Execute(string path, bool isDebugMode = false); // Execute in-memory lines to completion public void Execute(IEnumerable lines, bool isDebugMode = false); // Prepare a script file for stepwise execution public void Prepare(string path, bool isDebugMode = false); // Prepare in-memory lines for stepwise execution public void Prepare(IEnumerable lines, bool isDebugMode = false); // Execute up to standard line count then pause public StepResult Step(int lines = 1); // Run the script for up to budget duration then pause public StepResult RunFor(TimeSpan budget); // Execute the remaining script lines to completion public void RunToCompletion(); // Register a custom statement (full control) public void AddStatement(StatementInformation attribute, Action handler); public void AddStatement(StatementInformation attribute, Action handler); // Register a custom statement (convenience overloads) public void AddStatement(string name, SearchMode searchMode, SpaceAround spaceAround, Action handler); public void AddStatement(string name, SearchMode searchMode, SpaceAround spaceAround, Action handler); public void AddStatement(string name, SearchMode searchMode, SpaceAround spaceAround, ConsoleColor color, Action handler); public void AddStatement(string name, SearchMode searchMode, SpaceAround spaceAround, ConsoleColor color, Action handler); // Remove a built-in or custom statement permanently public void RemoveStatement(string name); // Disable a statement (silent no-op; reversible) public void DisableStatement(string name); // Re-enable a previously disabled statement public void EnableStatement(string name); // Request graceful stop public void Stop(); ``` #### Properties ```csharp // Read-only snapshot of all registered statements public ReadOnlyCollection StatementInformation { get; } // Whether a prepared script is currently active/running public bool IsRunning { get; } // The script's console and files (default: ConsoleHost); set before the script starts public IYesNtHost Host { get; set; } ``` --- ### `IStatementContext` ```csharp public interface IStatementContext // YesNt.Interpreter.Runtime ``` Passed to `Action` handlers registered via `AddStatement`. Provides access to the script state that a built-in statement handler would have. | Member | Type | Description | | ----------------------------- | --------------------------- | ---------------------------------------------------------------------------- | | `Variables` | `Dictionary` | Local variable table for the current scope | | `GlobalVariables` | `Dictionary` | Global variable table shared across all scopes | | `CurrentLine` | `string` | The line being processed; write here for inline-substitution handlers | | `LineNumber` | `int` | Zero-based index of the next line to execute; set this to implement jumps | | `SetOut(params values)` | `void` | Replace the output stack with `values`, the last on top, for `%out` | | `Exit(message, stopAllTasks)` | `void` | Terminate execution with a message; `stopAllTasks: true` also ends all tasks | --- ### `StepResult` ```csharp public enum StepResult // YesNt.Interpreter.Runtime ``` Returned by `Step` and `RunFor` to indicate the outcome of the incremental execution. | Value | Description | | ---------- | -------------------------------------------------------------- | | `Continue` | A line was executed and more lines remain. | | `Paused` | The step or time budget was exhausted before the script ended. | | `Waiting` | The script waits; step it again later, not at once. | | `Finished` | The script ran to completion (or terminated/exited). | --- ### `IYesNtHost` and `ConsoleHost` ```csharp public interface IYesNtHost // YesNt.Interpreter.Runtime { void Write(string text); // output outside debug mode, line ends included InputStatus TryReadLine(out string line); // %read_line InputStatus TryReadKey(out char key); // %read_key void Clear(); // clear string[] ReadScript(string path); // the script and its imports } public class ConsoleHost : IYesNtHost // the default; every member is virtual ``` `ConsoleHost.TryReadLine` waits for a console line. `TryReadKey` returns `Pending` until a key is pressed, so the script waits without blocking the thread that steps it. ### `InputStatus` | Value | Description | | --------- | ------------------------------------------------------ | | `Ready` | The input is there. | | `Pending` | Not yet: the script waits and asks again when stepped. | | `Closed` | There will be no more input: the script ends. |