コード例 #1
0
        private ScriptVarLink Shift(ref bool execute)
        {
            ScriptVarLink a = Expression(ref execute);

            if (_currentLexer.TokenType == ScriptLex.LexTypes.LShift ||
                _currentLexer.TokenType == ScriptLex.LexTypes.RShift ||
                _currentLexer.TokenType == ScriptLex.LexTypes.RShiftUnsigned)
            {
                ScriptLex.LexTypes op = _currentLexer.TokenType;
                _currentLexer.Match(op);
                ScriptVarLink b = Base(ref execute);

                Int32 shift = execute ? b.Var.GetInt() : 0;

                if (execute)
                {
                    if (op == ScriptLex.LexTypes.LShift)
                    {
                        a.Var.SetInt(a.Var.GetInt() << shift);
                    }

                    if (op == ScriptLex.LexTypes.RShift)
                    {
                        a.Var.SetInt(a.Var.GetInt() >> shift);
                    }

                    if (op == ScriptLex.LexTypes.RShiftUnsigned)
                    {
                        a.Var.SetInt((int)(((uint)a.Var.GetInt()) >> shift));
                    }
                }
            }

            return(a);
        }
コード例 #2
0
        public ScriptVarLink AddChild(String childName, ScriptVar child)
        {
            if (IsUndefined)
            {
                _flags = Flags.Object;
            }

            ScriptVar c = child ?? new ScriptVar();

            ScriptVarLink link = new ScriptVarLink(c, childName)
            {
                Owned = true
            };

            if (LastChild != null)
            {
                LastChild.Next = link;
                link.Prev      = LastChild;
                LastChild      = link;
            }
            else
            {
                FirstChild = link;
                LastChild  = link;
            }

            return(link);
        }
コード例 #3
0
        public Int32 GetArrayLength()
        {
            Int32 highest = -1;

            if (!IsArray)
            {
                return(0);
            }

            ScriptVarLink link = FirstChild;

            while (link != null)
            {
                Int32 outputVal;
                if (Int32.TryParse(link.Name, out outputVal))
                {
                    if (outputVal > highest)
                    {
                        highest = outputVal;
                    }
                }

                link = link.Next;
            }

            return(highest + 1);
        }
コード例 #4
0
        public ScriptVarLink AddChild(string childName, ScriptVar child, bool readOnly = false)
        {
            if (IsUndefined)
            {
                flags = Flags.Object;
            }

            var c = child ?? new ScriptVar();

            var link = new ScriptVarLink(c, childName, readOnly)
            {
                Owned = true
            };

            if (LastChild != null)
            {
                LastChild.Next = link;
                link.Prev      = LastChild;
                LastChild      = link;
            }
            else
            {
                FirstChild = link;
                LastChild  = link;
            }

            return(link);
        }
コード例 #5
0
        private ScriptVarLink ParseDefinition(ScriptLex.LexTypes lexType)
        {
            currentLexer.Match(lexType);
            var funcName = string.Empty;

            //named function
            if (currentLexer.TokenType == ScriptLex.LexTypes.Id)
            {
                funcName = currentLexer.TokenString;
                currentLexer.Match(ScriptLex.LexTypes.Id);
            }

            var funcVar = new ScriptVarLink(new ScriptVar(null, ScriptVar.Flags.Function), funcName);

            if (lexType == ScriptLex.LexTypes.RFunction || lexType == ScriptLex.LexTypes.RCatch)
            {
                ParseFunctionArguments(funcVar.Var);
            }

            var funcBegin = currentLexer.TokenStart;
            var noExecute = false;

            Block(ref noExecute);
            funcVar.Var.SetData(currentLexer.GetSubString(funcBegin));

            return(funcVar);
        }
コード例 #6
0
 public ScriptVarLink(ScriptVarLink toCopy)
 {
     Name  = toCopy.Name;
     Var   = toCopy.Var.Ref();
     Next  = toCopy.Next;
     Prev  = toCopy.Prev;
     Owned = toCopy.Owned;
 }
コード例 #7
0
 private void Init()
 {
     FirstChild        = null;
     LastChild         = null;
     _callback         = null;
     _callbackUserData = null;
     _data             = null;
     _intData          = 0;
     _doubleData       = 0;
 }
コード例 #8
0
 private void Init()
 {
     FirstChild       = null;
     LastChild        = null;
     callback         = null;
     callbackUserData = null;
     data             = null;
     intData          = 0;
     doubleData       = 0;
 }
