private ScriptObject GetVariable(CodeMember member) { ScriptObject ret = null; if (member.Parent == null) { string name = (string)member.MemberValue; ScriptObject obj = GetVariableObject(name); ret = (obj == null ? m_script.GetValue(name) : obj); ret.Name = name; } else { ScriptObject parent = ResolveOperand(member.Parent); /*此处设置一下堆栈位置 否则 函数返回值取值出错会报错位置 例如 function Get() { return null } Get().a 上述代码报错会报道 return null 那一行 但实际出错 是 .a 的时候 下面这句话就是把堆栈设置回 .a 那一行 */ m_script.SetStackInfo(member.StackInfo); if (member.Type == MEMBER_TYPE.VALUE) { object name = member.MemberValue; ret = parent.GetValue(name); ret.Name = parent.Name + "." + name.ToString(); } else { object name = ResolveOperand(member.MemberObject).KeyValue; ret = parent.GetValue(name); ret.Name = parent.Name + "." + name.ToString(); } } if (ret == null) throw new ExecutionException(m_script, "GetVariable member is error"); if (member.Calc != CALC.NONE) { ScriptNumber num = ret as ScriptNumber; if (num == null) throw new ExecutionException(m_script, "++或者--只能应用于Number类型"); return num.Calc(member.Calc); } return ret; }
public CodeAssign(CodeMember member, CodeObject value, TokenType assignType, string breviary, int line) : base(breviary, line) { this.member = member; this.value = value; this.AssignType = assignType; }
public CodeAssign(CodeMember member, CodeObject value, Scorpio.Compiler.TokenType assignType, string breviary, int line) : base(breviary, line) { this.member = member; this.value = value; this.AssignType = assignType; }
private object GetMember(CodeMember member) { return member.Type == MEMBER_TYPE.VALUE ? member.MemberValue : ResolveOperand(member.MemberObject).KeyValue; }
private void SetVariable(CodeMember member, ScriptObject variable) { if (member.Parent == null) { string name = (string)member.MemberValue; if (!SetVariableObject(name, variable)) m_script.SetObjectInternal(name, variable); } else { ResolveOperand(member.Parent).SetValue(GetMember(member), variable); } }
//返回变量数据 private CodeObject GetVariable(CodeObject parent) { CodeObject ret = parent; for ( ; ; ) { Token m = ReadToken(); if (m.Type == TokenType.Period) { ret = new CodeMember(ReadIdentifier(), ret); } else if (m.Type == TokenType.LeftBracket) { CodeObject member = GetObject(); ReadRightBracket(); if (member is CodeScriptObject) { ScriptObject obj = ((CodeScriptObject)member).Object; if (member.Not) { ret = new CodeMember(!obj.LogicOperation(), ret); } else if (member.Negative) { ScriptNumber num = obj as ScriptNumber; if (num == null) throw new ParserException("Script Object Type [" + obj.Type + "] is cannot use [-] sign", m); ret = new CodeMember(num.Negative().KeyValue, ret); } else { ret = new CodeMember(obj.KeyValue, ret); } } else { ret = new CodeMember(member, ret); } } else if (m.Type == TokenType.LeftPar) { UndoToken(); ret = GetFunction(ret); } else { UndoToken(); break; } ret.StackInfo = new StackInfo(m_strBreviary, m.SourceLine); } return ret; }
//获得单一变量 private CodeObject GetOneObject() { CodeObject ret = null; Token token = ReadToken(); bool not = false; bool negative = false; CALC calc = CALC.NONE; if (token.Type == TokenType.Not) { not = true; token = ReadToken(); } else if (token.Type == TokenType.Minus) { negative = true; token = ReadToken(); } else if (token.Type == TokenType.Increment) { calc = CALC.PRE_INCREMENT; token = ReadToken(); } else if (token.Type == TokenType.Decrement) { calc = CALC.PRE_DECREMENT; token = ReadToken(); } switch (token.Type) { case TokenType.Identifier: ret = new CodeMember((string)token.Lexeme); break; case TokenType.Function: UndoToken(); ret = new CodeFunction(ParseFunctionDeclaration(false)); break; case TokenType.LeftPar: ret = new CodeRegion(GetObject()); ReadRightParenthesis(); break; case TokenType.LeftBracket: UndoToken(); ret = GetArray(); break; case TokenType.LeftBrace: UndoToken(); ret = GetTable(); break; case TokenType.Eval: ret = GetEval(); break; case TokenType.Null: ret = new CodeScriptObject(m_script, null); break; case TokenType.Boolean: case TokenType.Number: case TokenType.String: case TokenType.SimpleString: ret = new CodeScriptObject(m_script, token.Lexeme); break; default: throw new ParserException("Object起始关键字错误 ", token); } ret.StackInfo = new StackInfo(m_strBreviary, token.SourceLine); ret = GetVariable(ret); ret.Not = not; ret.Negative = negative; if (ret is CodeMember) { if (calc != CALC.NONE) { ((CodeMember)ret).Calc = calc; } else { Token peek = ReadToken(); if (peek.Type == TokenType.Increment) { calc = CALC.POST_INCREMENT; } else if (peek.Type == TokenType.Decrement) { calc = CALC.POST_DECREMENT; } else { UndoToken(); } if (calc != CALC.NONE) { ((CodeMember)ret).Calc = calc; } } } else if (calc != CALC.NONE) { throw new ParserException("++ 或者 -- 只支持变量的操作", token); } return ret; }
//返回变量数据 private CodeObject GetVariable(CodeObject parent) { CodeObject ret = parent; for ( ; ; ) { Token m = ReadToken(); if (m.Type == TokenType.Period) { string identifier = ReadIdentifier(); ret = new CodeMember(identifier, ret); } else if (m.Type == TokenType.LeftBracket) { CodeObject member = GetObject(); ReadRightBracket(); if (member is CodeScriptObject) { ScriptObject obj = ((CodeScriptObject)member).Object; if (obj is ScriptNumber || obj is ScriptString) ret = new CodeMember(obj.ObjectValue, ret); else throw new ParserException("获取变量只能是 number或string", m); } else { ret = new CodeMember(member, ret); } } else if (m.Type == TokenType.LeftPar) { UndoToken(); ret = GetFunction(ret); } else { UndoToken(); break; } ret.StackInfo = new StackInfo(m_strBreviary, m.SourceLine); } return ret; }
private ScriptObject GetVariable(CodeMember member) { ScriptObject ret = null; if (member.Parent == null) { string name = (string)member.MemberValue; ScriptObject obj = GetVariableObject(name); ret = (obj == null ? m_script.GetValue(name) : obj); } else { ret = ResolveOperand(member.Parent).GetValue(GetMember(member)); } if (ret == null) throw new ExecutionException(m_script, "GetVariable member is error"); if (member.Calc != CALC.NONE) { ScriptNumber num = ret as ScriptNumber; if (num == null) throw new ExecutionException(m_script, "++或者--只能应用于Number类型"); return num.Calc(member.Calc); } return ret; }