visitThenExpr method

  1. @override
Object? visitThenExpr(
  1. Then expr
)
override

Implementation

@override
Object? visitThenExpr(Expr.Then expr) {
  Object? object = evaluate(expr.future);
  if (object is Future) {
    Stmt.Functional then;
    var func = evaluate(expr.then);
    if (func is! LoxFunction) {
      throw RuntimeError(
          expr.name, "Only func can be used as Mapping function");
    } else {
      then = func.declaration;
    }
    Environment previous = environment;
    if (expr.name.lexeme == 'then') {
      return object.then((value) {
        if (expr.then is! Expr.Anonymous) {
          previous = environment;
        }
        LoxFunction thenFun = LoxFunction(then, previous, false);
        return thenFun.call(this, [value], {});
      });
    } else if (expr.name.lexeme == 'catchError') {
      return object.catchError((error) {
        if (expr.then is! Expr.Anonymous) {
          previous = environment;
        }
        LoxFunction thenFun = LoxFunction(then, previous, false);
        return thenFun.call(this, [error], {});
      });
    }
  }
  throw RuntimeError(expr.name, "Only future can be used in then.");
}