package org.kobjects.expressionparser.demo.thin.ast;

import org.kobjects.expressionparser.demo.thin.EvaluationContext;
import org.kobjects.expressionparser.demo.thin.ParsingContext;
import org.kobjects.expressionparser.demo.thin.type.Type;

class UnresolvedOperator extends Node {
  String name;

  UnresolvedOperator(String name, Expression... children) {
    super(null, children);
    this.name = name;
  }

  @Override
  public Expression resolve(ParsingContext context) {
    boolean allNumber = true;
    Expression[] resolved = new Expression[children.length];
    for (int i = 0; i < children.length; i++) {
      resolved[i] = children[i].resolve(context);
      if (resolved[i].type() != Type.NUMBER) {
        allNumber = false;
      }
    }

    if (name.equals("=")) {
      return new Assignment(resolved[0], resolved[1]);
    }


    if (!allNumber) {
      if (!name.equals("+")) {
        throw new IllegalArgumentException("number arguments expected for " + name);
      }
      return new Concat(resolved[0], resolved[1]);
    }

    org.kobjects.expressionparser.demo.thin.Wasm.Op op;
    switch(name.charAt(0)) {
      case '+': op = org.kobjects.expressionparser.demo.thin.Wasm.Op.F64Add; break;
      case '-': op = org.kobjects.expressionparser.demo.thin.Wasm.Op.F64Sub; break;
      case '*': op = org.kobjects.expressionparser.demo.thin.Wasm.Op.F64Mul; break;
      case '/': op = org.kobjects.expressionparser.demo.thin.Wasm.Op.F64Div; break;
      default:
        throw new RuntimeException("Unrecognized operator: " + name);
    }
    return new Operator(op, resolved[0], resolved[1]);
  }

  @Override
  public Object eval(EvaluationContext context) {
    throw new UnsupportedOperationException("Unresolved");
  }

  public String toString() {
    return "(" + children[0] + ' ' + name + ' ' + children[1] + ')';
  }
}

@@ -773,24 +773,143 @@
 als(%22=%22)) %7B%0A
+      if (resolved%5B0%5D.isAssignable()) %7B%0A        throw new RuntimeException(%22Cannot assign to %22 + resolved%5B0%5D);%0A      %7D%0A
       return
@@ -958,17 +958,16 @@
 %0A    %7D%0A%0A
-%0A
     if (

resolve assignment check