package org.yinwang.yin.ast;

import org.yinwang.yin.Constants;
import org.yinwang.yin.Scope;
import org.yinwang.yin._;
import org.yinwang.yin.value.BoolType;
import org.yinwang.yin.value.BoolValue;
import org.yinwang.yin.value.UnionType;
import org.yinwang.yin.value.Value;

public class If extends Node {
    public Node test;
    public Node then;
    public Node orelse;


    public If(Node test, Node then, Node orelse, String file, int start, int end, int line, int col) {
        super(file, start, end, line, col);
        this.test = test;
        this.then = then;
        this.orelse = orelse;
    }


    public Value interp(Scope s) {
        Value tv = interp(test, s);
        if (tv instanceof BoolValue) {
            if (((BoolValue) tv).value) {
                return interp(then, s);
            } else {
                return interp(orelse, s);
            }
        } else {
            _.abort(test, "test is not boolean: " + tv);
            return null;
        }
    }


    @Override
    public Value typecheck(Scope s) {
        Value tv = typecheck(test, s);
        if (!(tv instanceof BoolType)) {
            _.abort(test, "test is not boolean: " + tv);
            return null;
        }
        Value thenType = typecheck(then, s);
        Value elseType = typecheck(orelse, s);
        return UnionType.union(thenType, elseType);
    }


    public String toString() {
        return "(" + Constants.IF_KEYWORD + " " + test + " " + then + " " + orelse + ")";
    }

}

@@ -689,16 +689,18 @@
      if 
+(!
 (tv inst
@@ -712,24 +712,25 @@
 f BoolValue)
+)
  %7B%0A         
@@ -736,103 +736,126 @@
     
-if (((BoolValue) tv).value) %7B%0A                return interp(then, s);%0A            %7D else %7B%0A    
+_.abort(test, %22test is not boolean: %22 + tv);%0A            return null;%0A        %7D%0A        if (((BoolValue) tv).value) %7B%0A
     
@@ -880,34 +880,18 @@
 erp(
-orelse
+then
 , s);%0A
-            %7D%0A
     
@@ -919,76 +919,32 @@
     
-_.abort(test, %22test is not boolean: %22 + tv);%0A            return null
+return interp(orelse, s)
 ;%0A  

fix indentation