コード例 #9
0
        public ScriptVarLink FindChildOrCreate(String childName, Flags varFlags = Flags.Undefined)
        {
            ScriptVarLink l = FindChild(childName);

            if (l != null)
            {
                return(l);
            }

            return(AddChild(childName, new ScriptVar(null, varFlags)));
        }
コード例 #10
0
 private void CreateLink(ref ScriptVarLink link, ScriptVar res)
 {
     if (link == null || link.Owned)
     {
         link = new ScriptVarLink(res, null);
     }
     else
     {
         link.ReplaceWith(res);
     }
 }
コード例 #11
0
        public ScriptVar GetArrayIndex(Int32 idx)
        {
            ScriptVarLink link = FindChild(String.Format("{0}", idx));

            if (link != null)
            {
                return(link.Var);
            }

            return(new ScriptVar(null, Flags.Null));
        }
コード例 #12
0
ファイル: ScriptVar.cs プロジェクト: ajinkyakulkarni/DScript
        public void Trace(Int32 indent, String name)
        {
            System.Diagnostics.Trace.TraceInformation("{0}{1} = '{2}' ({3})", new String(' ', indent), name ?? "ROOT", GetString(), _flags);

            ScriptVarLink link = FirstChild;

            while (link != null)
            {
                link.Var.Trace(indent + 2, link.Name);
                link = link.Next;
            }
        }
コード例 #13
0
        public Int32 GettChildren()
        {
            Int32         n    = 0;
            ScriptVarLink link = FirstChild;

            while (link != null)
            {
                n++;
                link = link.Next;
            }
            return(n);
        }
コード例 #14
0
        private ScriptVarLink FindInScopes(String name)
        {
            foreach (ScriptVar scriptVar in _scopes)
            {
                ScriptVarLink a = scriptVar.FindChild(name);
                if (a != null)
                {
                    return(a);
                }
            }

            return(null);
        }
コード例 #15
0
        public void RemoveAllChildren()
        {
            ScriptVarLink c = FirstChild;

            while (c != null)
            {
                ScriptVarLink t = c.Next;
                c = t;
            }

            FirstChild = null;
            LastChild  = null;
        }
コード例 #16
0
        public ScriptVarLink EvalComplex(String code)
        {
            ScriptLex         oldLex    = _currentLexer;
            Stack <ScriptVar> oldScopes = _scopes;

            _currentLexer = new ScriptLex(code);

            _callStack.Clear();
            _scopes.Clear();
            _scopes.Push(Root);

            ScriptVarLink v = null;

            try
            {
                bool execute = true;
                do
                {
                    v = Base(ref execute);
                    if (_currentLexer.TokenType != ScriptLex.LexTypes.Eof)
                    {
                        _currentLexer.Match((ScriptLex.LexTypes) ';');
                    }
                } while (_currentLexer.TokenType != ScriptLex.LexTypes.Eof);
            }
            catch (ScriptException ex)
            {
                String errorMessage = ex.Message;
                int    i            = 0;
                foreach (ScriptVar scriptVar in _scopes)
                {
                    errorMessage += "\n" + i++ + ": " + scriptVar;
                }

#if WINDOWS_UWP
                Debug.WriteLine(errorMessage);
#else
                Console.Write(errorMessage);
#endif
            }

            _currentLexer = oldLex;
            _scopes       = oldScopes;

            if (v != null)
            {
                return(v);
            }

            return(new ScriptVarLink(new ScriptVar(null), null));
        }
コード例 #17
0
        public ScriptVarLink EvalComplex(string code)
        {
            var oldLex    = currentLexer;
            var oldScopes = scopes;

            currentLexer = new ScriptLex(code);
            scopes       = new List <ScriptVar>();
            //callStack.Clear();
            scopes.Clear();
            scopes.PushBack(Root);

            ScriptVarLink v = null;

            try
            {
                var execute = true;
                do
                {
                    v = Base(ref execute);
                    if (currentLexer.TokenType != ScriptLex.LexTypes.Eof)
                    {
                        currentLexer.Match((ScriptLex.LexTypes) ';');
                    }
                } while (currentLexer.TokenType != ScriptLex.LexTypes.Eof);
            }
            catch (ScriptException ex)
            {
                var errorMessage = new StringBuilder(string.Format("ERROR on line {0} column {1} [{2}]", currentLexer.LineNumber, currentLexer.ColumnNumber, ex.Message));

                int i = 0;
                foreach (ScriptVar scriptVar in scopes)
                {
                    errorMessage.AppendLine();
                    errorMessage.Append(i++ + ": " + scriptVar);
                }

                Console.Error.WriteLine(errorMessage.ToString());
            }

            currentLexer = oldLex;
            scopes       = oldScopes;

            if (v != null)
            {
                return(v);
            }

            return(new ScriptVarLink(new ScriptVar(null), null));
        }
