From 6fe4972aa48a29d0aafee0461ccd6c635ca9ee6c Mon Sep 17 00:00:00 2001 From: Sophia Pearson Date: Mon, 23 May 2022 00:02:58 +0200 Subject: commands: add basic command parser infrastructure --- Scripts/Command.cs | 22 ++++++++++++++-- Scripts/CommandParser.cs | 27 ++++++++++++------- Scripts/Commands/LookCommand.cs | 57 +++++++++++++++++++++++++++++++++++++++++ Scripts/OutputRow.cs | 8 +++--- 4 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 Scripts/Commands/LookCommand.cs (limited to 'Scripts') diff --git a/Scripts/Command.cs b/Scripts/Command.cs index 4a7ce58..e784576 100644 --- a/Scripts/Command.cs +++ b/Scripts/Command.cs @@ -1,7 +1,25 @@ +using Godot; + namespace Texty.Scripts { - public class Command + public enum CommandType { - + Look + } + + public class Command : Object + { + public Command() + { + } + + public Command(CommandType type, string[] arguments) + { + RawArguments = arguments; + Type = type; + } + + public string[] RawArguments { get; } + public CommandType Type { get; } } } \ No newline at end of file diff --git a/Scripts/CommandParser.cs b/Scripts/CommandParser.cs index 1394a5e..97d8025 100644 --- a/Scripts/CommandParser.cs +++ b/Scripts/CommandParser.cs @@ -1,14 +1,23 @@ +using System.Linq; using Godot; +using Texty.Scripts.Commands; namespace Texty.Scripts { - public class CommandParser : Node - { - - public override void _Ready() - { - } - - } -} + public class CommandParser : Node + { + public override void _Ready() + { + } + public Command TryParse(string text) + { + var components = text.Split(' '); + return components[0].ToLower() switch + { + "look" => new LookCommand(components.Skip(1).ToArray()), + _ => null + }; + } + } +} \ No newline at end of file diff --git a/Scripts/Commands/LookCommand.cs b/Scripts/Commands/LookCommand.cs new file mode 100644 index 0000000..08783a9 --- /dev/null +++ b/Scripts/Commands/LookCommand.cs @@ -0,0 +1,57 @@ +using System.Linq; + +namespace Texty.Scripts.Commands +{ + public enum LookModifier + { + At, + Around + } + + public class LookCommand : Command + { + public LookCommand() + { + } + + public LookCommand(string[] arguments) : base(CommandType.Look, arguments) + { + (Modifier, arguments) = TryParseModifier(arguments); + Target = string.Join(" ", arguments); + } + + public string Target { get; } + public LookModifier? Modifier { get; } + + /// + /// Access this LookCommand's modifier as a string. + /// + /// + /// This function is provided as a means for testing the modifier parsing from GDScript. + /// + private string ModifierAsString => Modifier?.ToString(); + + public override string ToString() + { + return $"look {ModifierAsString?.ToLower()} {string.Join(" ", Target)}"; + } + + /// + /// Try to parse this LookCommand's modifier from the given arguments. + /// + /// The arguments given to this look commands + /// A pair consisting of the parse modifier and the remaining arguments + private static (LookModifier?, string[]) TryParseModifier(string[] arguments) + { + if (arguments.Length == 0) + return (null, arguments); + + return arguments[0].ToLower() switch + { + "at" => (LookModifier.At, arguments.Skip(1).ToArray()), + "around" => (LookModifier.Around, new string[] { }), + _ => (null, arguments) + }; + } + } +} \ No newline at end of file diff --git a/Scripts/OutputRow.cs b/Scripts/OutputRow.cs index 15a6454..13d9293 100644 --- a/Scripts/OutputRow.cs +++ b/Scripts/OutputRow.cs @@ -11,12 +11,12 @@ namespace Texty.Scripts [Signal] public delegate void OutputTextChanged(string newText); - public Label Input; - public Label Output; - private string _inputText = ""; private string _outputText = ""; + public Label Input; + public Label Output; + [Export] public string InputText { @@ -69,4 +69,4 @@ namespace Texty.Scripts Output } } -} \ No newline at end of file +} \ No newline at end of file -- cgit v1.2.3