public static double numBinopHandle(String op, double num1, double num2) { if (op.Equals("+", StringComparison.Ordinal)) { return(num1 + num2); } else if (op.Equals("-", StringComparison.Ordinal)) { return(num1 - num2); } else if (op.Equals("*", StringComparison.Ordinal)) { return(num1 * num2); } else if (op.Equals("/", StringComparison.Ordinal)) { if (num2 == 0) { LangException ex = new LangException("Divide by zero exception"); throw ex; } else { return(num1 / num2); } } else { LangException ex = new LangException("Bad num binop"); throw ex; } }
public void ParseBadExpression_Exception(string input) { ITokenizer tokenizer = new Tokenizer(input); IParser parser = new Parser(tokenizer, _context); LangException exception = Assert.Throws <LangException>(() => parser.Parse()); Assert.AreEqual(LangException.ErrorCode.UNEXPECTED_TOKEN, exception.ErrorType); }
public void ParseGroupingExpression_Fail(string expected) { ITokenizer tokenizer = new Tokenizer(expected); IParser parser = new Parser(tokenizer, _context); LangException exception = Assert.Throws <LangException>(() => parser.Parse()); Assert.AreEqual(LangException.ErrorCode.UNEXPECTED_TOKEN, exception.ErrorType); }
public static Value interp(ExprC exp) { if (exp.GetType() == typeof(numC)) { var n = exp as numC; return(new numV(n.number)); } else if (exp.GetType() == typeof(binopC)) { var b = exp as binopC; var lVal = interp(b.left); var rVal = interp(b.right); var lNum = lVal as numV; var rNum = rVal as numV; if (b.op == op.Equals("eq?", StringComparison.Ordinal) || b.op == op.Equals("<=", StringComparison.Ordinal)) { return(booleanBinopHandle(b.op, lNum.number, rNum.number)); } else if { return(numBinopHandle(b.op, lNum.number, rNum.number)); } } else if (exp.GetType() == typeof(boolC)) { boolC bC = exp as boolC; return(boolV(bC.b)); } else if (exp.GetType() == typeof(ifC)) { ifC i = exp as ifC; Value v = interp(i.conditional); if (v.GetType() == typeof(boolV)) { boolV bV = v as boolV; if (bV.b == true) { return(interp(i.then)); } else { return(interp(i.els)); } } else { stringV("interp: error, not a boolV value"); } } else { LangException ex = new LangException("Not a valid exprC"); throw ex; } }
public void ReadIdentifier_Exception(string input) { // Arrange ITokenizer tokenizer = new Tokenizer(input); // Act & Assert LangException exception = Assert.Throws <LangException>(() => tokenizer.ReadToken()); Assert.AreEqual(LangException.ErrorCode.UNEXPECTED_TOKEN, exception.ErrorType); }
public void ParseExpression_CustomOperators_Exception(string expected) { MathContext context = new MathContext(); ITokenizer tokenizer = new Tokenizer(expected); IParser parser = new Parser(tokenizer, context); LangException exception = Assert.Throws <LangException>(() => parser.Parse()); Assert.AreEqual(LangException.ErrorCode.UNEXPECTED_TOKEN, exception.ErrorType); }
public static Value interp(ExprC exp) { if (exp.GetType() == typeof(numC)) { var n = exp as numC; return(new numV(n.number)); } else if (exp.GetType() == typeof(binopC)) { var b = exp as binopC; var lVal = interp(b.left); var rVal = interp(b.right); var lNum = lVal as numV; var rNum = rVal as numV; return(new numV(numBinopHandle(b.op, lNum.number, rNum.number))); } else if (exp.GetType() == typeof(boolC)) { boolC bC = exp as boolC; return(new boolV(bC.b)); } else if (exp.GetType() == typeof(ifC)) { ifC i = exp as ifC; Value v = interp(i.conditional); if (v.GetType() == typeof(boolV)) { boolV bV = v as boolV; if (bV.b == true) { return(interp(i.then)); } else { return(interp(i.els)); } } else { LangException ex = new LangException("ifC failed"); throw ex; } } else { LangException ex = new LangException("Not a valid exprC"); throw ex; } }
public static Value interp(ExprC exp) { if (exp.GetType() == typeof(numC)) { var n = exp as numC; return(new numV(n.number)); } else { LangException ex = new LangException("Not a valid exprC"); throw ex; } }
public bool booleanBinopHandle(String op, Object prim1, Object prim2) { Type prim1Type = prim1.GetType(); Type prim2Type = prim2.GetType(); if (op.Equals("<=", StringComparison.Ordinal)) { if (prim1Type.Equals(typeof(double)) && prim2Type.Equals(typeof(double))) { double num1 = (double)prim1; double num2 = (double)prim2; return(num1 <= num2); } else { LangException ex = new LangException("Both types for <= must be doubles"); throw ex; } } else if (op.Equals("eq?", StringComparison.Ordinal)) { if (!prim1Type.Equals(prim2Type)) { return(false); } else if (prim1Type.Equals(typeof(double))) { double num1 = (double)prim1; double num2 = (double)prim2; return(num1 == num2); } else { bool bool1 = (bool)prim1; bool bool2 = (bool)prim2; return(bool1 == bool2); } } else { LangException ex = new LangException("Bad boolean binop"); throw ex; } }
/// <summary> /// Executes an action. /// </summary> /// <param name="action"></param> /// <param name="exceptionMessageFetcher"></param> public static RunResult Execute(Action action, Func <string> exceptionMessageFetcher = null) { DateTime start = DateTime.Now; bool success = true; string message = string.Empty; Exception scriptError = null; try { action(); } catch (Exception ex) { success = false; if (ex is LangException) { LangException lex = ex as LangException; const string langerror = "{0} : {1} at line : {2}, position: {3}"; message = string.Format(langerror, lex.Error.ErrorType, lex.Message, lex.Error.Line, lex.Error.Column); } else { message = ex.Message; } scriptError = ex; if (exceptionMessageFetcher != null) { message += exceptionMessageFetcher(); } } DateTime end = DateTime.Now; var runResult = new RunResult(start, end, success, message); runResult.Ex = scriptError; return(runResult); }
/// <summary> /// Collects an unexpected token error and advances to next token. /// </summary> public void AddError(TokenData token, string error) { var ex = new LangException("Parse", error, this._scriptPath, token.Line, token.LineCharPos); this._parseErrors.Add(ex); }
public void Evaluate_Unassigned_Variable_Exception(string input) { LangException exception = Assert.Throws <LangException>(() => _calculator.Evaluate(input)); Assert.AreEqual(LangException.ErrorCode.UNASSIGNED_VARIABLE, exception.ErrorType); }