コード例 #18
0
        public void RemoveChild(ScriptVar child)
        {
            ScriptVarLink link = FirstChild;

            while (link != null)
            {
                if (link.Var == child)
                {
                    break;
                }

                link = link.Next;
            }

            RemoveLink(link);
        }
コード例 #19
0
        public ScriptVarLink FindChild(String childName)
        {
            ScriptVarLink v = FirstChild;

            while (v != null)
            {
                if (v.Name == childName)
                {
                    return(v);
                }

                v = v.Next;
            }

            return(null);
        }
コード例 #20
0
        public void Execute(string code)
        {
            var oldLex    = currentLexer;
            var oldScopes = scopes;

            scopes = new List <ScriptVar>();

            scopes.Clear();
            scopes.PushBack(Root);

            var rootLink = new ScriptVarLink(Root, "root");

            callStack.Push(rootLink);

            using (currentLexer = new ScriptLex(code))
            {
                var execute = true;

                while (currentLexer.TokenType != 0)
                {
                    try
                    {
                        Statement(ref execute);
                    }
                    catch (Exception ex)
                    {
                        if (ex is ScriptException || ex is JITException)
                        {
                            var errorMessage = new StringBuilder(string.Format("ERROR on line {0} column {1} [{2}]", currentLexer.LineNumber, currentLexer.ColumnNumber, ex.Message));

                            Console.Error.WriteLine(errorMessage.ToString());

                            return;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            callStack.Pop();

            currentLexer = oldLex;
            scopes       = oldScopes;
        }
コード例 #21
0
        public ScriptVarLink AddChildNoDup(String childName, ScriptVar child)
        {
            ScriptVar c = child ?? new ScriptVar();

            ScriptVarLink v = FindChild(childName);

            if (v != null)
            {
                v.ReplaceWith(c);
            }
            else
            {
                v = AddChild(childName, c);
            }

            return(v);
        }
コード例 #22
0
        public ScriptVar DeepCopy()
        {
            ScriptVar newVar = new ScriptVar();

            newVar.CopySimpleData(this);

            ScriptVarLink link = FirstChild;

            while (link != null)
            {
                ScriptVar copied = link.Name != PrototypeClassName?link.Var.DeepCopy() : link.Var;

                newVar.AddChild(link.Name, copied);

                link = link.Next;
            }

            return(newVar);
        }
コード例 #23
0
        private ScriptVarLink FindInParentClasses(ScriptVar obj, String name)
        {
            ScriptVarLink implementation;
            ScriptVarLink parentClass = obj.FindChild(ScriptVar.PrototypeClassName);

            while (parentClass != null)
            {
                implementation = parentClass.Var.FindChild(name);
                if (implementation != null)
                {
                    return(implementation);
                }
                parentClass = parentClass.Var.FindChild(ScriptVar.PrototypeClassName);
            }

            if (obj.IsString)
            {
                implementation = _stringClass.FindChild(name);
                if (implementation != null)
                {
                    return(implementation);
                }
            }

            if (obj.IsArray)
            {
                implementation = _arrayClass.FindChild(name);
                if (implementation != null)
                {
                    return(implementation);
                }
            }

            implementation = _objectClass.FindChild(name);
            if (implementation != null)
            {
                return(implementation);
            }

            return(null);
        }
コード例 #24
0
        public void AddObject(String[] ns, String objectName, ScriptVar val)
        {
            ScriptVar baseVar = Root;

            if (ns != null)
            {
                int x = 0;
                for (; x < ns.Length; x++)
                {
                    ScriptVarLink link = baseVar.FindChild(ns[x]);

                    if (link == null)
                    {
                        link = baseVar.AddChild(ns[x], new ScriptVar(null, ScriptVar.Flags.Object));
                    }

                    baseVar = link.Var;
                }
            }

            baseVar.AddChild(objectName, val);
        }
コード例 #25
0
        public void CopyValue(ScriptVar val)
        {
            if (val != null)
            {
                CopySimpleData(val);
                RemoveAllChildren();

                ScriptVarLink link = val.FirstChild;

                while (link != null)
                {
                    ScriptVar copied = link.Name != PrototypeClassName?link.Var.DeepCopy() : link.Var;

                    AddChild(link.Name, copied);

                    link = link.Next;
                }
            }
            else
            {
                SetUndefined();
            }
        }
コード例 #26
0
        public void SetArrayIndex(Int32 idx, ScriptVar value)
        {
            ScriptVarLink link = FindChild(String.Format("{0}", idx));

            if (link != null)
            {
                if (value.IsUndefined)
                {
                    RemoveLink(link);
                }
                else
                {
                    link.ReplaceWith(value);
                }
            }
            else
            {
                if (!value.IsUndefined)
                {
                    AddChild(String.Format("{0}", idx), value);
                }
            }
        }
コード例 #27
0
        public void AddMethod(String funcProto, ScriptCallbackCB callback, Object userdata)
        {
            ScriptLex oldLex = _currentLexer;

            using (_currentLexer = new ScriptLex(funcProto))
            {
                ScriptVar baseVar = Root;

                _currentLexer.Match(ScriptLex.LexTypes.RFunction);
                String funcName = _currentLexer.TokenString;
                _currentLexer.Match(ScriptLex.LexTypes.Id);

                while (_currentLexer.TokenType == (ScriptLex.LexTypes) '.')
                {
                    _currentLexer.Match((ScriptLex.LexTypes) '.');
                    ScriptVarLink link = baseVar.FindChild(funcName);

                    if (link == null)
                    {
                        link = baseVar.AddChild(funcName, new ScriptVar(null, ScriptVar.Flags.Object));
                    }

                    baseVar  = link.Var;
                    funcName = _currentLexer.TokenString;
                    _currentLexer.Match(ScriptLex.LexTypes.Id);
                }

                ScriptVar funcVar = new ScriptVar(null, ScriptVar.Flags.Function | ScriptVar.Flags.Native);
                funcVar.SetCallback(callback, userdata);

                ParseFunctionArguments(funcVar);

                baseVar.AddChild(funcName, funcVar);
            }

            _currentLexer = oldLex;
        }
コード例 #28
0
        public void AddMethod(String[] ns, String funcName, String[] args, ScriptCallbackCB callback, Object userdata)
        {
            String    fName   = funcName;
            ScriptVar baseVar = Root;

            if (ns != null)
            {
                int x = 0;
                for (; x < ns.Length; x++)
                {
                    ScriptVarLink link = baseVar.FindChild(ns[x]);

                    if (link == null)
                    {
                        link = baseVar.AddChild(ns[x], new ScriptVar(null, ScriptVar.Flags.Object));
                    }

                    baseVar = link.Var;
                }
            }


            ScriptVar funcVar = new ScriptVar(null, ScriptVar.Flags.Function | ScriptVar.Flags.Native);

            funcVar.SetCallback(callback, userdata);

            //do we have any arguments to create?
            if (args != null)
            {
                foreach (string arg in args)
                {
                    funcVar.AddChildNoDup(arg, null);
                }
            }

            baseVar.AddChild(fName, funcVar);
        }
コード例 #29
0
        private ScriptVarLink ParseFunctionDefinition()
        {
            _currentLexer.Match(ScriptLex.LexTypes.RFunction);
            String funcName = String.Empty;

            //named function
            if (_currentLexer.TokenType == ScriptLex.LexTypes.Id)
            {
                funcName = _currentLexer.TokenString;
                _currentLexer.Match(ScriptLex.LexTypes.Id);
            }

            ScriptVarLink funcVar = new ScriptVarLink(new ScriptVar(null, ScriptVar.Flags.Function), funcName);

            ParseFunctionArguments(funcVar.Var);

            Int32 funcBegin = _currentLexer.TokenStart;
            bool  execute   = false;

            Block(ref execute);
            funcVar.Var.SetData(_currentLexer.GetSubString(funcBegin));

            return(funcVar);
        }
コード例 #30
0
        public void RemoveLink(ScriptVarLink link)
        {
            if (link == null)
            {
                return;
            }

            if (link.Next != null)
            {
                link.Next.Prev = link.Prev;
            }
            if (link.Prev != null)
            {
                link.Prev.Next = link.Next;
            }
            if (LastChild == link)
            {
                LastChild = link.Prev;
            }
            if (FirstChild == link)
            {
                FirstChild = link.Next;
            }
        }