import Program from './core/Program'
import Delegate from './core/Delegate'
import JSONObject from "./instances/JSONObject";

export default class ExpressionRunner {
  /**
   * Runs an expression with parameters
   *
   * @param source Expression source
   * @param params Expression parameters
   * @returns The result of the expression
   */
  public static run(source: string, params: JSONObject): any {
    const delegate: Delegate = this.getDelegate(source);
    return delegate.invoke(params);
  }

  /**
   * Gets the delegate for the expression
   *
   * @param source Expression source
   * @returns A delegate to invoke
   */
  public static getDelegate(source: string): Delegate {
    // Parse the source and return a delegate
    return new Program(source).parse();
  }
}
