<commit_before>package natlab.backends.vrirGen;

import natlab.tame.classes.reference.PrimitiveClassReference;
import ast.Expr;
import ast.FPLiteralExpr;
import ast.IntLiteralExpr;
import ast.MatrixExpr;
import ast.NameExpr;
import ast.ParameterizedExpr;
import ast.RangeExpr;
import ast.StringLiteralExpr;

public class ExprCaseHandler {
	public static void handleParameterizedExpr(ParameterizedExpr node,
			VrirXmlGen gen) {
		if (gen.getRemainingVars().contains(node.getVarName())) {

			ExprCaseHandler.handleIndexExpr(node, gen);
		} else {

			// Operator
			if (OperatorMapper.isOperator(node.getVarName())) {
				// Binary operator
				ExprCaseHandler.handleOpExpr(node, gen,
						OperatorMapper.get(node.getVarName()));
				// ExprCaseHandler.handlePlusExpr(node, this);

			}
			// Function Call
			else {

				ExprCaseHandler.handleFunCallExpr(node, gen);

			}

		}
	}

	public static void handleNameExpr(NameExpr node, VrirXmlGen gen) {
		if (HelperClass.isVar(gen, node.getName().getID())) {

			if (!gen.getSymTab().contains(node.getName().getID())) {

				VType vtype = HelperClass.generateVType(gen.getAnalysis(),
						gen.getIndex(), node.getName().getID());
				gen.getSymTab().putSymbol(vtype, node.getName().getID());
			}
			if (gen.getSymbol(node.getName().getID()) != null) {
				gen.appendToPrettyCode(toXMLHead("name",
						gen.getSymbol(node.getName().getID()).getId(), "id"));

			} else {
				throw new NullPointerException("Symbol not found for "
						+ node.getName().getID());
			}
			gen.appendToPrettyCode(gen.getSymbol(node.getName().getID())
					.getVtype().toXML());
		}

		else {
			handleFunCallExpr(node, gen);
		}

		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleOpExpr(ParameterizedExpr node, VrirXmlGen gen,
			String name) {
		if (node.getArgList().getNumChild() == 2) {
			handleBinExpr(node, gen, name);
		} else if (node.getArgList().getNumChild() == 1) {
			handleUnaryExpr(node, gen, name);

		}
	}

	public static void handleMatrixExpr(MatrixExpr node, VrirXmlGen gen) {
		if (node.getRows().getNumChild() > 1) {
			System.out.println("Multiple rows now supported");
			System.exit(1);
		}
		// for a single element
		if (node.getRow(0).getElementList().getNumChild() == 1) {
			node.getRow(0).getElement(0).analyze(gen);

		} else {
			// tuple type for multiple elements

			// gen.appendToPrettyCode(toXMLHead("tuple", "1", "ndims"));
			gen.appendToPrettyCode(toXMLHead("tuple"));
			gen.appendToPrettyCode(HelperClass.toXML("elems"));
			for (Expr expr : node.getRow(0).getElementList()) {

				expr.analyze(gen);

			}
			gen.appendToPrettyCode(HelperClass.toXML("/elems"));
			gen.appendToPrettyCode(toXMLTail());
		}
	}

	public static void handleUnaryExpr(ParameterizedExpr node, VrirXmlGen gen,
			String name) {
		gen.appendToPrettyCode(toXMLHead(name));
		// TODO : Get unary expression type
		gen.appendToPrettyCode(HelperClass.getExprType(node, gen).toXML());
		gen.appendToPrettyCode(HelperClass.toXML("base"));
		node.getArg(0).analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/base"));
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleBinExpr(ParameterizedExpr node, VrirXmlGen gen,
			String name) {

		if (name.trim().equalsIgnoreCase("mmult")) {
			VType vt = HelperClass.getExprType(node.getArg(0), gen);
			if (vt instanceof VTypeMatrix) {

				if (((VTypeMatrix) vt).getShape().getDimensions().get(0)
						.equalsOne()
						&& ((VTypeMatrix) vt).getShape().getDimensions().get(1)
								.equalsOne()
						&& (((VTypeMatrix) vt).getShape().getDimensions()
								.size() == 2)) {
					name = "mult";
				}
			} else {
				throw new UnsupportedOperationException(
						"operations on cell arrays not supported");
			}
			vt = HelperClass.getExprType(node.getArg(1), gen);
			if (vt instanceof VTypeMatrix) {

				if (((VTypeMatrix) vt).getShape().getDimensions().get(0)
						.equalsOne()
						&& ((VTypeMatrix) vt).getShape().getDimensions().get(1)
								.equalsOne()
						&& (((VTypeMatrix) vt).getShape().getDimensions()
								.size() == 2)) {
					name = "mult";
				}
			} else {
				throw new UnsupportedOperationException(
						"operations on cell arrays not supported");
			}

		}
		gen.appendToPrettyCode(toXMLHead(name));

		gen.appendToPrettyCode(HelperClass.getExprType(node, gen).toXML());
		gen.appendToPrettyCode("<rhs>\n");
		node.getArg(1).analyze(gen);
		gen.appendToPrettyCode("</rhs>\n");
		gen.appendToPrettyCode("<lhs>\n");
		node.getArg(0).analyze(gen);
		gen.appendToPrettyCode("</lhs>\n");
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleRangeExpr(RangeExpr node, VrirXmlGen gen) {
		gen.appendToPrettyCode(toXMLHead("range"));
		gen.appendToPrettyCode(HelperClass.toXML("start"));
		node.getLower().analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/start"));

		if (node.hasIncr()) {

			gen.appendToPrettyCode(HelperClass.toXML("step"));
			node.getIncr().analyze(gen);
			gen.appendToPrettyCode(HelperClass.toXML("/step"));
		}
		gen.appendToPrettyCode(HelperClass.toXML("stop"));
		node.getUpper().analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/stop"));
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleIntLiteralExpr(IntLiteralExpr expr, VrirXmlGen gen) {
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"Could not generate vtype for const expression");
		}
		if (expr.getValue().isImaginary()) {
			// TODO : Handle complex integers
		} else {
			String field = "";
			if (vt instanceof VTypeMatrix) {
				if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.DOUBLE) {
					field = "dval";
				} else if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.SINGLE) {
					field = "fval";
				} else if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT32
						|| ((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT64
						|| ((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT16
						|| ((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT8) {

				} else {
					throw new UnsupportedOperationException(
							"cant identify type" + ((VTypeMatrix) vt).getType());
				}
			} else {
				throw new UnsupportedOperationException("Cannot identify VType"
						+ vt.getClass());
			}
			gen.appendToPrettyCode(toXMLHead("realconst", expr.getValue()
					.getValue().toString(), field));
		}
		gen.appendToPrettyCode(vt.toXML());
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleFpLiteralExpr(FPLiteralExpr expr, VrirXmlGen gen) {
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"Could not generate vtype for const expression");
		}
		if (expr.getValue().isImaginary()) {
			// TODO: Handle complex floats
		} else {
			String field = "";
			if (vt instanceof VTypeMatrix) {
				if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.DOUBLE) {
					field = "dval";
				} else if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.SINGLE) {
					field = "fval";
				} else {
					throw new UnsupportedOperationException(
							"cant identify type" + ((VTypeMatrix) vt).getType());
				}
			} else {
				throw new UnsupportedOperationException("Cannot identify VType"
						+ vt.getClass());
			}
			gen.appendToPrettyCode(toXMLHead("realconst", expr.getValue()
					.getValue().toString(), field));
		}
		gen.appendToPrettyCode(vt.toXML());

		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleFunCallExpr(ParameterizedExpr expr, VrirXmlGen gen) {
		if (HelperClass.isAllocFunc(expr.getVarName())) {

			handleAllocExpr(expr, gen);
			return;

		}
		if (HelperClass.isLibFunc(expr.getVarName())) {
			handleLibCallExpr(expr, gen);
			return;
		}
		if (expr.getVarName().equals("colon")) {
			handleColonCall(expr, gen);
			return;
		}
		gen.appendToPrettyCode(toXMLHead("fncall", expr.getVarName(), "fnname"));
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"VType of function call expression could not be generated");
		}
		gen.appendToPrettyCode(vt.toXML());
		gen.appendToPrettyCode(HelperClass.toXML("args"));
		for (Expr args : expr.getArgList()) {
			args.analyze(gen);
		}
		gen.appendToPrettyCode(HelperClass.toXML("/args"));
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleFunCallExpr(NameExpr expr, VrirXmlGen gen) {
		gen.appendToPrettyCode(toXMLHead("fncall", expr.getName().getID(),
				"fnname"));
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"VType of function call expression could not be generated");
		}
		gen.appendToPrettyCode(vt.toXML());
		gen.appendToPrettyCode(HelperClass.toXML("args"));
		gen.appendToPrettyCode(HelperClass.toXML("/args"));
		gen.appendToPrettyCode(toXMLTail());

	}

