Open michalss opened 1 year ago
You can do it yourself in your own code! BasicSharp allows to define functions that can be used within basic scripts.
Example for contains
:
using System;
using BasicSharp;
namespace MyApp
{
class Program
{
public static Value Contains(Interpreter interpreter, List<Value> args)
{
if (args.Count < 2) // We expect 2 argument
throw new ArgumentException();
string a = args[0].Convert(ValueType.String).String; // convert argument to make sure it is string
string b = args[1].Convert(ValueType.String).String;
return Value(a.contains(b) ? 1.0 : 0.0); // convert true/false into 1 or 0
}
static void Main(string[] args)
{
string code = "print contains(\"hello world\", \"world\" )";
Interpreter basic = new Interpreter(code);
// add our 'contains' function to basic interpreter
basic.AddFunction('contains', Contains);
basic.printHandler += Console.WriteLine;
basic.inputHandler += Console.ReadLine;
try
{
basic.Exec();
}
catch (BasicException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Line);
}
}
}
}
Above code was written from memory without trying to compile it.
Hi i want to say this is great piece of work.
Can you please add contains indexOf *startWith
string methods pls ?
I would love to use it for simple calculations and string modifications...