public SkryptObject Sort(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var m = TypeConverter.ToMethod(values, 0); if (m.Parameters.Count < 2) { engine.ThrowError("Input function must have 2 parameters!"); } ((Array)self).List.Sort((x, y) => { var scope = SkryptMethod.GetPopulatedScope(m, new[] { x, y }); scope.ParentScope = ScopeContext; var r = m.Execute(engine, self, new[] { x, y }, scope); if (r.ReturnObject.ToBoolean()) { return(1); } else { return(-1); } }); return(self); }
public void AnalizeStatement(Node node, ScopeContext scopeContext) { // Check statement Node conditionNode = node.SubNodes[0]; SkryptObject result = engine.executor.ExecuteExpression(conditionNode, scopeContext); // Check block Node blockNode = node.SubNodes[1]; Analize(blockNode, scopeContext); // Check else/elseif if (node.SubNodes.Count > 2) { for (int i = 2; i < node.SubNodes.Count; i++) { // elseif block Node elseNode = node.SubNodes[i]; if (elseNode.Body == "elseif") { AnalizeStatement(elseNode, scopeContext); } else { Analize(elseNode, scopeContext); } } } }
public static SkryptObject Mod(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var a = TypeConverter.ToNumeric(values, 0); var b = TypeConverter.ToNumeric(values, 1); return(engine.Create <Numeric>(a % b)); }
public static SkryptObject Position(SkryptEngine engine, SkryptObject self) { var hitResultInstance = self as HitResultInstance; var point = hitResultInstance.raycastHit.point; return(engine.CreateVector3(point.x, point.y, point.z)); }
public static SkryptObject Normal(SkryptEngine engine, SkryptObject self) { var hitResultInstance = self as HitResultInstance; var normal = hitResultInstance.raycastHit.normal; return(engine.CreateVector3(normal.x, normal.y, normal.z)); }
/// <summary> /// Executes an index opeation. /// </summary> public SkryptObject ExecuteIndex(IndexNode node, ScopeContext scopeContext) { var arguments = new List <SkryptObject>(); foreach (var subNode in node.Arguments) { var result = ExecuteExpression(subNode, scopeContext); arguments.Add(result); } var Object = ExecuteExpression(node.Getter, scopeContext); // Dynamically change type so we can get it's actual operations. dynamic left = Convert.ChangeType(Object, Object.GetType()); Operation opLeft = SkryptObject.GetOperation(Operators.Index, Object.GetType(), arguments[0].GetType(), left.Operations); OperationDelegate operation = null; if (opLeft != null) { operation = opLeft.OperationDelegate; } else { _engine.ThrowError("No such operation as index " + left.Name + "!", node.Getter.Token); } var inputArray = new List <SkryptObject>(arguments); inputArray.Insert(0, Object); return(operation(inputArray.ToArray(), _engine)); }
public void SetFunctionOwner(SkryptObject function, SkryptObject owner) { if (function.GetType() == typeof(UserFunction)) { ((UserFunction)function).Class = owner; } }
private static SkryptObject Benchmark(SkryptEngine engine, SkryptObject self, Arguments arguments) { var function = arguments.GetAs <FunctionInstance>(0); var amount = arguments.GetAs <NumberInstance>(1).Value; var lastResult = default(SkryptObject); var sw = System.Diagnostics.Stopwatch.StartNew(); for (int x = 0; x < amount; x++) { lastResult = function.Function.Run(engine, null, Arguments.Empty); } sw.Stop(); Console.WriteLine($"Executed function {amount} times in {sw.Elapsed.TotalMilliseconds}ms"); Console.WriteLine($"Equals {1 / sw.Elapsed.TotalSeconds * amount} times per second"); Console.WriteLine($"Average {(sw.Elapsed.TotalMilliseconds / amount).ToString(".####################")}ms"); if (lastResult != null) { Console.WriteLine($"Last result: {lastResult}"); } return(null); }
public static SkryptObject ToHex(SkryptEngine engine, SkryptObject self, SkryptObject[] input) { var n = TypeConverter.ToNumeric(input, 0); var str = Sys.Convert.ToString((long)n, 16); return(engine.Create <String>(str)); }
private static SkryptObject Print(SkryptEngine engine, SkryptObject self, Arguments arguments) { Console.ForegroundColor = ConsoleColor.White; var str = ""; for (var j = 0; j < arguments.Length; j++) { if (arguments[j] == null) { str += "null"; } else { str += arguments[j].ToString(); } if (j < arguments.Length - 1) { str += ", "; } } Console.WriteLine(str); return(null); }
private void CreateProperty(SkryptObject target, IScopedContext ctx, ParserRuleContext propertyTree, bool isPrivate) { IToken nameToken = null; if (propertyTree.GetChild(0) is MemberDefinitionStatementContext assignCtx) { nameToken = assignCtx.name().NAME().Symbol; } else if (propertyTree.GetChild(0) is FunctionStatementContext fnCtx) { nameToken = fnCtx.name().NAME().Symbol; } else if (propertyTree.GetChild(0) is ModuleStatementContext moduleCtx) { nameToken = moduleCtx.name().NAME().Symbol; } else if (propertyTree.GetChild(0) is StructStatementContext structCtx) { nameToken = structCtx.name().NAME().Symbol; } var value = ctx.LexicalEnvironment.Variables[nameToken.Text].Value; if (value == null) { CompileErrorHandler.TolerateError(nameToken, "Field can't be set to an undefined value."); } target.CreateProperty(nameToken.Text, value, isPrivate, ctx.LexicalEnvironment.Variables[nameToken.Text].IsConstant); }
public object EvaluatePlusExpression(SkryptObject left, SkryptObject right) { if (left is NumberInstance && right is NumberInstance) { return((left as NumberInstance) + (right as NumberInstance)); } if (left is StringInstance) { return((left as StringInstance).Value + right); } if (right is StringInstance) { return(left + (right as StringInstance).Value); } if (left is VectorInstance && right is VectorInstance) { return(VectorInstance.ComponentMath(_engine, left as VectorInstance, right as VectorInstance, (x, y) => x + y)); } if (left is NumberInstance && right is VectorInstance) { return(VectorInstance.ComponentMathNumeric(_engine, right as VectorInstance, (x) => (left as NumberInstance) + x)); } if (left is VectorInstance && right is NumberInstance) { return(VectorInstance.ComponentMathNumeric(_engine, left as VectorInstance, (x) => x + (right as NumberInstance))); } return(new InvalidOperation()); }
public static SkryptObject error(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var s = TypeConverter.ToString(values, 0, engine); engine.ThrowError(s, engine.CurrentNode?.Token); return(engine.Create <Null>(s)); }
public static SkryptObject Clamp(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var x = TypeConverter.ToNumeric(values, 0); var a = TypeConverter.ToNumeric(values, 1); var b = TypeConverter.ToNumeric(values, 2); return(engine.Create <Numeric>(SysMath.Min(SysMath.Max(x, a), b))); }
public static SkryptObject Lerp(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var a = TypeConverter.ToNumeric(values, 0); var b = TypeConverter.ToNumeric(values, 1); var t = TypeConverter.ToNumeric(values, 2); return(engine.Create <Numeric>(a * (1 - t) + b * t)); }
public SkryptObject Draw(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var s = (Canvas)self; s.Window.Draw(); return(s); }
public SkryptObject Push(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var a = TypeConverter.ToAny(values, 0); ((Array)self).List.Add(a); return((Array)self); }
public static SkryptObject Open(SkryptEngine engine, SkryptObject self, SkryptObject[] values) { var path = TypeConverter.ToString(values, 0, engine); var file = engine.Create <File>(path); return(file); }
public object EvaluateBitNotExpression(SkryptObject value) { if (value is NumberInstance) { return(~(int)(value as NumberInstance).Value); } return(new InvalidOperation()); }
public object EvaluateGreaterEqualExpression(SkryptObject left, SkryptObject right) { if (left is NumberInstance && right is NumberInstance) { return((left as NumberInstance).Value >= (right as NumberInstance).Value); } return(new InvalidOperation()); }
public object EvaluateExponentExpression(SkryptObject left, SkryptObject right) { if (left is NumberInstance && right is NumberInstance) { return(Math.Pow(left as NumberInstance, right as NumberInstance)); } return(new InvalidOperation()); }
public object EvaluateNotExpression(SkryptObject value) { if (value is BooleanInstance) { return(!(value as BooleanInstance)); } return(new InvalidOperation()); }
public static SkryptObject ToUTF16(SkryptEngine engine, SkryptObject self, SkryptObject[] input) { var str = TypeConverter.ToString(input, 0, engine); byte[] bytes = Sys.Text.Encoding.Default.GetBytes(str); var encoded = Sys.Text.UnicodeEncoding.Unicode.GetString(bytes); return(engine.Create <String>(encoded)); }
public static SkryptObject GetPixel(SkryptEngine engine, SkryptObject self, Arguments arguments) { var x = arguments.GetAs <NumberInstance>(0); var y = arguments.GetAs <NumberInstance>(1); var color = self.AsType <ImageInstance>().bitMap.GetPixel((int)x, (int)y); return(engine.GetValue("Color").AsType <ColorType>().Construct(color.R, color.G, color.B)); }
public void AddVariable(string Name, SkryptObject Value, bool IsConstant = false) { Variables[Name] = new Variable { Name = Name, Value = Value, IsConstant = IsConstant, Scope = this }; }
public static Sys.String ToString(SkryptObject[] args, int index, SkryptEngine engine) { if (args == null || index > args.Length - 1) { return(new Sys.String("")); } return(SkryptObject.GetString(args[index], engine)); }
public void SetVariable(string name, SkryptObject value, Modifier modifiers = Modifier.None) { Variables[name] = new Variable { Name = name, Value = value, Modifiers = modifiers, Scope = this }; }
public SkryptObject Reverse(SkryptEngine engine, SkryptObject self, SkryptObject[] input) { var s = (String)self; char[] charArray = s.Value.ToCharArray(); Sys.Array.Reverse(charArray); return(engine.Create <String>(new string(charArray))); }
public SkryptObject Find(SkryptEngine engine, SkryptObject self, SkryptObject[] input) { return(engine.Create <Numeric>( ((String)self).Value.IndexOf( TypeConverter.ToString(input, 0, engine), (int)TypeConverter.ToNumeric(input, 1) ) )); }
public SkryptObject Replace(SkryptEngine engine, SkryptObject self, SkryptObject[] input) { return(engine.Create <String>( ((String)self).Value.Replace( TypeConverter.ToString(input, 0, engine), TypeConverter.ToString(input, 1, engine) ) )); }