public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); object right = RightChildExpression.Calculate(context, this); if (left == null && right == null) return ""; if (left == null && right.GetType() == typeof(string)) return right; if (right == null && left.GetType() == typeof(string)) return left; left = left ?? ""; right = right ?? ""; if (left.GetType() == typeof(string) || right.GetType() == typeof(string)) return left.ToString() + right.ToString(); if (left.IsNumeric() && right.IsNumeric()) { if (left.GetType() == typeof(int) && right.GetType() == typeof(int)) return (int)left + (int)right; else return Decimal.Add((decimal)left, (decimal)right); } return left.ToString() + right.ToString(); }
public CinarDebugger(Context context) { InitializeComponent(); this.debugStartContext = context; editor.Text = Context.code; editor.Document.ReadOnly = true; editor.ActiveTextAreaControl.TextArea.KeyDown += new KeyEventHandler(TextArea_KeyDown); }
public void SetMarker(Context context) { editor.Document.MarkerStrategy.RemoveAll(m => m != null); if (Context.CurrentStatement != null) { statusBarLabel.Text = Context.CurrentStatement.ToString().Split('\n')[0].Trim(); txtOutput.Text = debugStartContext.RootContext.Interpreter.Output; LineSegment seg = editor.Document.GetLineSegment(Context.CurrentStatement.LineNumber); TextMarker marker = new TextMarker(seg.Offset + Context.CurrentStatement.ColumnNumber, seg.Length - Context.CurrentStatement.ColumnNumber, TextMarkerType.SolidBlock, Color.Yellow); editor.Document.MarkerStrategy.AddMarker(marker); editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(seg.Offset); tree.Nodes.Clear(); while (context.parent != null) { foreach (object key in context.Variables.Keys) addNode(null, key.ToString(), context.Variables[key]); context = context.parent; } Application.DoEvents(); this.Refresh(); } }
public abstract object Calculate(Context context, ParserNode parentNode);
public void SetValue(object obj, object val, Context context) { if (RightChildExpression is MemberAccess) (RightChildExpression as MemberAccess).SetValue(LeftChildExpression.Calculate(context, this), val, context); else if (RightChildExpression is Variable) { if (obj == null) throw new Exception(LeftChildExpression + " is null"); MemberInfo[] members = obj.GetType().GetMember((RightChildExpression as Variable).Name); if (members == null || members.Length == 0) { try { obj.SetIndexedValue((RightChildExpression as Variable).Name, val); } catch { obj.SetIndexedValue((RightChildExpression as Variable).Calculate(context, this), val); } } else { MemberInfo mi = members[0]; if (mi is FieldInfo) (mi as FieldInfo).SetValue(obj, val.ChangeType((mi as FieldInfo).FieldType)); else if (mi is PropertyInfo) (mi as PropertyInfo).SetValue(obj, val.ChangeType((mi as PropertyInfo).PropertyType), null); } } else { obj.SetIndexedValue(RightChildExpression.Calculate(context, this), val); } }
public override object Calculate(Context context, ParserNode parentNode) { object val = nullableExp.Calculate(context, this); if (val == null || val.Equals("")) return notNullExp.Calculate(context, this); else return val; }
public override object Calculate(Context context, ParserNode parentNode) { return fValue; }
public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); object right = RightChildExpression.Calculate(context, this); if(left!=null && left.GetType()==typeof(string)) return left.ToString()[Convert.ToInt32(right)]; return left.GetIndexedValue(right); }
public override object Calculate(Context context, ParserNode parentNode) { return context.GetVariableValue(fName); }
public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); object right = RightChildExpression.Calculate(context, this); left = left ?? Decimal.Zero; right = right ?? Decimal.Zero; if (!left.IsNumeric()) { decimal res = decimal.Zero; Decimal.TryParse(left.ToString(), out res); left = res; } if (!right.IsNumeric()) { decimal res = decimal.Zero; Decimal.TryParse(right.ToString(), out res); right = res; } var result = Decimal.Subtract(Convert.ToDecimal(left), Convert.ToDecimal(right)); if (left.GetType() == typeof(int) && right.GetType() == typeof(int)) return (int)result; else return result; }
public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); bool bLeft = AndExpression.ParseBool(left); if (bLeft) return true; object right = RightChildExpression.Calculate(context, this); return AndExpression.ParseBool(right); }
public override object Calculate(Context context, ParserNode parentNode) { object val = ChildExpression.Calculate(context, this); if (val == null) return true; else if (val.Equals(DBNull.Value)) return true; else if (val.GetType() == typeof(bool)) return !((bool)val); else if (val.GetType() == typeof(string)) return val.ToString().Trim() == ""; else if (val.Equals(0)) return true; else if (val.Equals(0f)) return true; else if (val.Equals(0d)) return true; else if (val.Equals(0m)) return true; else return false; }
public override object Calculate(Context context, ParserNode parentNode) { FunctionCall fc = (FunctionCall)ChildExpression; string className = fc.Name; Type t = Context.GetType(className, context.Using); if (t == null) throw new Exception("Undefined type: " + className); object[] parameters = null; if (fc.Arguments.Length > 0) { parameters = new object[fc.Arguments.Length]; for (int i = 0; i < fc.Arguments.Length; i++) parameters[i] = fc.Arguments[i].Calculate(context, this); } if (parameters == null) return Activator.CreateInstance(t); else return Activator.CreateInstance(t, parameters); }
public override object Calculate(Context context, ParserNode parentNode) { object val = ChildExpression.Calculate(context, this); if (val == null) return 0; else if (val.GetType() == typeof(int)) return -1 * (int)val; else if (val.GetType() == typeof(long)) return -1 * (long)val; else if (val.GetType() == typeof(decimal)) return -1 * (decimal)val; else if (val.GetType() == typeof(float)) return -1 * (float)val; else if (val.GetType() == typeof(double)) return -1 * (float)val; else return 0; }
public override object Calculate(Context context, ParserNode parentNode) { if (Name == "write" || Name == "print" || Name == "echo") { if (fArguments.Length > 1 && fArguments[0] is StringConstant) { string s = fArguments[0].Calculate(context, this).ToString(); object[] objParams = new object[fArguments.Length-1]; for(int i=1; i<fArguments.Length; i++) objParams[i - 1] = fArguments[i].Calculate(context, this).ToString(); context.Output.Write(String.Format(s, objParams)); } else foreach (Expression expression in fArguments) context.Output.Write(expression.Calculate(context, this)); } else if (Name == "eval" && fArguments.Length>0) { Interpreter ip = new Interpreter("$" + fArguments[0].Calculate(context, this) + "$", null); ip.Parse(); ip.Execute(); return ip.Output; } else if (Name == "typeof") { if (fArguments.Length == 0 || !(fArguments[0] is Variable)) throw new Exception("typeof operator argument error. Usage: typeof(DateTime)"); string typeName = (fArguments[0] as Variable).Name; return Context.GetType(typeName, context.Using); } else if (context.Functions[Name] != null) { FunctionDefinitionStatement func = (FunctionDefinitionStatement)context.Functions[Name]; Hashtable arguments = new Hashtable(); int i = 0; foreach (Expression expression in fArguments) { object paramVal = expression.Calculate(context, this); if (func.Parameters.Count > i) arguments[func.Parameters[i]] = paramVal; i++; } func.Block.Execute(context, this, arguments); object res = Context.ReturnValue; Context.ReturnValue = null; return res; } else throw new Exception("Undefined function: " + this); return null; }
public override object Calculate(Context context, ParserNode parentNode) { return AndExpression.ParseBool(boolExp.Calculate(context, this)) ? trueExpression.Calculate(context, this) : falseExpression.Calculate(context, this); }
public override object Calculate(Context context, ParserNode parentNode) { object res = context.GetVariableValue(fVar.Name); context.SetVariableValue(fVar.Name, new Addition(Var, new IntegerConstant(Amount)).Calculate(context, this)); return res; }
public override object Calculate(Context context, ParserNode parentNode) { object res = LeftChildExpression.Calculate(context, this); object val = RightChildExpression.Calculate(context, this); if (val.IsNumeric() && res.GetType() == typeof(string)) res = res.ToString()[Convert.ToInt32(val)]; else res = res.GetIndexedValue(val); return res; }
public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); object right = RightChildExpression.Calculate(context, this); if (left.IsNumeric() && right.IsNumeric()) return ((int)left) ^ ((int)right); else return AndExpression.ParseBool(left) ^ AndExpression.ParseBool(right); }
public override object Calculate(Context context, ParserNode parentNode) { object left = Exp.Calculate(context, this); Type type = Context.GetType(typeName, context.Using); if (type == null) throw new Exception("There is no such type name: " + typeName); return left == null ? false : (left.GetType() == type); }
public override object Calculate(Context context, ParserNode parentNode) { object val = ChildExpression.Calculate(context, this); if (val == null) throw new Exception("null cannot be the operand of a bitwise complement (~)"); if (val.GetType() == typeof(int)) return ~((int)val); if (val.GetType() == typeof(uint)) return ~((uint)val); if (val.GetType() == typeof(long)) return ~((long)val); if (val.GetType() == typeof(ulong)) return ~((ulong)val); throw new Exception(val + " cannot be the operand of a bitwise complement (~)"); }
public override object Calculate(Context context, ParserNode parentNode) { object res = LeftChildExpression.Calculate(context, this); // eğer sonuç null ve LeftExp variable ise static call olabilir if (res == null && LeftChildExpression is Variable) { string className = (LeftChildExpression as Variable).Name; Type t = Context.GetType(className, context.Using); if (t != null) { if (RightChildExpression is Variable) { string propName = (RightChildExpression as Variable).Name; PropertyInfo pi = t.GetProperty(propName, BindingFlags.Static | BindingFlags.Public); if (pi != null) res = pi.GetValue(null, null); else { FieldInfo fi = t.GetField(propName); if (fi != null) res = fi.GetValue(null); else res = ""; } } else if (RightChildExpression is FunctionCall) { FunctionCall fc = (FunctionCall)RightChildExpression; object[] paramValues = new object[fc.Arguments.Length]; Type[] paramTypes = new Type[fc.Arguments.Length]; int i = 0; foreach (Expression exp in fc.Arguments) { paramValues[i] = exp.Calculate(context, this); paramTypes[i] = paramValues[i]==null ? null : paramValues[i].GetType(); i++; } //MethodInfo mi = t.GetMethod(fc.Name, BindingFlags.Static | BindingFlags.Public, null, paramTypes, null); MethodInfo mi = null; ParameterInfo[] arrPi; FindMethod(t, fc.Name, paramTypes, out mi, out arrPi, BindingFlags.Static | BindingFlags.Public); if (mi == null) throw new Exception("Undefined param types for static method: " + this); else res = mi.Invoke(null, paramValues); } else if (RightChildExpression is MemberAccess) { res = new MemberAccess(new MemberAccess(this.LeftChildExpression, (this.RightChildExpression as MemberAccess).LeftChildExpression), (this.RightChildExpression as MemberAccess).RightChildExpression).Calculate(context, this); } else res = null; } else throw new Exception("Undefined static method: " + this); } else if (RightChildExpression is MemberAccess) { res = new MemberAccess(new MemberAccess(this.LeftChildExpression, (this.RightChildExpression as MemberAccess).LeftChildExpression), (this.RightChildExpression as MemberAccess).RightChildExpression).Calculate(context, this); } else if (RightChildExpression is FunctionCall) { FunctionCall fc = (FunctionCall)RightChildExpression; List<object> paramValues = new List<object>(); List<Type> paramTypes = new List<Type>(); int i = 0; foreach (Expression exp in fc.Arguments) { paramValues.Add(exp.Calculate(context, this)); paramTypes.Add(paramValues[i] == null ? typeof(object) : paramValues[i].GetType()); i++; } if (res is string) { switch (fc.Name.ToLowerInvariant()) { case "split": return res.ToString().Split(new string[] { paramValues[0].ToString() }, StringSplitOptions.RemoveEmptyEntries); } } //MethodInfo mi = res.GetType().GetMethod(fc.Name, paramTypes); MethodInfo mi = null; ParameterInfo[] pinfo = null; FindMethod(res.GetType(), fc.Name, paramTypes.ToArray(), out mi, out pinfo); if (mi == null) throw new Exception("Undefined method: " + this); else { if (mi.GetParameters().Length > paramTypes.Count) { paramTypes.Add(typeof(object[])); paramValues.Add(new object[0]); } for (int j = 0; j < paramTypes.Count; j++) { try { if(!paramTypes[j].IsSubclassOf(pinfo[j].ParameterType)) paramValues[j] = paramValues[j].ChangeType(pinfo[j].ParameterType); } catch { throw new Exception("Cannot convert from " + paramValues[j].GetType().Name + " to " + pinfo[j].ParameterType.Name + " or undefined method: " + this); } } res = mi.Invoke(res, paramValues.ToArray()); } } else if (RightChildExpression is Variable) { Variable var = (Variable)RightChildExpression; PropertyInfo pi = res.GetType().GetProperty(var.Name); if (pi != null) { try { res = pi.GetValue(res, null); } catch(Exception ex) { throw new Exception("Property found but couldnt be read: " + this + " (" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message) + ")"); } } else { FieldInfo fi = res.GetType().GetField(var.Name); if (fi != null) { try { res = fi.GetValue(res); } catch(Exception ex) { throw new Exception("Field found but couldnt be read: " + this + " (" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message) + ")"); } } else { MethodInfo mi = res.GetType().GetMethod("get_Item", new Type[] { typeof(string) }); if (mi != null) { try { object paramObj = var.Name.ChangeType(mi.GetParameters()[0].ParameterType); res = mi.Invoke(res, new object[] { paramObj }); } catch(Exception ex) { throw new Exception("Indexer found but couldnt be read: " + this + " (" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message) + ")"); } } else throw new Exception("Undefined property, field or indexer parameter: " + this); } } } else { object val = RightChildExpression.Calculate(context, this); if (val.IsNumeric() && res.GetType() == typeof(string)) res = res.ToString()[Convert.ToInt32(val)]; else res = res.GetIndexedValue(val); } return res; }
public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); object right = RightChildExpression.Calculate(context, this); if (left == null && right == null) { left = right = 0; } if (left == null) { if (right.GetType() == typeof(string)) left = ""; else if (right.GetType() == typeof(DateTime)) left = new DateTime(0, 0, 0); else left = 0; } if (right == null) { if (left.GetType() == typeof(string)) right = ""; else if (left.GetType() == typeof(DateTime)) right = new DateTime(1, 1, 1); else right = 0; } try { if (left.GetType() == typeof(string) && right.GetType() != typeof(string)) left = left.ChangeType(right.GetType()); if (right.GetType() == typeof(string) && left.GetType() != typeof(string)) right = right.ChangeType(left.GetType()); } catch { } if (left.IsNumeric() && right.IsNumeric()) { left = Convert.ToDecimal(left); right = Convert.ToDecimal(right); } if (left.GetType() != right.GetType()) { left = left.ToString(); right = right.ToString(); } IComparable leftC = (IComparable)left; switch (Operator) { case ComparisonOperator.Equal: return leftC.CompareTo(right) == 0; case ComparisonOperator.NotEqual: return leftC.CompareTo(right) != 0; case ComparisonOperator.LessThan: return leftC.CompareTo(right) < 0; case ComparisonOperator.GreaterThan: return leftC.CompareTo(right) > 0; case ComparisonOperator.LessThanOrEqual: return leftC.CompareTo(right) < 0 || leftC.CompareTo(right) == 0; case ComparisonOperator.GreaterThanOrEqual: return leftC.CompareTo(right) > 0 || leftC.CompareTo(right) == 0; default: throw new Exception("Undefined comparison operation: " + Operator); } }
public override object Calculate(Context context, ParserNode parentNode) { object left = LeftChildExpression.Calculate(context, this); object right = RightChildExpression.Calculate(context, this); left = left ?? Decimal.Zero; right = right ?? Decimal.One; if (!left.IsNumeric()) { decimal res = decimal.Zero; Decimal.TryParse(left.ToString(), out res); left = res; } if (!right.IsNumeric()) { decimal res = decimal.Zero; Decimal.TryParse(right.ToString(), out res); right = res; } return Decimal.Divide(Convert.ToDecimal(left), Convert.ToDecimal(right)); }
public void Execute(TextWriter output) { context = new Context(); context.Output = output; context.Variables = attributes; context.Variables["true"] = true; context.Variables["false"] = false; context.Variables["null"] = null; context.Using.Add("System"); if(this.Usings!=null) foreach (string nameSpace in this.Usings) if(!string.IsNullOrEmpty(nameSpace)) context.Using.Add(nameSpace); Context.code = this.Code; context.Interpreter = this; Stopwatch watch = new Stopwatch(); watch.Start(); try { StatementCollection coll = new StatementCollection(statements, false); coll.Execute(context, null, null); this.ExecutionSuccessful = true; } catch (Exception ex) { this.ExecutionSuccessful = false; context.Output.Write(ex.Message + (ex.InnerException != null ? " - " + ex.InnerException.Message : "") + " at line " + (Context.CurrentStatement.LineNumber + 1)); } watch.Stop(); this.ExecutingTime = watch.ElapsedMilliseconds; if (Context.debuggerWindow != null) { Context.debugging = false; Context.debuggerWindow.Close(); Context.debuggerWindow = null; } }