minor fixes and adjustments

This commit is contained in:
Valerie Wolfe 2024-04-03 17:26:40 -04:00
parent 1a23989a8f
commit c8bf6073e6
3 changed files with 36 additions and 14 deletions

View file

@ -76,11 +76,13 @@ namespace Dungeoneer.Interpreter {
}
/** <summary> convenience object class for tokenized input </summary> */
public class TokenSet : List<Token> {
public TokenSet() : base() { }
public TokenSet(List<Token> set) : base(set) { }
/** <summary> returns the first token in the set and removes it. </summary> */
public Token Pop() {
Token output = this[0];
RemoveAt(0);
@ -102,20 +104,24 @@ namespace Dungeoneer.Interpreter {
}
/** <summary> a token that contains a value. </summary> */
public abstract class DataToken<T> : Token {
public T Value { get; protected set; }
}
/** <summary> a token that is ignored during arithmetic evaluation. </summary> */
public abstract class MetadataToken<T> : DataToken<T> {
public T Value { get; protected set; }
}
/** <summary> a token with a numerical value </summary> */
public abstract class ValueToken : DataToken<int> {
public override string ToString() { return Value.ToString(); }
}
/** <summary> a token with a string value </summary> */
public abstract class StringToken : DataToken<string> {
private const string Style = "\x1b[92m";
@ -129,6 +135,7 @@ namespace Dungeoneer.Interpreter {
}
/** <summary> a token representing an arithmetic operator. </summary> */
public class OperatorToken : DataToken<char> {
public OperatorToken(char op) { Value = op; }
@ -137,6 +144,7 @@ namespace Dungeoneer.Interpreter {
}
/** <summary> a token representing a numeric literal </summary> */
public class NumberToken : ValueToken {
internal static readonly Regex Pattern = new Regex(@"^(\d+)$");
@ -147,8 +155,7 @@ namespace Dungeoneer.Interpreter {
}
public class NameToken : Token {
public string Value { get; private set; }
public class NameToken : DataToken<string> {
internal static readonly Regex Pattern = new Regex(@"^[A-Za-z][A-Za-z\d\-_]+$");

View file

@ -37,7 +37,7 @@ namespace Dungeoneer {
}
}
string message = success ? $"{Format.Green}pass" : "{Format.Red}fail";
string message = success ? $"{Format.Green}pass" : $"{Format.Red}fail";
Console.WriteLine($"{dicePool} > {target}\n => {message}{Format.Reset}");
}

View file

@ -19,36 +19,51 @@ namespace Dungeoneer {
Scope = Engine.CreateScope();
dynamic builtin = Engine.GetBuiltinModule();
// set up imports
var paths = Engine.GetSearchPaths();
paths.Add("/usr/lib/python3.12/");
Engine.SetSearchPaths(paths);
// set up python environment
builtin.SetVariable("input", (Func<string, string>)Input);
builtin.RemoveVariable("open");
builtin.SetVariable("input", (Func<string, string>)Input); // `input` in IP is broken; swap for our implementation
builtin.RemoveVariable("open"); // remove 'open' builtin; don't trust scripts with file i/o
// helper vars
Scope.repl_commands = Program.ReplCommands;
Scope.roll_macros = Program.RollMacros;
Scope.repl_commands = Program.ReplCommands; // repl command dictionary
Scope.roll_macros = Program.RollMacros; // roll macro dictionary
// interpreter api object
Scope.interpret = new ExpandoObject();
Scope.interpret.tokenize = (Func<string, TokenSet>)Lexer.Tokenize;
Scope.interpret.input = (Func<string,TokenSet>)LexInput;
Scope.interpret.first_token = (Func<string, Token>)Lexer.First;
// Scope.interpret.parse_roll = (Func<string, string>)Interpreter.Parser.ParseRoll;
Scope.interpret.parse_roll = (Func<string, int>)Interpreter.Parser.ParseRoll;
// helper functions
Scope.roll = (Func<int, int>)Dungeoneer.Util.Roll;
}
/** <summary> Executes a Python file. </summary> */
public static void Run(string file) { Engine.ExecuteFile(file, Scope); }
public static dynamic Expr(string expression) { return Engine.Execute(expression, Scope); }
/** <summary> used for the python interpreter repl. don't use this!!! </summary> */
public static dynamic Expression(string input) { return Engine.Execute(input, Scope); }
/** <summary>
* reimplementation of the Python builtin `input`.
* the default builtin doesn't work correctly.
* </summary>
*/
public static string Input(string prompt) {
Console.Write(prompt);
return Console.ReadLine();
}
/** <summary>
* implementation of the `interpret.input` helper object method. tokenizes
* the result of the `input` builtin.
* </summary>
*/
public static TokenSet LexInput(string prompt) {
var input = Input(prompt);
return Interpreter.Lexer.Tokenize(input);
}
}
}