Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is absolutely perfect #2

Open
michalss opened this issue Jan 6, 2023 · 1 comment
Open

This is absolutely perfect #2

michalss opened this issue Jan 6, 2023 · 1 comment

Comments

@michalss
Copy link

michalss commented Jan 6, 2023

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...

@Timu5
Copy link
Owner

Timu5 commented Jan 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants