internal static object ComponentMath(SkryptEngine engine, VectorInstance left, VectorInstance right, Func <double, double, double> func)
        {
            var dimension = left.Components.Length;

            if (dimension == right.Components.Length)
            {
                var args = new double[dimension];

                for (var i = 0; i < dimension; i++)
                {
                    var newVal = func(left.Components[i], right.Components[i]);

                    args[i] = newVal;
                }

                return(engine.Vector.Construct(args));
            }

            return(new InvalidOperation());
        }
        public static SkryptObject FromByteArray(SkryptEngine engine, SkryptObject self, Arguments arguments)
        {
            var array     = arguments.GetAs <ArrayInstance>(0);
            var rawString = "";

            for (var i = 0; i < array.SequenceValues.Count; i++)
            {
                var rawValue = array.SequenceValues[i];

                if (rawValue is NumberInstance num)
                {
                    rawString += (char)num.Value;
                }
                else
                {
                    throw new FatalErrorException("Number expected.");
                }
            }

            return(engine.CreateString(rawString));
        }
        public static SkryptObject Split(SkryptEngine engine, SkryptObject self, Arguments arguments)
        {
            var str   = self as StringInstance;
            var input = new List <string>();

            for (int i = 0; i < arguments.Values.Length; i++)
            {
                var s = arguments.GetAs <StringInstance>(i);

                input.Add(s.Value);
            }

            var split = str.Value.Split(input.ToArray(), StringSplitOptions.None);

            var stringArray = new StringInstance[split.Length];

            for (int i = 0; i < split.Length; i++)
            {
                stringArray[i] = engine.CreateString(split[i]);
            }

            return(engine.CreateArray(stringArray));
        }
        public static SkryptObject IndexOf(SkryptEngine engine, SkryptObject self, Arguments arguments)
        {
            var str    = self as StringInstance;
            var search = arguments.GetAs <StringInstance>(0);
            var start  = arguments[1];

            if (start == null)
            {
                return(engine.CreateNumber(str.Value.IndexOf(search)));
            }
            else
            {
                if (start is NumberInstance num)
                {
                    var startIndex = Math.Max(Math.Min((int)num, str.Value.Length - 1), 0);

                    return(engine.CreateNumber(str.Value.IndexOf(search, startIndex)));
                }
                else
                {
                    throw new InvalidArgumentTypeException($"Expected argument of type Number.");
                }
            }
        }
        public static SkryptObject Substring(SkryptEngine engine, SkryptObject self, Arguments arguments)
        {
            var str   = self as StringInstance;
            var start = arguments.GetAs <NumberInstance>(0);
            var end   = arguments[1];

            if (end == null)
            {
                return(engine.CreateString(str.Value.Substring((int)start)));
            }
            else
            {
                if (end is NumberInstance)
                {
                    var length = Math.Max(Math.Min((int)(NumberInstance)end, str.Value.Length - 1) - (int)start, 0);

                    return(engine.CreateString(str.Value.Substring((int)start, length)));
                }
                else
                {
                    throw new InvalidArgumentTypeException($"Expected argument of type Number.");
                }
            }
        }
 public NumberType(SkryptEngine engine) : base(engine)
 {
     Template = engine.TemplateMaker.CreateTemplate(typeof(NumberInstance));
 }
        public static SkryptObject ParseInt(SkryptEngine engine, SkryptObject self, Arguments input)
        {
            var value = int.Parse(input[0].ToString(), System.Globalization.CultureInfo.InvariantCulture);

            return(engine.CreateNumber(value));
        }
Beispiel #8
0
 protected ErrorHandler(SkryptEngine engine)
 {
     this.engine = engine;
 }
 public SkryptInstance(SkryptEngine engine) : base(engine)
 {
 }
Beispiel #10
0
        public SkryptObject Run(SkryptEngine engine, SkryptObject self, Arguments args)
        {
            var blockStmnt         = Context.StmntBlock;
            var visitor            = new SkryptVisitor(engine);
            var lexicalEnvironment = LexicalEnvironment.MakeCopy(Context.LexicalEnvironment);

            if (BaseEnvironment != null)
            {
                lexicalEnvironment.Parent = BaseEnvironment;
            }
            lexicalEnvironment.Variables["self"].Value = self;

            for (int i = 0; i < Parameters.Length; i++)
            {
                var parameter = Parameters[i];
                var input     = args[i];

                if (input is IValue noref)
                {
                    input = noref.Copy();
                }

                if (i < args.Values.Length)
                {
                    lexicalEnvironment.Variables[parameter.Name].Value = input;
                }
                else
                {
                    lexicalEnvironment.Variables[parameter.Name].Value = parameter.Default == null ? null : engine.Visitor.Visit(parameter.Default);
                }
            }

            var returnValue = default(SkryptObject);
            var block       = blockStmnt.block();
            var expr        = blockStmnt.expression();
            var assignStmnt = blockStmnt.assignStmnt();
            var returnStmnt = blockStmnt.returnStmnt();

            visitor.CurrentEnvironment = lexicalEnvironment;

            if (block != null)
            {
                var prev = visitor.CurrentEnvironment;

                visitor.CurrentEnvironment = visitor.CurrentEnvironment.Children.Find(x => x.Context == block);

                for (int i = 0; i < block.ChildCount; i++)
                {
                    var c = block.GetChild(i);

                    visitor.Visit(c);

                    if (Context.JumpState == JumpState.Return)
                    {
                        Context.JumpState = JumpState.None;
                        break;
                    }
                }

                returnValue = Context.ReturnValue;
            }
            else if (expr != null)
            {
                returnValue = visitor.Visit(expr);
            }
            else if (assignStmnt != null)
            {
                visitor.Visit(assignStmnt);
            }
            else if (returnStmnt != null)
            {
                visitor.Visit(returnStmnt);

                returnValue = Context.ReturnValue;
            }

            if (returnValue is FunctionInstance functionInstance && functionInstance.Function is ScriptFunction scriptFunction)
            {
                var checkParent = scriptFunction.Context.Context;
                var isDefinedInCurrentFunction = false;

                while (checkParent.Parent != null)
                {
                    if (checkParent == Context.Context)
                    {
                        isDefinedInCurrentFunction = true;
                        break;
                    }

                    checkParent = checkParent.Parent;
                }

                if (isDefinedInCurrentFunction)
                {
                    scriptFunction.BaseEnvironment = visitor.CurrentEnvironment;
                }
            }

            // BaseEnvironment = null;

            return(returnValue);
        }
 public static SkryptObject Length(SkryptEngine engine, SkryptObject self)
 {
     return(engine.CreateNumber(Math.Sqrt((NumberInstance)Length2(engine, self))));
 }
 public VectorInstance(SkryptEngine engine) : base(engine)
 {
 }
 public MemoryModule(SkryptEngine engine) : base(engine)
 {
 }
        public static SkryptObject GetAllocatedBytes(SkryptEngine engine, SkryptObject self, Arguments arguments)
        {
            var bytes = SkryptEngine.GetAllocatedBytesForCurrentThread() - engine.InitialMemoryUsage;

            return(engine.CreateNumber(bytes));
        }
 public ErrorListener(SkryptEngine engine, CompileErrorHandler compileErrorHandler)
 {
     _engine = engine;
     _compileErrorHandler = compileErrorHandler;
 }
Beispiel #16
0
 public ArrayIteratorInstance(SkryptEngine engine) : base(engine)
 {
 }
Beispiel #17
0
 public SkryptVisitor(SkryptEngine engine)
 {
     _engine = engine;
 }
 public BooleanInstance(SkryptEngine engine, bool value) : base(engine)
 {
     Value = value;
 }
Beispiel #19
0
 public DebugModule(SkryptEngine engine) : base(engine)
 {
 }
        public static SkryptObject Normalized(SkryptEngine engine, SkryptObject self)
        {
            var length = (NumberInstance)Length(engine, self);

            return(ComponentMathNumeric(engine, self as VectorInstance, (x) => x / length) as VectorInstance);
        }
 public static SkryptObject Type(SkryptEngine engine, SkryptObject self)
 {
     return((self as SkryptInstance).TypeObject);
 }
Beispiel #22
0
 public ScriptModule(string name, SkryptEngine engine) : base(engine)
 {
     Name = name;
 }
Beispiel #23
0
 public ScriptInstance(SkryptEngine engine) : base(engine)
 {
     //CreateProperty("Hash", engine.CreateNumber(GetHashCode()));
 }
 public static SkryptObject IsInRange(SkryptEngine engine, SkryptObject self, Arguments arguments) => null;
 public static SkryptObject Get(SkryptEngine engine, SkryptObject self, Arguments arguments) => null;
 public SkryptObject Run(SkryptEngine engine, SkryptObject self)
 {
     return(Property.Invoke(engine, self));
 }
 public EnumerableTrait(SkryptEngine engine) : base(engine)
 {
 }
Beispiel #28
0
 public IteratorTrait(SkryptEngine engine) : base(engine)
 {
 }
Beispiel #29
0
 public CompileErrorHandler(SkryptEngine engine, string source)
 {
     this.engine = engine;
     Source      = source;
 }
 public DefaultErrorHandler(SkryptEngine engine) : base(engine)
 {
 }