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); }
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); }
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); }
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); }
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); }
public ScriptVarLink(ScriptVarLink toCopy) { Name = toCopy.Name; Var = toCopy.Var.Ref(); Next = toCopy.Next; Prev = toCopy.Prev; Owned = toCopy.Owned; }
private void Init() { FirstChild = null; LastChild = null; _callback = null; _callbackUserData = null; _data = null; _intData = 0; _doubleData = 0; }
private void Init() { FirstChild = null; LastChild = null; callback = null; callbackUserData = null; data = null; intData = 0; doubleData = 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))); }
private void CreateLink(ref ScriptVarLink link, ScriptVar res) { if (link == null || link.Owned) { link = new ScriptVarLink(res, null); } else { link.ReplaceWith(res); } }
public ScriptVar GetArrayIndex(Int32 idx) { ScriptVarLink link = FindChild(String.Format("{0}", idx)); if (link != null) { return(link.Var); } return(new ScriptVar(null, Flags.Null)); }
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; } }
public Int32 GettChildren() { Int32 n = 0; ScriptVarLink link = FirstChild; while (link != null) { n++; link = link.Next; } return(n); }
private ScriptVarLink FindInScopes(String name) { foreach (ScriptVar scriptVar in _scopes) { ScriptVarLink a = scriptVar.FindChild(name); if (a != null) { return(a); } } return(null); }
public void RemoveAllChildren() { ScriptVarLink c = FirstChild; while (c != null) { ScriptVarLink t = c.Next; c = t; } FirstChild = null; LastChild = null; }
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)); }
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)); }
public void RemoveChild(ScriptVar child) { ScriptVarLink link = FirstChild; while (link != null) { if (link.Var == child) { break; } link = link.Next; } RemoveLink(link); }
public ScriptVarLink FindChild(String childName) { ScriptVarLink v = FirstChild; while (v != null) { if (v.Name == childName) { return(v); } v = v.Next; } return(null); }
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; }
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); }
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); }
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); }
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); }
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(); } }
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); } } }
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; }
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); }
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); }
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; } }