public override IObject GetNewInstans(IObject[] objs) { if (objs.Length == 1 && objs[0].IType == IObjectType.I_String) return new I_String(((I_String) objs[0]).VALUE); return new I_Error("Can not convert to String"); }
public override IObject MethodOperator(IObject[] strParams) { if(strParams.Length < 1) return new IObject(); double retValue = 0; for (int i = 0; i < strParams.Length; ++i) { try { switch (strParams[i].IType) { case IObjectType.I_Float: retValue += ((I_Float)strParams[i]).VALUE; break; case IObjectType.I_Int: retValue += ((I_Int)strParams[i]).VALUE; break; } } catch { } } return new I_Float(retValue / strParams.Length); }
public void AddIObjectWithMembers(string name, IObject iobj) { if (preDefinedIObjects.ContainsKey(name)) preDefinedIObjects.Remove(name); preDefinedIObjects.Add(name, iobj); }
protected void AddMember(string str, IObject iobj) { if (members.ContainsKey(str)) members.Remove(str); members.Add(str, iobj); }
public IObject AddOrUpdateUserDefinedIObject(string name, IObject getValue) { if (userdefinedIObjects.ContainsKey(name)) userdefinedIObjects[name] = getValue; else userdefinedIObjects.Add(name, getValue); return getValue; }
public override IObject MethodOperator(IObject[] strParams) { if (strParams.Length == 1) { double value = Compiler.I_Number.ToDouble(strParams[0]); return new I_Float(Math.Sin(value)); } return new I_Error("Wrong number of arguments."); }
public override IObject GetNewInstans(IObject obj) { switch (obj.IType) { case IObjectType.I_Int: return new I_Int((I_Int)obj); default: return new I_Error("Can not create copy."); } }
public override IObject MethodOperator(IObject[] strParams) { if(strParams.Length != 4) return new IObject(); if (strParams[0].EqualEqualOperator(strParams[1])) { return strParams[2]; } return strParams[3]; }
public override IObject MethodOperator(IObject[] strParams) { if(strParams.Length < 2) return new IObject(); IObject minValue = strParams[0]; for (int i = 1; i < strParams.Length; ++i) { if (minValue.LargerOperator(strParams[i])) minValue = strParams[i]; } return minValue; }
public override IObject EqualOperator(IObject iobj) { switch (iobj.IType) { case IObjectType.I_Float: value = (float)((I_Float)iobj).VALUE; return this; case IObjectType.I_Int: value = ((I_Int)iobj).VALUE; return this; } value = 0; return this; }
public static string ToString(IObject obj) { switch (obj.IType) { case IObjectType.I_Float: return ((I_Float)obj).VALUE.ToString(); case IObjectType.I_Int: return ((I_Int)obj).VALUE.ToString(); case IObjectType.I_String: return ((I_String)obj).VALUE.ToString(); default: return ""; } }
public override IObject GetNewInstans(IObject[] objs) { if (objs.Length == 1) { switch (objs[0].IType) { case IObjectType.I_Float: return new I_Int((int)((I_Float)objs[0]).VALUE); case IObjectType.I_Int: return new I_Int(((I_Int)objs[0]).VALUE); } } return new I_Error("Can not convert to Int32"); }
public override bool EqualEqualOperator(IObject iobj) { switch (iobj.IType) { case IObjectType.I_Float: if (((I_Float)iobj).VALUE == this.VALUE) return true; break; case IObjectType.I_Int: if (((I_Int)iobj).VALUE == this.VALUE) return true; break; } return false; }
public override IObject DividerOperator(IObject rightSide) { try { switch (rightSide.IType) { case IObjectType.I_Float: return new I_Float(value / ((I_Float)rightSide).VALUE); case IObjectType.I_Int: return new I_Float(value / ((I_Int)rightSide).VALUE); default: return new I_Float(value); } } catch { } return new I_Float(value); }
public virtual bool LessOperator(IObject rightSide) { return false; }
public virtual IObject FactorialOperator(IObject rightSide) { return this; }
public virtual IObject EqualOperator(IObject iobj) { return this; }
public virtual bool EqualEqualOperator(IObject iobj) { return false; }
public virtual IObject DividerOperator(IObject rightSide) { return this; }
public virtual IObject PlusOperator(IObject rightSide) { return this; }
public virtual IObject MultiplierOperator(IObject rightSide) { return this; }
public virtual IObject ModuloOperator(IObject rightSide) { return this; }
public override IObject MethodOperator(IObject[] strParams) { if (strParams.Length != 2) return new I_Error("Number of arguments must be 2. x.ModPower(Int exp, Int n) -> (x ^ exp) % n"); try { return new I_Int(referens.BIG_VALUE.modPow(((I_Int)strParams[0]).BIG_VALUE, ((I_Int)strParams[1]).BIG_VALUE)); } catch { return new I_Error("No invers found!"); } }
public override IObject PowerOperator(IObject rightSide) { switch (rightSide.IType) { case IObjectType.I_Float: double value = ((I_Float)rightSide).VALUE; if(value == 0.5f) return new I_Int(bigValue.sqrt()); return new I_Float(Math.Pow(VALUE, value)); case IObjectType.I_Int: try { BigInteger tmp = bigValue.Power((uint)((I_Int)rightSide)); return new I_Int(tmp); } catch { return new I_Error("Overflow"); } default: return new I_Error("Calculation error"); } }
private IObject ParsString(Dictionary<string, IObject> scope, string str) { if (str == null || str == "" || str == " ") return new IObject(); if (SimpleMethodCall.IsMatch(str)) { string methodName = SimpleMethodCall.Match(str).Groups[1].Value; if (scope.ContainsKey(methodName)) { string methodArgsStr = SimpleMethodCall.Match(str).Groups[2].Value; string[] inParams = Tools.SplitArgs(methodArgsStr); List<IObject> inIObjectParams = new List<IObject>(); for (int i = 0; i < inParams.Length; ++i) { IObject obj = ParsString(scope, inParams[i]); if (!(obj.IType == IObjectType.I_Error || obj.IType == IObjectType.I_Null)) inIObjectParams.Add(obj); } if (scope.ContainsKey(methodName)) { IObject methodObj = GetIObjectFromScope(scope, methodName); IObject ret = methodObj.MethodOperator(inIObjectParams.ToArray()); return ret; } } } else if(SimpleString.IsMatch(str)) { IObject ret = new I_String(SimpleString.Match(str).Groups[1].Value); return ret; } else if (SimpleVariable.IsMatch(str)) { string variableName = SimpleVariable.Match(str).Groups[1].Value; if (scope.ContainsKey(variableName)) { IObject ret = GetIObjectFromScope(scope, variableName); return ret; } if (userdefinedIObjects.ContainsKey(variableName)) { IObject ret = userdefinedIObjects[variableName]; return ret; } } else if (SimpleInteger.IsMatch(str)) { string integerValueStr = SimpleInteger.Match(str).Groups[1].Value; return new I_Int(integerValueStr); } else if (SimpleFloat.IsMatch(str)) { string floatValueStr = SimpleFloat.Match(str).Groups[1].Value; floatValueStr = floatValueStr.Replace('.', ','); if (floatValueStr.Length < 10) { IObject ret = new I_Float(Double.Parse(floatValueStr)); return ret; } else { try { IObject ret = new I_Float(double.Parse(floatValueStr)); return ret; } catch { IObject ret = new I_Error("Float to big. Must be in range (10^k where |k| < 308)."); return ret; } } } else if (SimpleDeclaration.IsMatch(str)) { string type = SimpleDeclaration.Match(str).Groups[1].Value; string name = SimpleDeclaration.Match(str).Groups[2].Value; string operand = SimpleDeclaration.Match(str).Groups[3].Value; string value = SimpleDeclaration.Match(str).Groups[4].Value; if (!types.ContainsKey(type)) return new I_Error("Type not recognized."); IObject_Constructor ioc = types[type]; if (value == "") { if (operand == "") { IObject newInstanse = ioc.GetNewInstans(); IObject ret = AddOrUpdateUserDefinedIObject(name, newInstanse); return ret; } else { variableBracket.Name = name; variableBracket.IType = ioc.GetObjectType(); return new IObject(); } } else { IObject getValue = ParsString(scope, value); IObject[] objInParams = { getValue }; IObject newValue = ioc.GetNewInstans(objInParams); if (newValue.IType == IObjectType.I_Error) { IObject ret = newValue; return ret; } else { IObject ret = AddOrUpdateUserDefinedIObject(name, newValue); return ret; } } } else if (SimpleEqual.IsMatch(str)) { string variableName = SimpleEqual.Match(str).Groups[1].Value; string variableValueStr = SimpleEqual.Match(str).Groups[2].Value; if (userdefinedIObjects.ContainsKey(variableName)) { // The variable exists, need to do a new version of it. IObject compiledValue = ParsString(scope, variableValueStr); IObject previousVersionValue = GetUserDefinedIObject(variableName); IObject newVersionCopy; switch (compiledValue.IType) { case IObjectType.I_Null: newVersionCopy = new IObject(); break; case IObjectType.I_Error: newVersionCopy = new I_Error(((I_Error)compiledValue)); break; default: newVersionCopy = iTypes.GetConstructor(compiledValue.IType).GetNewInstans(compiledValue); break; } AddOrUpdateUserDefinedIObject(variableName, newVersionCopy); newVersionCopy.EqualOperator(compiledValue); return newVersionCopy; } else { IObject compiledValue = ParsString(scope, variableValueStr); AddOrUpdateUserDefinedIObject(variableName, compiledValue); return compiledValue; } } else if (SimpleEqualBracket.IsMatch(str)) { string variableName = SimpleEqualBracket.Match(str).Groups[1].Value; variableBracket.Name = variableName; return new IObject(); } else if (SimpleDotSeparator.IsMatch(str)) { string memberName = SimpleDotSeparator.Match(str).Groups[1].Value; string rest = SimpleDotSeparator.Match(str).Groups[2].Value; Dictionary<string, IObject> newScope = ParsString(scope, memberName).GetMembers(); if (newScope.Count == 0) { newScope = ParsString(preDefinedIObjects, memberName).GetMembers(); } if (newScope.Count == 0 && types.ContainsKey(memberName)) { newScope = types[memberName].GetMembers(); } IObject ret = ParsString(newScope, rest); return ret; } if (operandTree.ContainOperands(str)) { return operandTree.ParseOperands(str, scope); } if (SimpleContainsParentheses.IsMatch(str)) { IObject ret = ParsParentheses(scope, str); if (ret.IType != IObjectType.I_Null) { return ret; } } IObject finalReturn = new I_Error("Command not understood (" + str + ")"); return finalReturn; }
public void SetVarAsIObjectIfExist(string name, IObject obj) { if (userdefinedIObjects.ContainsKey(name)) userdefinedIObjects[name] = obj; }
private IObject DoOperand(IObject leftSide, char operand, IObject rightSide) { if (operand == '+') return leftSide.PlusOperator(rightSide); else if (operand == '-') return leftSide.MinusOperator(rightSide); else if (operand == '*') return leftSide.MultiplierOperator(rightSide); else if (operand == '/') return leftSide.DividerOperator(rightSide); else if (operand == '^') return leftSide.PowerOperator(rightSide); else return new IObject(); }
private IObject DoOperand(IObject leftSide, string operand, IObject rightSide) { if (operand == "+") return leftSide.PlusOperator(rightSide); else if (operand == "-") return leftSide.MinusOperator(rightSide); else if (operand == "*") return leftSide.MultiplierOperator(rightSide); else if (operand == "/") return leftSide.DividerOperator(rightSide); else if (operand == "^") return leftSide.PowerOperator(rightSide); else return new IObject(); }
private IObject ParsParentheses(Dictionary<string, IObject> scope, string str) { char[] ca = str.ToCharArray(); Int32 startP = -1; Int32 endP = -1; Int32 depth = -1; for (int i = 0; i < ca.Length; ++i) { if (ca[i] == '(') { if(i > 0) if (ca[i - 1] != ' ') // Borde va okej med +-*/^ inte bara blanksteg return new IObject(); depth = 1; startP = i; break; } } for (int i = startP+1; i < ca.Length; ++i) { if (ca[i] == '(') ++depth; else if (ca[i] == ')') --depth; if (depth == 0) { endP = i; break; } } if(depth != 0) return new IObject(); IObject middlePart = ParsString(scope, new string(ca, startP + 1, endP - startP - 1)); int leftPrio = 0; int leftOperandIndex = -1; for (int i = startP-1; i > -1; --i) { if (OperandPrio(ca[i]) != 0) { leftPrio = OperandPrio(ca[i]); leftOperandIndex = i; break; } } int rightPrio = 0; int rightOperandIndex = -1; for (int i = endP + 1; i < ca.Length; ++i) { if (OperandPrio(ca[i]) != 0) { rightPrio = OperandPrio(ca[i]); rightOperandIndex = i; break; } } if (leftOperandIndex == -1 && rightOperandIndex == -1) { return middlePart; } IObject leftHand = new IObject(); if (leftOperandIndex - 1 > 0) leftHand = ParsString(scope, new string(ca, 0, leftOperandIndex - 1)); else if (startP - 1 > 0) leftHand = ParsString(scope, new string(ca, 0, startP - 1)); IObject rightHand = new IObject(); if (rightOperandIndex + 1 < ca.Length-1 && rightOperandIndex > endP) rightHand = ParsString(scope, new string(ca, rightOperandIndex + 1, ca.Length - 1 - rightOperandIndex)); else if (endP + 1 < ca.Length-1) rightHand = ParsString(scope, new string(ca, endP + 1, ca.Length - 1 - endP)); if (leftPrio < rightPrio && rightPrio > 0) { IObject firstResult = DoOperand(middlePart, new string(ca[rightOperandIndex], 1), rightHand); if (leftPrio > 0) return DoOperand(leftHand, new string(ca[leftOperandIndex], 1), firstResult); return firstResult; } else if (leftPrio >= rightPrio && leftPrio > 0) { IObject firstResult = DoOperand(leftHand, new string(ca[leftOperandIndex], 1), middlePart); if (rightPrio > 0) return DoOperand(firstResult, new string(ca[rightOperandIndex], 1), rightHand); return firstResult; } return middlePart; }
public override IObject MethodOperator(IObject[] strParams) { if (strParams.Length != 1) return new I_Error("Number of arguments must be 1."); try { return new I_Int(referens.BIG_VALUE.modInverse(((I_Int)strParams[0]).BIG_VALUE)); } catch { return new I_Error("No invers found!"); } }