-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialog.cs
More file actions
85 lines (77 loc) · 2.83 KB
/
Dialog.cs
File metadata and controls
85 lines (77 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
namespace GameDialog.Lang
{
/// <summary>
/// Main entry point for the Game Dialog Script language.
/// </summary>
public class Dialog
{
/// <summary>
/// The interpreter instance used for executing scripts.
/// </summary>
private readonly Interpreter _interpreter;
/// <summary>
/// Initializes a new instance of the Dialog class.
/// </summary>
public Dialog()
{
_interpreter = new Interpreter();
}
/// <summary>
/// Executes a Game Dialog Script source code from a string.
/// Yields runtime items from << statements.
/// </summary>
/// <param name="inlineSource">The source code to execute.</param>
/// <returns>Enumerable of runtime items.</returns>
public IEnumerable<RuntimeItem> RunInline(string inlineSource)
{
var source = Source.Inline(inlineSource ?? throw new ArgumentNullException(nameof(inlineSource)));
return Run(source);
}
/// <summary>
/// Executes a Game Dialog Script file.
/// Yields runtime items from << statements.
/// </summary>
/// <param name="filePath">The path to the script file to execute.</param>
/// <returns>Enumerable of runtime items.</returns>
public IEnumerable<RuntimeItem> RunFile(string filePath)
{
var source = Source.FromFile(filePath ?? throw new ArgumentNullException(nameof(filePath)));
return Run(source);
}
/// <summary>
/// Executes a Game Dialog Script source code from a TextReader (streaming mode).
/// Yields runtime items from << statements.
/// </summary>
/// <param name="source">The source code to execute.</param>
/// <returns>Enumerable of runtime items.</returns>
private IEnumerable<RuntimeItem> Run(Source source)
{
// Tokenize (streaming).
using var lexer = new Lexer(source);
var tokens = lexer.Tokenize();
// Parse (streaming).
var parser = new Parser(tokens);
var statements = parser.Parse();
// Execute (streaming) - yields output values from << statements.
foreach (var item in _interpreter.Execute(statements))
{
yield return item;
}
}
/// <summary>
/// Gets all variables as a sequence of name-value pairs.
/// </summary>
public IEnumerable<(string name, RuntimeItem value)> Variables
{
get
{
foreach (var kvp in _interpreter.Variables)
{
yield return (kvp.Key, kvp.Value);
}
}
}
}
}