	public static void handleColonCall(ParameterizedExpr expr, VrirXmlGen gen) {
		gen.appendToPrettyCode(toXMLHead("range"));
		int indx = 0;
		gen.appendToPrettyCode(HelperClass.toXML("start"));
		expr.getArg(indx).analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/start"));
		indx++;
		if (expr.getArgList().getNumChild() > 2) {
			gen.appendToPrettyCode(HelperClass.toXML("step"));
			expr.getArg(indx).analyze(gen);
			gen.appendToPrettyCode(HelperClass.toXML("/step"));
			indx++;
		}
		gen.appendToPrettyCode(HelperClass.toXML("stop"));
		expr.getArg(indx).analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/stop"));
		indx++;
		gen.appendToPrettyCode(toXMLTail());<commit_msg>Range is no longer an expression but is instead a stand alone node.<commit_after>package natlab.backends.vrirGen;

import natlab.tame.classes.reference.PrimitiveClassReference;
import ast.Expr;
import ast.FPLiteralExpr;
import ast.IntLiteralExpr;
import ast.MatrixExpr;
import ast.NameExpr;
import ast.ParameterizedExpr;
import ast.RangeExpr;
import ast.StringLiteralExpr;

public class ExprCaseHandler {
	public static void handleParameterizedExpr(ParameterizedExpr node,
			VrirXmlGen gen) {
		if (gen.getRemainingVars().contains(node.getVarName())) {

			ExprCaseHandler.handleIndexExpr(node, gen);
		} else {

			// Operator
			if (OperatorMapper.isOperator(node.getVarName())) {
				// Binary operator
				ExprCaseHandler.handleOpExpr(node, gen,
						OperatorMapper.get(node.getVarName()));
				// ExprCaseHandler.handlePlusExpr(node, this);

			}
			// Function Call
			else {

				ExprCaseHandler.handleFunCallExpr(node, gen);

			}

		}
	}

	public static void handleNameExpr(NameExpr node, VrirXmlGen gen) {
		if (HelperClass.isVar(gen, node.getName().getID())) {

			if (!gen.getSymTab().contains(node.getName().getID())) {

				VType vtype = HelperClass.generateVType(gen.getAnalysis(),
						gen.getIndex(), node.getName().getID());
				gen.getSymTab().putSymbol(vtype, node.getName().getID());
			}
			if (gen.getSymbol(node.getName().getID()) != null) {
				gen.appendToPrettyCode(toXMLHead("name",
						gen.getSymbol(node.getName().getID()).getId(), "id"));

			} else {
				throw new NullPointerException("Symbol not found for "
						+ node.getName().getID());
			}
			gen.appendToPrettyCode(gen.getSymbol(node.getName().getID())
					.getVtype().toXML());
		}

		else {
			handleFunCallExpr(node, gen);
		}

		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleOpExpr(ParameterizedExpr node, VrirXmlGen gen,
			String name) {
		if (node.getArgList().getNumChild() == 2) {
			handleBinExpr(node, gen, name);
		} else if (node.getArgList().getNumChild() == 1) {
			handleUnaryExpr(node, gen, name);

		}
	}

	public static void handleMatrixExpr(MatrixExpr node, VrirXmlGen gen) {
		if (node.getRows().getNumChild() > 1) {
			System.out.println("Multiple rows now supported");
			System.exit(1);
		}
		// for a single element
		if (node.getRow(0).getElementList().getNumChild() == 1) {
			node.getRow(0).getElement(0).analyze(gen);

		} else {
			// tuple type for multiple elements

			// gen.appendToPrettyCode(toXMLHead("tuple", "1", "ndims"));
			gen.appendToPrettyCode(toXMLHead("tuple"));
			gen.appendToPrettyCode(HelperClass.toXML("elems"));
			for (Expr expr : node.getRow(0).getElementList()) {

				expr.analyze(gen);

			}
			gen.appendToPrettyCode(HelperClass.toXML("/elems"));
			gen.appendToPrettyCode(toXMLTail());
		}
	}

	public static void handleUnaryExpr(ParameterizedExpr node, VrirXmlGen gen,
			String name) {
		gen.appendToPrettyCode(toXMLHead(name));
		// TODO : Get unary expression type
		gen.appendToPrettyCode(HelperClass.getExprType(node, gen).toXML());
		gen.appendToPrettyCode(HelperClass.toXML("base"));
		node.getArg(0).analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/base"));
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleBinExpr(ParameterizedExpr node, VrirXmlGen gen,
			String name) {

		if (name.trim().equalsIgnoreCase("mmult")) {
			VType vt = HelperClass.getExprType(node.getArg(0), gen);
			if (vt instanceof VTypeMatrix) {

				if (((VTypeMatrix) vt).getShape().getDimensions().get(0)
						.equalsOne()
						&& ((VTypeMatrix) vt).getShape().getDimensions().get(1)
								.equalsOne()
						&& (((VTypeMatrix) vt).getShape().getDimensions()
								.size() == 2)) {
					name = "mult";
				}
			} else {
				throw new UnsupportedOperationException(
						"operations on cell arrays not supported");
			}
			vt = HelperClass.getExprType(node.getArg(1), gen);
			if (vt instanceof VTypeMatrix) {

				if (((VTypeMatrix) vt).getShape().getDimensions().get(0)
						.equalsOne()
						&& ((VTypeMatrix) vt).getShape().getDimensions().get(1)
								.equalsOne()
						&& (((VTypeMatrix) vt).getShape().getDimensions()
								.size() == 2)) {
					name = "mult";
				}
			} else {
				throw new UnsupportedOperationException(
						"operations on cell arrays not supported");
			}

		}
		gen.appendToPrettyCode(toXMLHead(name));

		gen.appendToPrettyCode(HelperClass.getExprType(node, gen).toXML());
		gen.appendToPrettyCode("<rhs>\n");
		node.getArg(1).analyze(gen);
		gen.appendToPrettyCode("</rhs>\n");
		gen.appendToPrettyCode("<lhs>\n");
		node.getArg(0).analyze(gen);
		gen.appendToPrettyCode("</lhs>\n");
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleRangeExpr(RangeExpr node, VrirXmlGen gen) {
		gen.appendToPrettyCode(HelperClass.toXML("range"));
		gen.appendToPrettyCode(HelperClass.toXML("start"));
		node.getLower().analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/start"));

		if (node.hasIncr()) {

			gen.appendToPrettyCode(HelperClass.toXML("step"));
			node.getIncr().analyze(gen);
			gen.appendToPrettyCode(HelperClass.toXML("/step"));
		}
		gen.appendToPrettyCode(HelperClass.toXML("stop"));
		node.getUpper().analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/stop"));
		gen.appendToPrettyCode(HelperClass.toXML("/range"));
	}

	public static void handleIntLiteralExpr(IntLiteralExpr expr, VrirXmlGen gen) {
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"Could not generate vtype for const expression");
		}
		if (expr.getValue().isImaginary()) {
			// TODO : Handle complex integers
		} else {
			String field = "";
			if (vt instanceof VTypeMatrix) {
				if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.DOUBLE) {
					field = "dval";
				} else if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.SINGLE) {
					field = "fval";
				} else if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT32
						|| ((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT64
						|| ((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT16
						|| ((VTypeMatrix) vt).getType() == PrimitiveClassReference.INT8) {
					field = "ival";

				} else {
					throw new UnsupportedOperationException(
							"cant identify type" + ((VTypeMatrix) vt).getType());
				}
			} else {
				throw new UnsupportedOperationException("Cannot identify VType"
						+ vt.getClass());
			}
			gen.appendToPrettyCode(toXMLHead("realconst", expr.getValue()
					.getValue().toString(), field));
		}
		gen.appendToPrettyCode(vt.toXML());
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleFpLiteralExpr(FPLiteralExpr expr, VrirXmlGen gen) {
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"Could not generate vtype for const expression");
		}
		if (expr.getValue().isImaginary()) {
			// TODO: Handle complex floats
		} else {
			String field = "";
			if (vt instanceof VTypeMatrix) {
				if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.DOUBLE) {
					field = "dval";
				} else if (((VTypeMatrix) vt).getType() == PrimitiveClassReference.SINGLE) {
					field = "fval";
				} else {
					throw new UnsupportedOperationException(
							"cant identify type" + ((VTypeMatrix) vt).getType());
				}
			} else {
				throw new UnsupportedOperationException("Cannot identify VType"
						+ vt.getClass());
			}
			gen.appendToPrettyCode(toXMLHead("realconst", expr.getValue()
					.getValue().toString(), field));
		}
		gen.appendToPrettyCode(vt.toXML());

		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleFunCallExpr(ParameterizedExpr expr, VrirXmlGen gen) {
		if (HelperClass.isAllocFunc(expr.getVarName())) {

			handleAllocExpr(expr, gen);
			return;

		}
		if (HelperClass.isLibFunc(expr.getVarName())) {
			handleLibCallExpr(expr, gen);
			return;
		}
		if (expr.getVarName().equals("colon")) {
			handleColonCall(expr, gen);
			return;
		}
		gen.appendToPrettyCode(toXMLHead("fncall", expr.getVarName(), "fnname"));
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"VType of function call expression could not be generated");
		}
		gen.appendToPrettyCode(vt.toXML());
		gen.appendToPrettyCode(HelperClass.toXML("args"));
		for (Expr args : expr.getArgList()) {
			args.analyze(gen);
		}
		gen.appendToPrettyCode(HelperClass.toXML("/args"));
		gen.appendToPrettyCode(toXMLTail());
	}

	public static void handleFunCallExpr(NameExpr expr, VrirXmlGen gen) {
		gen.appendToPrettyCode(toXMLHead("fncall", expr.getName().getID(),
				"fnname"));
		VType vt = HelperClass.getExprType(expr, gen);
		if (vt == null) {
			throw new NullPointerException(
					"VType of function call expression could not be generated");
		}
		gen.appendToPrettyCode(vt.toXML());
		gen.appendToPrettyCode(HelperClass.toXML("args"));
		gen.appendToPrettyCode(HelperClass.toXML("/args"));
		gen.appendToPrettyCode(toXMLTail());

	}

	public static void handleColonCall(ParameterizedExpr expr, VrirXmlGen gen) {
		gen.appendToPrettyCode(HelperClass.toXML("range"));
		int indx = 0;
		gen.appendToPrettyCode(HelperClass.toXML("start"));
		expr.getArg(indx).analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/start"));
		indx++;
		if (expr.getArgList().getNumChild() > 2) {
			gen.appendToPrettyCode(HelperClass.toXML("step"));
			expr.getArg(indx).analyze(gen);
			gen.appendToPrettyCode(HelperClass.toXML("/step"));
			indx++;
		}
		gen.appendToPrettyCode(HelperClass.toXML("stop"));
		expr.getArg(indx).analyze(gen);
		gen.appendToPrettyCode(HelperClass.toXML("/stop"));
		indx++;
		gen.appendToPrettyCode(HelperClass.toXML("/range"));
{'fraction_non_alphanumeric': 0.15322121604139716, 'fraction_numerical': 0.0031047865459249676, 'mean_word_length': 4.3006034009873835, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1372569', 'n_tokens_mistral': 7516, 'n_tokens_neox': 6341, 'n_words': 1105}