private static IObject getData(IExpressionData data) { IObject Value = null; if (data is DoubleType) { Value = new VariableData(typeof(Double), (double)data.GetData()); } else if (data is StringType) { Value = new VariableData(typeof(String), (string)data.GetData()); } else if (data is BoolType) { Value = new VariableData(typeof(Boolean), (bool)data.GetData()); } else if (data is ObjectType) { Value = (IObject)data.GetData(); } else { throw new Exception("Unknown data type in Variable Dictionary"); } return(Value); }
public IExpressionData Evaluate() { IExpressionData leftData = leftExpression.Evaluate(); IExpressionData rightData = rightExpression.Evaluate(); if (leftData.GetType() != rightData.GetType()) { throw new Exception("Operation on different types"); } if (leftData is DoubleType) { double left, right; left = (double)leftData.GetData(); right = (double)rightData.GetData(); switch (Operation) { case '+': return(new DoubleType(left + right)); case '-': return(new DoubleType(left - right)); case '*': return(new DoubleType(left * right)); case '/': return(new DoubleType(left / right)); default: throw new Exception("Unknown operation"); } } else if (leftData is StringType) { if (Operation != '+') { throw new Exception("Unknown operation on string"); } string left = (string)leftData.GetData(); string right = (string)rightData.GetData(); return(new StringType(left + right)); } else if (leftData is BoolType) { throw new Exception("Cant use operator on boolen type"); } else { throw new Exception("Not initialized data"); } }
private IObject getObject() { IExpressionData data = expression.Evaluate(); IObject result = null; if (data is StringType) { result = new VariableData(typeof(string), data.GetData()); } else if (data is DoubleType) { result = new VariableData(typeof(double), data.GetData()); } else if (data is BoolType) { result = new VariableData(typeof(bool), data.GetData()); } else if (data is ObjectType) { result = (IObject)data.GetData(); } return(result); }
public IExpressionData Evaluate() { IExpressionData data = expression.Evaluate(); if (data is DoubleType) { switch (Operator) { case '-': return(new DoubleType(-(double)data.GetData())); case '+': return(data); default: return(data); } } else if (data is BoolType && Operator == '!') { return(new BoolType(!(bool)data.GetData())); } else { throw new Exception("Cant create unary expression. Evaluate"); } }
//Methods public IExpressionData Evaluate() { if (!IsObject) { return(DefaultEvaluate()); } IExpressionData container = (new VariableExpression(name)).Evaluate(); IObject array = null; if (container is ObjectType) { array = (IObject)container.GetData(); } else { throw new Exception("Brackets to a variable"); } GetIndexes(); for (int i = 0; i < Indexes.Count; ++i) { if (Indexes[i] is string && array is ArrayObject) { array = ((ArrayObject)array)[(string)Indexes[i]]; } else if (Indexes[i] is int && array is ArrayIndex) { array = ((ArrayIndex)array)[(int)Indexes[i]]; } else { throw new Exception("Inappropriate index in brackets"); } } if (array is VariableData) { return(getVariable(array)); } else { throw new Exception("Inappropriate count of indexes"); } }
//File private static IExpressionData LoadText(List <IExpression> parameters) { if (parameters.Count == 0) { throw new Exception("Empty"); } IExpressionData data = parameters[0].Evaluate(); string path = ""; if (data is StringType) { path = (string)data.GetData(); } else { throw new Exception("Not string"); } string text = ""; using (StreamReader sr = new StreamReader(path)) text = sr.ReadToEnd(); return(new StringType(text)); }
public IExpressionData Evaluate() { IExpressionData leftData = leftExpression.Evaluate(); IExpressionData rightData = rightExpression.Evaluate(); if (leftData.GetType() != rightData.GetType()) { throw new Exception("Operation on different types"); } if (Operation == "&" || Operation == "|") { if (leftData.GetType() != typeof(BoolType)) { throw new Exception("Boolen operation on none boolen variable"); } bool left = (bool)leftData.GetData(); bool right = (bool)rightData.GetData(); if (Operation == "&" && (left && right)) { return(new BoolType(true)); } else if (Operation == "|" && (left || right)) { return(new BoolType(true)); } else { return(new BoolType(false)); } } else if (Operation == ">" || Operation == "<" || Operation == "==" || Operation == "<=" || Operation == ">=" || Operation == "!=") { if (leftData.GetType() == typeof(BoolType)) { throw new Exception("Not boolen operation on boolen type"); } if (leftData.GetType() == typeof(StringType)) { if (Operation == "==") { if ((string)leftData.GetData() == (string)rightData.GetData()) { return(new BoolType(true)); } else { return(new BoolType(false)); } } else if (Operation == "!=") { if ((string)leftData.GetData() != (string)rightData.GetData()) { return(new BoolType(true)); } else { return(new BoolType(false)); } } else { throw new Exception("Cant use this operation on string"); } } else if (leftData.GetType() == typeof(DoubleType)) { double left = (double)leftData.GetData(); double right = (double)rightData.GetData(); bool result = false; switch (Operation) { case ">": if (left > right) { result = true; } break; case "<": if (left < right) { result = true; } break; case ">=": if (left >= right) { result = true; } break; case "<=": if (left <= right) { result = true; } break; case "==": if (left == right) { result = true; } break; case "!=": if (left != right) { result = true; } break; } return(new BoolType(result)); } else { throw new Exception("Unknown data type"); } } else { throw new Exception("Unknown operation in Condition expression"); } }