protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { if (Array == null) { return(Completion.Exception(Language.NullException, this)); } var a = Array.Execute(enviroment); if (!a.IsValue) { return(a); } if (a.ReturnValue is object[]) { object[] arra = a.ReturnValue as object[]; return(new Completion(arra.Length)); } else if (a.ReturnValue is object[, ]) { object[,] arra = a.ReturnValue as object[, ]; return(new Completion(arra.Length)); } return(Completion.Exception(Language.NotArrayException, Array)); }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { if (Raw == null) { return(new Completion(null)); } int intValue; float floatValue; double doubleValue; bool b; if (Raw.StartsWith("\"") && Raw.EndsWith("\"")) { string str = GetStringLateral(Raw.Substring(1, Raw.Length - 2)); return(new Completion(str)); } if (Raw.StartsWith("'") && Raw.EndsWith("'") && Raw.Length == 3) { return(new Completion(Raw[1])); } if (Raw.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase) || Raw.StartsWith("&H", StringComparison.CurrentCultureIgnoreCase)) { string hex = Raw.Substring(2); uint uintValue; if (uint.TryParse(hex, System.Globalization.NumberStyles.HexNumber, CultureInfo.CurrentCulture, out uintValue)) { return(new Completion(uintValue)); } } if (Raw.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { return(new Completion(true)); } if (Raw.Equals("false", StringComparison.CurrentCultureIgnoreCase)) { return(new Completion(false)); } if (Raw.Equals("null", StringComparison.CurrentCultureIgnoreCase)) { return(new Completion(null)); } if (int.TryParse(Raw, out intValue)) { return(new Completion(intValue)); } if (float.TryParse(Raw, out floatValue)) { return(new Completion(floatValue)); } if (double.TryParse(Raw, out doubleValue)) { return(new Completion(doubleValue)); } if (Boolean.TryParse(Raw, out b)) { return(new Completion(b)); } //if (enviroment.HasValue(Raw)) // return new Completion(enviroment.GetValue(Raw)); return(Completion.Exception(string.Format(Properties.Language.InvalidFormat, Raw), this)); }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { try { List <object> values = new List <object>(); for (int i = 0; i < Args.Count; i++) { if (Args[i] == null) { return(Completion.Exception(Language.NullException, this)); } var c = Args[i].Execute(enviroment); if (!c.IsValue) { return(c); } if (!TypeConverters.IsNumber(c.ReturnValue)) { return(Completion.Exception(Language.NotNumberException, Args[i])); } values.Add(c.ReturnValue); } int methodCount = 0; var ms = typeof(Math).GetMethods(); MethodInfo doubleMethod = null; foreach (var m in ms) { if (m.Name.Equals(MathFunction) && m.GetParameters().Length == Args.Count) { methodCount++; bool isDouble = true; foreach (var p in m.GetParameters()) { if (!p.ParameterType.Equals(typeof(double))) { isDouble = false; break; } } if (isDouble) { doubleMethod = m; } } } if (methodCount < 0) { return(Completion.Exception("no function found", this)); } else if (methodCount == 1) { if (doubleMethod != null) { var ps = doubleMethod.GetParameters(); List <object> fparam = new List <object>(); for (int i = 0; i < Args.Count; i++) { fparam.Add(TypeConverters.GetValue(values[i], ps[i].ParameterType)); } return(new Completion(doubleMethod.Invoke(null, fparam.ToArray()))); } else { MethodInfo method = typeof(Math).GetMethod(MathFunction); var ps = method.GetParameters(); List <object> fparam = new List <object>(); for (int i = 0; i < Args.Count; i++) { fparam.Add(TypeConverters.GetValue(values[i], ps[i].ParameterType)); } return(new Completion(method.Invoke(null, fparam.ToArray()))); } } else if (methodCount == 2) { if (doubleMethod != null) { var ps = doubleMethod.GetParameters(); List <object> fparam = new List <object>(); for (int i = 0; i < Args.Count; i++) { fparam.Add(TypeConverters.GetValue(values[i], ps[i].ParameterType)); } return(new Completion(doubleMethod.Invoke(null, fparam.ToArray()))); } return(Completion.Exception(Language.UnknowException, this)); } else { var T = TypeConverters.GetMaxTypes(values.ToArray()); if (T.Equals(typeof(char))) { return(CallMathFunc <int>(values)); } else if (T.Equals(typeof(int))) { return(CallMathFunc <int>(values)); } else if (T.Equals(typeof(float))) { return(CallMathFunc <float>(values)); } else { return(CallMathFunc <double>(values)); } } }catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(Completion.Exception(e.Message, this)); } }
public Completion Execute(ExecutionEnvironment enviroment) { if (Left == null || Right == null) { return(new Completion("Null Exception", CompletionType.Exception)); } var left = Left.Execute(enviroment); if (left.Type != CompletionType.Value) { return(left); } var right = Right.Execute(enviroment); if (right.Type != CompletionType.Value) { return(right); } Type T = TypeConverters.GetNumberTypes(left.ReturnValue, right.ReturnValue); if (T == null) { return(Completion.Exception("Only nuber can accepted", this)); } if (T.Equals(typeof(int))) { try { var l = TypeConverters.GetValue <int>(left.ReturnValue); var r = TypeConverters.GetValue <int>(right.ReturnValue); switch (Operator) { case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); } return(new Completion(l + r)); } catch (Exception e) { return(new Completion(e.Message, CompletionType.Exception)); } } if (T.Equals(typeof(float))) { try { var l = TypeConverters.GetValue <float>(left.ReturnValue); var r = TypeConverters.GetValue <float>(right.ReturnValue); switch (Operator) { case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); } return(new Completion(l + r)); } catch (Exception e) { return(new Completion(e.Message, CompletionType.Exception)); } } if (T.Equals(typeof(long))) { try { var l = TypeConverters.GetValue <long>(left.ReturnValue); var r = TypeConverters.GetValue <long>(right.ReturnValue); switch (Operator) { case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); } return(new Completion(l + r)); } catch (Exception e) { return(new Completion(e.Message, CompletionType.Exception)); } } { try { var l = TypeConverters.GetValue <double>(left.ReturnValue); var r = TypeConverters.GetValue <double>(right.ReturnValue); switch (Operator) { case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); } return(new Completion(l + r)); } catch (Exception e) { return(new Completion(e.Message, CompletionType.Exception)); } } }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { if (Expression == null) { return(Completion.Exception(Properties.Language.NullException, this)); } var c = Expression.Execute(enviroment); if (c.Type != CompletionType.Value) { return(c); } if (!TypeConverters.IsNumber(c.ReturnValue)) { return(Completion.Exception(Properties.Language.NotNumber, Expression)); } Type t = c.ReturnValue.GetType(); if (t.Equals(typeof(int))) { int v = TypeConverters.GetValue <int>(c.ReturnValue); int old = v; if (Operator == UpdateOperator.Add) { v += 1; } else { v = v - 1; } if (Expression is Identifier) { enviroment.SetValue(((Identifier)Expression).Variable, v); } return(new Completion(IsPrefix ? v : old)); } else if (t.Equals(typeof(float))) { float v = TypeConverters.GetValue <float>(c.ReturnValue); float old = v; if (Operator == UpdateOperator.Add) { v += 1; } else { v = v - 1; } if (Expression is Identifier) { enviroment.SetValue(((Identifier)Expression).Variable, v); } return(new Completion(IsPrefix ? v : old)); } else { double v = TypeConverters.GetValue <double>(c.ReturnValue); double old = v; if (Operator == UpdateOperator.Add) { v += 1; } else { v = v - 1; } if (Expression is Identifier) { enviroment.SetValue(((Identifier)Expression).Variable, v); } return(new Completion(IsPrefix ? v : old)); } }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { if (Step == null) { return(Completion.Void); } double degree = 0; try { var c = Step.Execute(enviroment); if (c.Type != CompletionType.Value) { return(c); } degree = TypeConverters.GetValue <double>(c.ReturnValue); } catch { return(Completion.Exception("Wrong number format", Step)); } bool reflection = false; if (enviroment.HasValue("$$ReflectionOnTouchSide&&") && enviroment.GetValue <bool>("$$ReflectionOnTouchSide&&")) { reflection = true; } Sprite sp = enviroment.GetValue("$$INSTANCE$$") as Sprite; int x = sp.X + (int)(Math.Cos(sp.Direction * Math.PI / 180) * degree); int y = sp.Y + (int)(Math.Sin(sp.Direction * Math.PI / 180) * degree); int direction = sp.Direction; if (reflection) { while (x < 0) { x = -x; direction = 180 - direction; } while (x > CurrentEnviroment.ScreenWidth) { x = CurrentEnviroment.ScreenWidth * 2 - x; direction = 180 - direction; } while (y < 0) { y = -y; direction = -direction; } while (y > CurrentEnviroment.ScreenHeight) { y = CurrentEnviroment.ScreenHeight * 2 - y; direction = -direction; } } sp.X = x; sp.Y = y; sp.Direction = direction; App.Current.Dispatcher.InvokeAsync(new Action(() => { (enviroment.GetValue("$$Player") as ScriptPlayer).DrawScript(); })); return(Completion.Void); }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { DateTime startTime; DateTime finishTime; double orgin_x; double orgin_y; double totalTime; if (X == null) { return(Completion.Void); } double x = 0; try { var c = X.Execute(enviroment); if (c.Type != CompletionType.Value) { return(c); } x = TypeConverters.GetValue <double>(c.ReturnValue); } catch { return(Completion.Exception("Wrong number format", X)); } if (Y == null) { return(Completion.Void); } double y = 0; try { var c = Y.Execute(enviroment); if (c.Type != CompletionType.Value) { return(c); } y = TypeConverters.GetValue <double>(c.ReturnValue); } catch { return(Completion.Exception("Wrong number format", Y)); } if (Time == null) { return(Completion.Void); } double time = 0; try { var c = Time.Execute(enviroment); if (c.Type != CompletionType.Value) { return(c); } time = TypeConverters.GetValue <double>(c.ReturnValue); } catch { return(Completion.Exception("Wrong number format", Time)); } Sprite sp = enviroment.GetValue("$$INSTANCE$$") as Sprite; if (x < 0) { x = 0; } if (x > CurrentEnviroment.ScreenWidth) { x = CurrentEnviroment.ScreenWidth; } if (y < 0) { y = 0; } if (y > CurrentEnviroment.ScreenHeight) { y = CurrentEnviroment.ScreenHeight; } orgin_x = sp.X; orgin_y = sp.Y; startTime = DateTime.Now; finishTime = DateTime.Now.AddSeconds(time); totalTime = (finishTime - startTime).TotalMilliseconds; while (true) { if (DateTime.Now > finishTime) { break; } double duration = (DateTime.Now - startTime).TotalMilliseconds; sp.X = (int)(orgin_x + (x - orgin_x) * duration / totalTime); sp.Y = (int)(orgin_y + (y - orgin_y) * duration / totalTime); App.Current.Dispatcher.InvokeAsync(new Action(() => { (enviroment.GetValue("$$Player") as ScriptPlayer).DrawScript(); })); Thread.Sleep(100); } sp.X = (int)x; sp.Y = (int)y; App.Current.Dispatcher.InvokeAsync(new Action(() => { (enviroment.GetValue("$$Player") as ScriptPlayer).DrawScript(); })); return(Completion.Void); }
public Completion Assign(ExecutionEnvironment enviroment, object value) { if (List == null || Index == null) { return(Completion.Exception("Null Exception", this)); } var a = List.Execute(enviroment); if (!a.IsValue) { return(a); } if (a.ReturnValue is Dictionary <object, object> ) { var i = Index.Execute(enviroment); if (!i.IsValue) { return(i); } if ((i.ReturnValue == null)) { return(Completion.Exception("dictionary key can not be null", Index)); } var iv = i.ReturnValue; Dictionary <object, object> d = a.ReturnValue as Dictionary <object, object>; d[iv] = value; return(new Completion(value)); } else { var i = Index.Execute(enviroment); if (!i.IsValue) { return(i); } if (!(i.ReturnValue is int)) { return(Completion.Exception("Only integer is accepted", Index)); } var iv = (int)i.ReturnValue; if (a.ReturnValue is object[]) { object[] arra = a.ReturnValue as object[]; if (iv < 0 && iv >= arra.Length) { return(Completion.Exception("value " + iv + " is out of array index", Index)); } arra[iv] = value; return(new Completion(value)); } else if (a.ReturnValue is List <object> ) { List <object> arra = a.ReturnValue as List <object>; if (iv < 0 && iv >= arra.Count) { return(Completion.Exception("value " + iv + " is out of list index", Index)); } arra[iv] = value; return(new Completion(value)); } return(Completion.Exception("value is not an array, list or dictionary", List)); } }
public Completion Assign(ExecutionEnvironment enviroment, object value) { if (Array == null || Index1 == null || Index2 == null) { return(Completion.Exception(Language.NullException, this)); } var a = Array.Execute(enviroment); if (!a.IsValue) { return(a); } if (!(a.ReturnValue is object[, ])) { return(Completion.Exception(Language.NotNumberException, Array)); } object[,] arra = a.ReturnValue as object[, ]; var i = Index1.Execute(enviroment); if (!i.IsValue) { return(i); } if (!(i.ReturnValue is int)) { return(Completion.Exception(Language.NotNumberException, Index1)); } var iv = (int)i.ReturnValue; var r = Index2.Execute(enviroment); if (!r.IsValue) { return(r); } if (!(r.ReturnValue is int)) { return(Completion.Exception(Language.NotNumberException, Index2)); } var jv = (int)r.ReturnValue; if (iv < 0 && iv >= arra.GetLength(0)) { return(Completion.Exception(Language.IndexOutOfRangeException, Index1)); } if (jv < 0 && jv >= arra.GetLength(1)) { return(Completion.Exception(Language.IndexOutOfRangeException, Index2)); } try { arra[iv, jv] = value; } catch (Exception e) { return(Completion.Exception(e.Message, this)); } return(new Completion(value)); }
public Completion Assign(ExecutionEnvironment enviroment, object value) { if (Array == null || Index == null) { return(Completion.Exception(Language.NullException, this)); } var a = Array.Execute(enviroment); if (!a.IsValue) { return(a); } if (a.ReturnValue is Dictionary <object, object> ) { var i = Index.Execute(enviroment); if (!i.IsValue) { return(i); } if ((i.ReturnValue == null)) { return(Completion.Exception(Language.ValueNullException, Index)); } var iv = i.ReturnValue; Dictionary <object, object> d = a.ReturnValue as Dictionary <object, object>; d[iv] = value; return(new Completion(value)); } else { var i = Index.Execute(enviroment); if (!i.IsValue) { return(i); } if (!(i.ReturnValue is int)) { return(Completion.Exception(Language.NotNumberException, Index)); } var iv = (int)i.ReturnValue; if (a.ReturnValue is object[]) { object[] arra = a.ReturnValue as object[]; if (iv < 0 && iv >= arra.Length) { return(Completion.Exception(Language.IndexOutOfRangeException, Index)); } arra[iv] = value; return(new Completion(value)); } else if (a.ReturnValue is List <object> ) { List <object> arra = a.ReturnValue as List <object>; if (iv < 0 && iv >= arra.Count) { return(Completion.Exception(Language.IndexOutOfRangeException, Index)); } arra[iv] = value; return(new Completion(value)); } return(Completion.Exception(Language.NotCollectionException, Array)); } }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { /* * if (DelegateFunction != null) * { * List<object> ps = new List<object>(); * for (int i = 0; i < Args.Count; i++) * { * Expression e = Args[i]; * string name = ArgTyps[i]; * * Completion cp = e.Execute(enviroment); * if (cp.Type != CompletionType.Value) * return cp; * ps.Add(cp.ReturnValue); * } * DelegateFunction.Invoke(ps.ToArray()); * return Completion.Void; * }*/ foreach (var f in enviroment.Module.Functions) { if (Function.Equals(f.Name)) { ExecutionEnvironment current = new ExecutionEnvironment(enviroment.GetInstanceEnvironment()); for (int i = 0; i < Args.Count; i++) { Expression e = Args[i]; string name = ArgTyps[i]; Completion cp = e.Execute(enviroment); if (cp.Type != CompletionType.Value) { return(cp); } current.RegisterValue(f.Params[i].Name, cp.ReturnValue); } var c = f.Execute(current); return(c); } } DelegateFunction func = enviroment.GetFunction(Function); List <object> parameters = new List <object>(); if (func != null) { try { for (int i = 0; i < Args.Count; i++) { Expression e = Args[i]; string name = ArgTyps[i]; if (e == null) { return(Completion.Exception(Properties.Language.ParameterNullException, this)); } Completion cp = e.Execute(enviroment); if (cp.Type != CompletionType.Value) { return(cp); } parameters.Add(cp.ReturnValue); } return(new Completion(func.Invoke(parameters.ToArray()))); } catch (Exception e) { return(Completion.Exception(e.Message, this)); } } return(Completion.Void); }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { if (Left == null) { return(Completion.Exception(Properties.Language.NullException, this)); } if (Right == null) { return(Completion.Exception(Properties.Language.NullException, this)); } var left = Left.Execute(enviroment); if (left.Type != CompletionType.Value) { return(left); } var right = Right.Execute(enviroment); if (right.Type != CompletionType.Value) { return(right); } if (IsLogicalOperator(Operator)) { return(ExecuteLogical(enviroment, left.ReturnValue, right.ReturnValue)); } if (Operator == Operator.Add) { if ((left.ReturnValue is string) || right.ReturnValue is string) { return(ExecuteStringAdd(enviroment, left.ReturnValue, right.ReturnValue)); } } if (Operator == Operator.Equal) { if (left.ReturnValue is string) { return(new Completion(left.ReturnValue is string && (left.ReturnValue as string).Equals(right.ReturnValue))); } if (right.ReturnValue is string) { return(new Completion(right.ReturnValue is string && (right.ReturnValue as string).Equals(left.ReturnValue))); } if (left.ReturnValue is bool) { return(new Completion(right.ReturnValue is bool && (bool)right.ReturnValue == (bool)left.ReturnValue)); } if (right.ReturnValue is bool) { return(new Completion(left.ReturnValue is bool && (bool)left.ReturnValue == (bool)right.ReturnValue)); } if (left.ReturnValue == null) { if (right.ReturnValue == null) { return(new Completion(true)); } else { return(new Completion(false)); } } if (right.ReturnValue == null) { if (left.ReturnValue == null) { return(new Completion(true)); } return(new Completion(false)); } } if (Operator == Operator.NotEqual) { if (left.ReturnValue is string) { return(new Completion(!(left.ReturnValue as string).Equals(right.ReturnValue))); } if (right.ReturnValue is string) { return(new Completion(!(right.ReturnValue as string).Equals(left.ReturnValue))); } if (left.ReturnValue is bool) { return(new Completion(!(right.ReturnValue is bool) || (bool)right.ReturnValue != (bool)left.ReturnValue)); } if (right.ReturnValue is bool) { return(new Completion(!(left.ReturnValue is bool) || (bool)left.ReturnValue != (bool)right.ReturnValue)); } if (left.ReturnValue == null) { if (right.ReturnValue != null) { return(new Completion(true)); } else { return(new Completion(false)); } } if (right.ReturnValue == null) { if (left.ReturnValue != null) { return(new Completion(true)); } return(new Completion(false)); } } if (Operator == Operator.Add) { if ((left.ReturnValue is DateTime) && right.ReturnValue is DateTime) { return(ExecuteStringAdd(enviroment, left.ReturnValue, right.ReturnValue)); } } Type T = TypeConverters.GetMaxTypes(left.ReturnValue, right.ReturnValue); if (T == null) { return(Completion.Exception(Properties.Language.NotNumber, this)); } if (Operator == Operator.Mod || Operator == Operator.BitAnd || Operator == Operator.BitOr || Operator == Operator.BitLeftShift || Operator == Operator.BitRightShift || Operator == Operator.BitExclusiveOr) { T = typeof(int); } if (T.Equals(typeof(char))) { try { var l = TypeConverters.GetValue <char>(left.ReturnValue); var r = TypeConverters.GetValue <char>(right.ReturnValue); switch (Operator) { case Operator.Add: return(new Completion(l + r)); case Operator.Minus: return(new Completion(l - r)); case Operator.Mulitiply: return(new Completion(l * r)); case Operator.Divide: return(new Completion(l / r)); case Operator.Mod: return(new Completion(l % r)); case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); case Operator.NotEqual: return(new Completion(l != r)); case Operator.BitAnd: return(new Completion(l & r)); case Operator.BitOr: return(new Completion(l | r)); case Operator.BitLeftShift: return(new Completion(l << r)); case Operator.BitRightShift: return(new Completion(l >> r)); case Operator.BitExclusiveOr: return(new Completion(l ^ r)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(Completion.Exception(e.Message, this)); } } if (T.Equals(typeof(int))) { try { var l = TypeConverters.GetValue <int>(left.ReturnValue); var r = TypeConverters.GetValue <int>(right.ReturnValue); switch (Operator) { case Operator.Add: return(new Completion(l + r)); case Operator.Minus: return(new Completion(l - r)); case Operator.Mulitiply: return(new Completion(l * r)); case Operator.Divide: return(new Completion(l / r)); case Operator.Mod: return(new Completion(l % r)); case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); case Operator.NotEqual: return(new Completion(l != r)); case Operator.BitAnd: return(new Completion(l & r)); case Operator.BitOr: return(new Completion(l | r)); case Operator.BitLeftShift: return(new Completion(l << r)); case Operator.BitRightShift: return(new Completion(l >> r)); case Operator.BitExclusiveOr: return(new Completion(l ^ r)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(Completion.Exception(e.Message, this)); } } if (T.Equals(typeof(float))) { try { var l = TypeConverters.GetValue <float>(left.ReturnValue); var r = TypeConverters.GetValue <float>(right.ReturnValue); switch (Operator) { case Operator.Add: return(new Completion(l + r)); case Operator.Minus: return(new Completion(l - r)); case Operator.Mulitiply: return(new Completion(l * r)); case Operator.Divide: return(new Completion(l / r)); case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); case Operator.NotEqual: return(new Completion(l != r)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } catch (Exception e) { return(Completion.Exception(e.Message, this)); } } if (T.Equals(typeof(long))) { try { var l = TypeConverters.GetValue <long>(left.ReturnValue); var r = TypeConverters.GetValue <long>(right.ReturnValue); switch (Operator) { case Operator.Add: return(new Completion(l + r)); case Operator.Minus: return(new Completion(l - r)); case Operator.Mulitiply: return(new Completion(l * r)); case Operator.Divide: return(new Completion(l / r)); case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); case Operator.NotEqual: return(new Completion(l != r)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } catch (Exception e) { return(Completion.Exception(e.Message, this)); } } { try { double l = TypeConverters.GetValue <double>(left.ReturnValue); double r = TypeConverters.GetValue <double>(right.ReturnValue); switch (Operator) { case Operator.Add: return(new Completion(l + r)); case Operator.Minus: return(new Completion(l - r)); case Operator.Mulitiply: return(new Completion(l * r)); case Operator.Divide: return(new Completion(l / r)); case Operator.Great: return(new Completion(l > r)); case Operator.GreatOrEqual: return(new Completion(l >= r)); case Operator.Less: return(new Completion(l < r)); case Operator.LessOrEqual: return(new Completion(l <= r)); case Operator.Equal: return(new Completion(l == r)); case Operator.NotEqual: return(new Completion(l != r)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } catch (Exception e) { return(Completion.Exception(e.Message, this)); } } }
public Completion Assign(ExecutionEnvironment environment, object value) { if (string.IsNullOrEmpty(SetMemberFunction)) { return(Completion.Exception("this is can not set", this)); } if (Object == null) { return(Completion.Exception("object parameter can not be null", this)); } var tc = Object.Execute(environment); if (!tc.IsValue) { return(tc); } object obj = tc.ReturnValue; if (obj == null) { return(Completion.Exception("object value can not be null", Object)); } object m = null; if (IsMemberRequired) { if (Member == null) { return(Completion.Exception("member parameter can not be null", this)); } var mc = Member.Execute(environment); if (!mc.IsValue) { return(mc); } m = mc.ReturnValue; } Type t = obj.GetType(); try { if (MemberType == MemberType.Function) { MethodInfo set = t.GetMethod(SetMemberFunction); if (set == null) { return(Completion.Exception("No member function for " + obj.GetType(), Member)); } if (IsMemberRequired) { set.Invoke(obj, new object[] { m, value }); } else { set.Invoke(obj, new object[] { value }); } return(new Completion(value)); } else if (MemberType == MemberType.Property) { PropertyInfo set = t.GetProperty(SetMemberFunction); if (set == null) { return(Completion.Exception("No member function for " + obj.GetType(), Member)); } set.SetValue(obj, value); return(new Completion(value)); } else { FieldInfo set = t.GetField(SetMemberFunction); if (set == null) { return(Completion.Exception("No member function for " + obj.GetType(), Member)); } set.SetValue(obj, value); return(new Completion(value)); } }catch (Exception e) { return(Completion.Exception(e.Message, this)); } }
protected override Completion ExecuteImpl(ExecutionEnvironment environment) { if (Object == null) { return(Completion.Exception("object parameter can not be null", this)); } var tc = Object.Execute(environment); if (!tc.IsValue) { return(tc); } object obj = tc.ReturnValue; if (obj == null) { return(Completion.Exception("object value can not be null", Object)); } object m = null; if (IsMemberRequired) { if (Member == null) { return(Completion.Exception("member parameter can not be null", this)); } var mc = Member.Execute(environment); if (!mc.IsValue) { return(mc); } m = mc.ReturnValue; } Type t = obj.GetType(); try { if (MemberType == MemberType.Function) { MethodInfo get = t.GetMethod(GetMemberFunction); if (get == null) { return(Completion.Exception("No member function for " + obj.GetType(), Member)); } object value = null; if (IsMemberRequired) { value = get.Invoke(obj, new object[] { m }); } else { value = get.Invoke(obj, new object[] { }); } return(new Completion(value)); } else if (MemberType == MemberType.Property) { PropertyInfo get = t.GetProperty(GetMemberFunction); if (get == null) { return(Completion.Exception("No member function for " + obj.GetType(), Member)); } object value = get.GetValue(obj); return(new Completion(value)); } else { FieldInfo get = t.GetField(GetMemberFunction); if (get == null) { return(Completion.Exception("No member function for " + obj.GetType(), Member)); } object value = get.GetValue(obj); return(new Completion(value)); } } catch (Exception e) { return(Completion.Exception(e.Message, this)); } }
public Completion Execute(Class m) { IsAborting = false; IsCompleted = false; _current = m; if (_current == null) { return(null); } Completion c = Completion.Void; ExecutionEnvironment baseEnv = this.GetBaseEnvironment(); ExecutionEnvironment classEnv = new ExecutionEnvironment(baseEnv); ExecutionEnvironment instanceEnv = new ExecutionEnvironment(classEnv); foreach (var v in m.Variables) { instanceEnv.RegisterValue(v.Name, v.Value); } foreach (var func in _current.Functions) { if ("main".Equals(func.Name, StringComparison.OrdinalIgnoreCase)) { var parameter = func.Params; ExecutionEnvironment functionEnv = new ExecutionEnvironment(instanceEnv); ExecutionStarted?.Invoke(this, null); foreach (var p in parameter) { functionEnv.RegisterValue(p.Name, null); } foreach (var block in m.BlockStatements) { foreach (var s in block.Body) { if (s is ExpressionStatement) { Expression exp = (s as ExpressionStatement).Expression; if (exp is VariableDeclarationExpression) { exp.Execute(instanceEnv); } } } } try { IsCompleted = false; c = func.Execute(functionEnv); break; }catch (Exception e) { IsCompleted = true; Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); ExecutionAborted?.Invoke(this, new Completion(e.Message, CompletionType.Exception)); return(Completion.Void); } } } IsCompleted = true; if (c.Type == CompletionType.Value) { ExecutionCompleted?.Invoke(this, c); } else if (c.Type == CompletionType.Exception) { ExecutionAborted?.Invoke(this, c); } else { ExecutionAborted?.Invoke(this, Completion.Exception(Properties.Language.UnknowException, null)); } return(c); }
protected override Completion ExecuteImpl(ExecutionEnvironment enviroment) { if (Right == null) { return(Completion.Exception(Properties.Language.NoValueAssigned, this)); } var right = Right.Execute(enviroment); if (right.Type != CompletionType.Value) { return(right); } if (Left == null) { return(right); } if (Left is IAssignment) { try { if (Operator == AssignmentOperator.Equal) { return((Left as IAssignment).Assign(enviroment, right.ReturnValue)); } var l = Left.Execute(enviroment); if (!l.IsValue) { return(l); } if (l.ReturnValue == null) { return(Completion.Exception(Properties.Language.VariableNullException, Left)); } Type lt = l.ReturnValue.GetType(); if (l.ReturnValue is string || right.ReturnValue is string) { if (Operator != AssignmentOperator.AddEqual) { return(Completion.Exception(Properties.Language.StringOnlySupport, this)); } return((Left as IAssignment).Assign(enviroment, l.ReturnValue + "" + right.ReturnValue)); } if (!TypeConverters.IsNumber(l.ReturnValue)) { return(Completion.Exception(Properties.Language.NotNumber, Left)); } if (!TypeConverters.IsNumber(right.ReturnValue)) { return(Completion.Exception(Properties.Language.NotNumber, Right)); } Type maxType = TypeConverters.GetMaxTypes(l.ReturnValue, right.ReturnValue); Console.Write(maxType); if (maxType.Equals(typeof(char))) { var lint = TypeConverters.GetValue <char>(l.ReturnValue); var rint = TypeConverters.GetValue <char>(right.ReturnValue); if (Operator == AssignmentOperator.AddEqual) { return((Left as IAssignment).Assign(enviroment, lint + rint)); } if (Operator == AssignmentOperator.MinusEqual) { return((Left as IAssignment).Assign(enviroment, lint - rint)); } if (Operator == AssignmentOperator.MulitiplyEqual) { return((Left as IAssignment).Assign(enviroment, lint * rint)); } if (Operator == AssignmentOperator.DivideEqual) { return((Left as IAssignment).Assign(enviroment, lint / rint)); } if (Operator == AssignmentOperator.ModEqual) { return((Left as IAssignment).Assign(enviroment, lint % rint)); } if (Operator == AssignmentOperator.BitAndEqual) { return((Left as IAssignment).Assign(enviroment, lint & rint)); } if (Operator == AssignmentOperator.BitExclusiveOrEqual) { return((Left as IAssignment).Assign(enviroment, lint ^ rint)); } if (Operator == AssignmentOperator.BitLeftShiftEqual) { return((Left as IAssignment).Assign(enviroment, lint << rint)); } if (Operator == AssignmentOperator.BitOrEqual) { return((Left as IAssignment).Assign(enviroment, lint | rint)); } if (Operator == AssignmentOperator.BitRightShiftEqual) { return((Left as IAssignment).Assign(enviroment, lint >> rint)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } if (Operator == AssignmentOperator.BitAndEqual || Operator == AssignmentOperator.BitExclusiveOrEqual || Operator == AssignmentOperator.BitLeftShiftEqual || Operator == AssignmentOperator.BitOrEqual || Operator == AssignmentOperator.BitRightShiftEqual) { int lint = TypeConverters.GetValue <int>(l.ReturnValue); int rint = TypeConverters.GetValue <int>(right.ReturnValue); if (Operator == AssignmentOperator.BitAndEqual) { return((Left as IAssignment).Assign(enviroment, lint & rint)); } if (Operator == AssignmentOperator.BitExclusiveOrEqual) { return((Left as IAssignment).Assign(enviroment, lint ^ rint)); } if (Operator == AssignmentOperator.BitLeftShiftEqual) { return((Left as IAssignment).Assign(enviroment, lint << rint)); } if (Operator == AssignmentOperator.BitOrEqual) { return((Left as IAssignment).Assign(enviroment, lint | rint)); } if (Operator == AssignmentOperator.BitRightShiftEqual) { return((Left as IAssignment).Assign(enviroment, lint >> rint)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } if (maxType.Equals(typeof(int))) { int lint = TypeConverters.GetValue <int>(l.ReturnValue); int rint = TypeConverters.GetValue <int>(right.ReturnValue); if (Operator == AssignmentOperator.AddEqual) { return((Left as IAssignment).Assign(enviroment, lint + rint)); } if (Operator == AssignmentOperator.MinusEqual) { return((Left as IAssignment).Assign(enviroment, lint - rint)); } if (Operator == AssignmentOperator.MulitiplyEqual) { return((Left as IAssignment).Assign(enviroment, lint * rint)); } if (Operator == AssignmentOperator.DivideEqual) { return((Left as IAssignment).Assign(enviroment, lint / rint)); } if (Operator == AssignmentOperator.ModEqual) { return((Left as IAssignment).Assign(enviroment, lint % rint)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } else if (maxType.Equals(typeof(float))) { float lint = TypeConverters.GetValue <float>(l.ReturnValue); float rint = TypeConverters.GetValue <int>(right.ReturnValue); if (Operator == AssignmentOperator.AddEqual) { return((Left as IAssignment).Assign(enviroment, lint + rint)); } if (Operator == AssignmentOperator.MinusEqual) { return((Left as IAssignment).Assign(enviroment, lint - rint)); } if (Operator == AssignmentOperator.MulitiplyEqual) { return((Left as IAssignment).Assign(enviroment, lint * rint)); } if (Operator == AssignmentOperator.DivideEqual) { return((Left as IAssignment).Assign(enviroment, lint / rint)); } if (Operator == AssignmentOperator.ModEqual) { return((Left as IAssignment).Assign(enviroment, lint % rint)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } else { double lint = TypeConverters.GetValue <double>(l.ReturnValue); double rint = TypeConverters.GetValue <double>(right.ReturnValue); if (Operator == AssignmentOperator.AddEqual) { return((Left as IAssignment).Assign(enviroment, lint + rint)); } if (Operator == AssignmentOperator.MinusEqual) { return((Left as IAssignment).Assign(enviroment, lint - rint)); } if (Operator == AssignmentOperator.MulitiplyEqual) { return((Left as IAssignment).Assign(enviroment, lint * rint)); } if (Operator == AssignmentOperator.DivideEqual) { return((Left as IAssignment).Assign(enviroment, lint / rint)); } if (Operator == AssignmentOperator.ModEqual) { return((Left as IAssignment).Assign(enviroment, lint % rint)); } return(Completion.Exception(Properties.Language.UnknowException, this)); } }catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(Completion.Exception(e.Message, this)); } } else { return(Completion.Exception(Properties.Language.InvalidBeforeEqual, Left)); } }