public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); ICLS_Expression expr_if = listParam[0]; bool bif = (bool)expr_if.ComputeValue(content).value; //if (expr_init != null) expr_init.ComputeValue(content); ICLS_Expression expr_go1 = listParam[1]; ICLS_Expression expr_go2 = null; if (listParam.Count > 2) { expr_go2 = listParam[2]; } CLS_Content.Value value = null; if (bif && expr_go1 != null) { value = expr_go1.ComputeValue(content); } else if (!bif && expr_go2 != null) { value = expr_go2.ComputeValue(content); } //while((bool)expr_continue.value); //for 逻辑 //做数学计算 //从上下文取值 //_value = null; content.OutStack(this); return(value); }
// 执行一段脚本 public object Execute(string script) { IList <Token> tokens = null; #if UNITY_EDITOR try { #endif if (m_clsEnv == null) { Debug.LogWarning("please run project script first !"); return(null); } tokens = m_clsEnv.ParserToken(script); //词法分析 RuntimeCompilerTokens(tokens); ICLS_Expression expr = m_clsEnv.Expr_CompilerToken(tokens, false); //语法分析,语法块 CLS_Content.Value value = m_clsEnv.Expr_Execute(expr, m_clsContent); //执行表达式 return(value == null ? null : value.value); #if UNITY_EDITOR } catch (System.Exception ex) { Debug.LogError(ex.Message + "\n" + m_clsContent.DumpStack(tokens) + ex); return(null); } #endif }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); CLS_Content.Value vrt = null; ICLS_Expression expr_while = listParam[0]; ICLS_Expression expr_block = listParam[1]; while ((bool)expr_while.ComputeValue(content).value) { if (expr_block != null) { CLS_Content.Value v = expr_block.ComputeValue(content); if (v != null) { if (v.breakBlock > 2) { vrt = v; } if (v.breakBlock > 1) { break; } } } } content.DepthRemove(); content.OutStack(this); return(vrt); }
public ICLS_Expression Optimize(ICLS_Expression value, ICLS_Environment env) { ICLS_Expression expr = value as ICLS_Expression; if (expr == null) return value; else return OptimizeDepth(expr, CLS_Content.NewContent(env)); }
public CLS_Content.Value Execute(ICLS_Expression expr, CLS_Content content = null) { if (content == null) { content = CreateContent(); } return(expr.ComputeValue(content)); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); ICLS_Expression expr_while = listParam[1] as ICLS_Expression; ICLS_Expression expr_block = listParam[0] as ICLS_Expression; CLS_Content.Value vrt = null; do { if (expr_block != null) { if (expr_block is CLS_Expression_Block) { var v = expr_block.ComputeValue(content); if (v != null) { if (v.breakBlock > 2) { vrt = v; } if (v.breakBlock > 1) { break; } } } else { content.DepthAdd(); var v = expr_block.ComputeValue(content); if (v != null) { if (v.breakBlock > 2) { vrt = v; } if (v.breakBlock > 1) { break; } } content.DepthRemove(); } //if (v.breakBlock == 1) continue; //if (v.breakBlock == 2) break; //if (v.breakBlock == 10) return v; } } while ((bool)expr_while.ComputeValue(content).value); content.DepthRemove(); content.OutStack(this); return(vrt); //for 逻辑 //做数学计算 //从上下文取值 //_value = null; }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); CLS_Expression_Define define = listParam[0] as CLS_Expression_Define; if (define == null) { } define.ComputeValue(content); System.Collections.IEnumerable emu = listParam[1].ComputeValue(content).value as System.Collections.IEnumerable; ICLS_Expression expr_block = listParam[2] as ICLS_Expression; var it = emu.GetEnumerator(); while (it.MoveNext()) { content.Set(define.value_name, it.Current); if (expr_block != null) { var v = expr_block.ComputeValue(content); if (v != null && v.breakBlock > 1) { break; } ; } } //ICLS_Expression expr_continue = listParam[1] as ICLS_Expression; //ICLS_Expression expr_step = listParam[2] as ICLS_Expression; //ICLS_Expression expr_block = listParam[3] as ICLS_Expression; //for (;(bool)expr_continue.ComputeValue(content).value; expr_step.ComputeValue(content)) //{ // if(expr_block!=null) // { // var v = expr_block.ComputeValue(content); // if (v != null && v.breakBlock > 1) break; ; // //if (v.breakBlock == 1) continue; // //if (v.breakBlock == 2) break; // //if (v.breakBlock == 10) return v; // } //} content.DepthRemove(); content.OutStack(this); return(null); //for 逻辑 //做数学计算 //从上下文取值 //_value = null; }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); List <string> depth__; content.Record(out depth__); try { ICLS_Expression expr = listParam[0]; if (expr is CLS_Expression_Block) { expr.ComputeValue(content); } else { content.DepthAdd(); expr.ComputeValue(content); content.DepthRemove(); } } catch (Exception err) { bool bParse = false; int i = 1; while (i < listParam.Count) { CLS_Expression_Define def = listParam[i] as CLS_Expression_Define; if (err.GetType() == (Type)def.value_type || err.GetType().IsSubclassOf((Type)def.value_type)) { content.DepthAdd(); content.DefineAndSet(def.value_name, def.value_type, err); listParam[i + 1].ComputeValue(content); content.DepthRemove(); bParse = true; break; } i += 2; } if (!bParse) { throw err; } } content.Restore(depth__, this); //while((bool)expr_continue.value); //for 逻辑 //做数学计算 //从上下文取值 //_value = null; content.OutStack(this); return(null); }
public void Restore(List <string> depth, ICLS_Expression expr) { while (tvalues.Peek() != depth) { tvalues.Pop(); } while (stackExpr.Peek() != expr) { stackExpr.Pop(); } }
public override void MemberValueSet(CLS_Content content, object object_this, string valuename, object value, bool isBaseCall = false) { Quaternion newVal = (Quaternion)object_this; if (valuename == "eulerAngles") { newVal.eulerAngles = ((Vector3)value); } else if (valuename == "w") { newVal.w = Convert.ToSingle(value); } else if (valuename == "x") { newVal.x = Convert.ToSingle(value); } else if (valuename == "y") { newVal.y = Convert.ToSingle(value); } else if (valuename == "z") { newVal.z = Convert.ToSingle(value); } else { base.MemberValueSet(content, object_this, valuename, value, isBaseCall); } ICLS_Expression expLeft = content.CallExpression.listParam[0]; CLS_Expression_GetValue expGetValue = expLeft as CLS_Expression_GetValue; if (expGetValue != null) { content.Set(expGetValue.value_name, newVal); return; } CLS_Expression_MemberFind expMemberFind = expLeft as CLS_Expression_MemberFind; if (expMemberFind != null) { expGetValue = expMemberFind.listParam[0] as CLS_Expression_GetValue; if (content.CallExpression is CLS_Expression_SelfOpWithValue) { content.Set(expGetValue.value_name, newVal); return; } CLS_Content.Value val = content.Get(expGetValue.value_name); content.environment.GetType(val.type).function.MemberValueSet(content, val.value, expMemberFind.membername, newVal); return; } base.MemberValueSet(content, object_this, valuename, value, isBaseCall); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); ICLS_Expression expr_init = listParam[0] as ICLS_Expression; if (expr_init != null) { expr_init.ComputeValue(content); } ICLS_Expression expr_continue = listParam[1] as ICLS_Expression; ICLS_Expression expr_step = listParam[2] as ICLS_Expression; ICLS_Expression expr_block = listParam[3] as ICLS_Expression; for (; (bool)expr_continue.ComputeValue(content).value; expr_step.ComputeValue(content)) { if (expr_block != null) { if (expr_block is CLS_Expression_Block) { var v = expr_block.ComputeValue(content); if (v != null && v.breakBlock > 1) { break; } ; } else { content.DepthAdd(); var v = expr_block.ComputeValue(content); if (v != null && v.breakBlock > 1) { break; } ; content.DepthRemove(); } //if (v.breakBlock == 1) continue; //if (v.breakBlock == 2) break; //if (v.breakBlock == 10) return v; } } content.DepthRemove(); content.OutStack(this); return(null); //for 逻辑 //做数学计算 //从上下文取值 //_value = null; }
public void InStack(ICLS_Expression expr) { if (!useDebug) { return; } if (stacklist.Count > 0 && stacklist.Peek() == expr) { throw new Exception("InStack error"); } stacklist.Push(expr); }
public void OutStack(ICLS_Expression expr) { if (!useDebug) { return; } if (stacklist.Peek() != expr) { throw new Exception("OutStack error:" + expr.ToString() + " err:" + stacklist.Peek().ToString()); } stacklist.Pop(); }
public override CLS_Content.Value MemberCall(CLS_Content content, object object_this, string function, BetterList <CLS_Content.Value> _params, bool isBaseCall = false) { CLS_Content.Value retVal = null; Color newVal = (Color)object_this; if (function == "Equals") { retVal = new CLS_Content.Value(); retVal.type = typeof(bool); retVal.value = newVal.Equals(_params[0].value); } else if (function == "ToString") { if (_params.size == 0) { retVal = new CLS_Content.Value(); retVal.type = typeof(string); retVal.value = newVal.ToString(); } else if (_params.size == 1) { retVal = new CLS_Content.Value(); retVal.type = typeof(string); retVal.value = newVal.ToString(((string)_params[0].value)); } } else { return(base.MemberCall(content, object_this, function, _params, isBaseCall)); } ICLS_Expression expLeft = content.CallExpression.listParam[0]; CLS_Expression_GetValue expGetValue = expLeft as CLS_Expression_GetValue; if (expGetValue != null) { content.Set(expGetValue.value_name, newVal); return(retVal); } CLS_Expression_MemberFind expMemberFind = expLeft as CLS_Expression_MemberFind; if (expMemberFind != null) { expGetValue = expMemberFind.listParam[0] as CLS_Expression_GetValue; CLS_Content.Value val = content.Get(expGetValue.value_name); content.environment.GetType(val.type).function.MemberValueSet(content, val.value, expMemberFind.membername, newVal); return(retVal); } return(base.MemberCall(content, object_this, function, _params, isBaseCall)); }
public ICLS_Expression Optimize(ICLS_Expression value, CLS_Content content) { ICLS_Expression expr = value as ICLS_Expression; if (expr == null) { return(value); } else { return(OptimizeDepth(expr, content)); } }
public ICLS_Expression Optimize(ICLS_Expression value, ICLS_Environment env) { ICLS_Expression expr = value as ICLS_Expression; if (expr == null) { return(value); } else { return(OptimizeDepth(expr, new CLS_Content(env))); } }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); CLS_Content.Value vrt = null; ICLS_Expression expr_init = listParam[0]; ICLS_Expression expr_continue = listParam[1]; ICLS_Expression expr_step = listParam[2]; ICLS_Expression expr_block = listParam[3]; if (expr_init != null) { expr_init.ComputeValue(content); } for (;;) { if (expr_continue != null && !(bool)expr_continue.ComputeValue(content).value) { break; } if (expr_block != null) { CLS_Content.Value v = expr_block.ComputeValue(content); if (v != null) { if (v.breakBlock > 2) { vrt = v; } if (v.breakBlock > 1) { break; } } } if (expr_step != null) { expr_step.ComputeValue(content); } } content.DepthRemove(); content.OutStack(this); return(vrt); }
ICLS_Expression OptimizeSingle(ICLS_Expression expr, CLS_Content content) { if (expr is CLS_Expression_Math2Value || expr is CLS_Expression_Math2ValueAndOr || expr is CLS_Expression_Math2ValueLogic) { if (expr.listParam[0] is ICLS_Value && expr.listParam[1] is ICLS_Value) { CLS_Content.Value result = expr.ComputeValue(content); if ((Type)result.type == typeof(bool)) { CLS_Value_Value <bool> value = new CLS_Value_Value <bool>(); value.value_value = (bool)result.value; value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return(value); } else { ICLS_Type v = content.environment.GetType(result.type); ICLS_Value value = v.MakeValue(result.value); value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return(value); } } } if (expr is CLS_Expression_Math3Value) { CLS_Content.Value result = expr.listParam[0].ComputeValue(content); if ((Type)result.type == typeof(bool)) { bool bv = (bool)result.value; if (bv) { return(expr.listParam[1]); } else { return(expr.listParam[2]); } } } return(expr); }
public override void MemberValueSet(CLS_Content content, object object_this, string valuename, object value, bool isBaseCall = false) { Color newVal = (Color)object_this; if (valuename == "a") { newVal.a = Convert.ToSingle(value); } else if (valuename == "b") { newVal.b = Convert.ToSingle(value); } else if (valuename == "g") { newVal.g = Convert.ToSingle(value); } else if (valuename == "r") { newVal.r = Convert.ToSingle(value); } else { base.MemberValueSet(content, object_this, valuename, value, isBaseCall); } ICLS_Expression expLeft = content.CallExpression.listParam[0]; CLS_Expression_GetValue expGetValue = expLeft as CLS_Expression_GetValue; if (expGetValue != null) { content.Set(expGetValue.value_name, newVal); return; } CLS_Expression_MemberFind expMemberFind = expLeft as CLS_Expression_MemberFind; if (expMemberFind != null) { expGetValue = expMemberFind.listParam[0] as CLS_Expression_GetValue; if (content.CallExpression is CLS_Expression_SelfOpWithValue) { content.Set(expGetValue.value_name, newVal); return; } CLS_Content.Value val = content.Get(expGetValue.value_name); content.environment.GetType(val.type).function.MemberValueSet(content, val.value, expMemberFind.membername, newVal); return; } base.MemberValueSet(content, object_this, valuename, value, isBaseCall); }
ICLS_Expression OptimizeDepth(ICLS_Expression expr, CLS_Content content) { // 先进行深入优化 if (expr.listParam != null) { for (int i = 0; i < expr.listParam.Count; i++) { ICLS_Expression subexpr = expr.listParam[i] as ICLS_Expression; if (subexpr != null) { expr.listParam[i] = OptimizeDepth(subexpr, content); } } } return(OptimizeSingle(expr, content)); }
public string DumpStack(IList <Token> tokenlist = null) { string dumpInfo = ""; if (this.CallType != null && this.CallType.tokenlist != null) { tokenlist = this.CallType.tokenlist; } foreach (var subc in this.stackContent) { dumpInfo += subc.DumpStack(tokenlist); } if (tokenlist == null) { return(dumpInfo); } dumpInfo += "CSLightDumpStack:" + this.CallFileName; if (stackExpr.size == 0) { return(dumpInfo + "\n"); } ICLS_Expression exp = CallExpression; if (exp != null) { dumpInfo += " [line=" + exp.lineBegin + "] "; int tlCount = tokenlist != null ? tokenlist.Count : 0; for (int i = exp.tokenBegin; i <= exp.tokenEnd; i++) { if (i >= tlCount) { break; } dumpInfo += tokenlist[i].text; } } dumpInfo += "\n"; return(dumpInfo); }
ICLS_Expression OptimizeDepth(ICLS_Expression expr, CLS_Content content) { //先进行深入优化 if (expr.listParam != null) { for (int i = 0; i < expr.listParam.Count; i++) { ICLS_Expression subexpr = expr.listParam[i] as ICLS_Expression; if (subexpr != null) { expr.listParam[i] = OptimizeDepth(subexpr, content); } } } return OptimizeSingle(expr, content); }
// 添加基类函数 public void addBaseFun(ICLS_Expression baseFun, int tbegin, int tend, int lbegin, int lend) { if (expr_runtime == null) { expr_runtime = baseFun; } else if (!(expr_runtime is CLS_Expression_Block)) { CLS_Expression_Block baseBlock = new CLS_Expression_Block(tbegin, tend, lbegin, lend); baseBlock.listParam.Add(baseFun); baseBlock.listParam.Add(expr_runtime); expr_runtime = baseBlock; } else { expr_runtime.listParam.Insert(0, baseFun); } }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); CLS_Content.Value value = null; if ((bool)listParam[0].ComputeValue(content).value) { ICLS_Expression expr_block = listParam[1]; if (expr_block != null) { if (expr_block is CLS_Expression_Block) { value = expr_block.ComputeValue(content); } else { content.DepthAdd(); value = expr_block.ComputeValue(content); content.DepthRemove(); } } } else if (listParam.Count > 2) { ICLS_Expression expr_elseif = listParam[2]; if (expr_elseif != null) { if (expr_elseif is CLS_Expression_Block) { value = expr_elseif.ComputeValue(content); } else { content.DepthAdd(); value = expr_elseif.ComputeValue(content); content.DepthRemove(); } } } content.OutStack(this); return(value); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); ICLS_Expression leftExp = listParam[0]; CLS_Content.Value left = leftExp.ComputeValue(content); CLS_Content.Value right = listParam[1].ComputeValue(content); ICLS_Type type = content.environment.GetType(left.type); CLType returntype; left.value = type.Math2Value(content, mathop, left.value, right, out returntype); //left.value = type.ConvertTo(content, left.value, left.type); if (leftExp is CLS_Expression_MemberFind) { CLS_Expression_MemberFind f = leftExp as CLS_Expression_MemberFind; CLS_Content.Value parent = f.listParam[0].ComputeValue(content); ICLS_Type ptype = content.environment.GetType(parent.type); ptype.function.MemberValueSet(content, parent.value, f.membername, left.value); } else if (leftExp is CLS_Expression_StaticFind) { CLS_Expression_StaticFind f = leftExp as CLS_Expression_StaticFind; f.type.function.StaticValueSet(content, f.staticmembername, left.value); } else if (leftExp is CLS_Expression_IndexFind) { CLS_Expression_IndexFind f = leftExp as CLS_Expression_IndexFind; CLS_Content.Value parent = f.listParam[0].ComputeValue(content); CLS_Content.Value key = f.listParam[1].ComputeValue(content); ICLS_Type ptype = content.environment.GetType(parent.type); ptype.function.IndexSet(content, parent.value, key.value, left.value); } content.OutStack(this); return(null); }
public ICLS_Expression CompilerCode(string code, bool SimpleExpression = false) { int hash = code.GetHashCode(); if (cachedExpression.ContainsKey(hash)) { return(cachedExpression[hash]); } IList <Token> tokens = ParserToken(code); ICLS_Expression expr = CompilerToken(tokens, SimpleExpression); expr = Optimize(expr); if (expr != null) { cachedExpression[hash] = expr; } return(expr); }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); CLS_Content.Value vrt = null; CLS_Expression_Define define = listParam[0] as CLS_Expression_Define; define.ComputeValue(content); IEnumerator it = (listParam[1].ComputeValue(content).value as IEnumerable).GetEnumerator(); ICLS_Expression expr_block = listParam[2]; while (it.MoveNext()) { content.Set(define.value_name, it.Current); if (expr_block != null) { CLS_Content.Value v = expr_block.ComputeValue(content); if (v != null) { if (v.breakBlock > 2) { vrt = v; } if (v.breakBlock > 1) { break; } } } } content.DepthRemove(); content.OutStack(this); return(vrt); }
public DeleLambda(CLS_Content content, IList <ICLS_Expression> param, ICLS_Expression func) { this.content = content.Clone(); this.expr_func = func; for (int i = 0, count = param.Count; i < count; i++) { CLS_Expression_GetValue v1 = param[i] as CLS_Expression_GetValue; if (v1 != null) { paramTypes.Add(null); paramNames.Add(v1.value_name); continue; } CLS_Expression_Define v2 = param[i] as CLS_Expression_Define; if (v2 != null) { paramTypes.Add(v2.value_type); paramNames.Add(v2.value_name); continue; } throw new Exception("DeleLambda 参数不正确"); } }
public DeleLambda(CLS_Content content, IList<ICLS_Expression> param, ICLS_Expression func) { this.content = content.Clone(); this.expr_func = func; for (int i = 0, count = param.Count; i < count; i++) { CLS_Expression_GetValue v1 = param[i] as CLS_Expression_GetValue; if (v1 != null) { paramTypes.Add(null); paramNames.Add(v1.value_name); continue; } CLS_Expression_Define v2 = param[i] as CLS_Expression_Define; if (v2 != null) { paramTypes.Add(v2.value_type); paramNames.Add(v2.value_name); continue; } throw new Exception("DeleLambda 参数不正确"); } }
public CLS_Content.Value ComputeValue(CLS_Content content) { content.InStack(this); content.DepthAdd(); CLS_Content.Value value = null; foreach (ICLS_Expression i in listParam) { ICLS_Expression e = i as ICLS_Expression; if (e != null) { value = e.ComputeValue(content); } if (value != null && value.breakBlock != 0) { break; } } content.DepthRemove(); content.OutStack(this); return(value); }
public DeleLambda(CLS_Content content,IList<ICLS_Expression> param,ICLS_Expression func) { this.content = content.Clone(); this.expr_func = func; foreach(var p in param) { CLS_Expression_GetValue v1 = p as CLS_Expression_GetValue; CLS_Expression_Define v2 = p as CLS_Expression_Define; if (v1 != null) { paramTypes.Add(null); paramNames.Add(v1.value_name); } else if (v2 != null) { paramTypes.Add(v2.value_type); paramNames.Add(v2.value_name); } else { throw new Exception("DeleLambda 参数不正确"); } } }
public DeleLambda(CLS_Content content, IList <ICLS_Expression> param, ICLS_Expression func) { this.content = content.Clone(); this.expr_func = func; foreach (var p in param) { CLS_Expression_GetValue v1 = p as CLS_Expression_GetValue; CLS_Expression_Define v2 = p as CLS_Expression_Define; if (v1 != null) { paramTypes.Add(null); paramNames.Add(v1.value_name); } else if (v2 != null) { paramTypes.Add(v2.value_type); paramNames.Add(v2.value_name); } else { throw new Exception("DeleLambda 参数不正确"); } } }
public CLS_Content.Value Execute(ICLS_Expression expr, CLS_Content content = null) { if (content == null) content = CreateContent(); return expr.ComputeValue(content); }
public ICLS_Expression Optimize(ICLS_Expression old) { return compiler.Optimize(old, contentGloabl); }
public void OutStack(ICLS_Expression expr) { if (!useDebug) return; if (stacklist.Peek() != expr) { throw new Exception("OutStack error:" + expr.ToString() + " err:"+stacklist.Peek().ToString()); } stacklist.Pop(); }
// 可以搞出Block public bool Compiler_Expression_Block(IList<Token> tlist, ICLS_Environment content, int pos, int posend, out ICLS_Expression value) { int begin = pos; value = null; List<ICLS_Expression> values = new List<ICLS_Expression>(); int end = 0; do { if (tlist[begin].type == TokenType.COMMENT) { begin++; continue; } if (tlist[begin].type == TokenType.PUNCTUATION && tlist[begin].text == ";") { begin++; continue; } int bdep; //脱一次壳 end = FindCodeInBlock(tlist, ref begin, out bdep); if (end > posend) { end = posend; } int expend = end; int expbegin = begin; if (expbegin > expend) return true; if (bdep == 2) //编译块表达式 { expbegin++; expend--; ICLS_Expression subvalue; bool bsucc = Compiler_Expression_Block(tlist,content, expbegin, expend, out subvalue); if (bsucc) { if (subvalue != null) values.Add(subvalue); } else { return false; } } else { ICLS_Expression subvalue; bool bsucc = Compiler_Expression(tlist, content,expbegin, expend, out subvalue); if (bsucc) { if (subvalue != null) values.Add(subvalue); } else { return false; } } begin = end + 1; } while (begin <= posend); if (values.Count == 1) { value = values[0]; } else if (values.Count > 1) { value = new CLS_Expression_Block(values, pos, end, tlist[pos].line, tlist[end].line); } return true; }
// 不出Block,必须一次解析完,括号为优先级 public bool Compiler_Expression(IList<Token> tlist, ICLS_Environment environment, int pos, int posend, out ICLS_Expression value) { if (pos > posend) { value = null; return false; } int begin = pos; value = null; List<ICLS_Expression> values = new List<ICLS_Expression>(); do { if (tlist[begin].type == TokenType.COMMENT) { begin++; continue; } if (tlist[begin].type == TokenType.PUNCTUATION && tlist[begin].text == ";") { begin++; continue; } int bdep; //脱一次壳 int end = FindCodeAny(tlist, ref begin, out bdep); if (end > posend) { end = posend; } else if (end < posend) { bool bMath = false; for (int i = end + 1; i <= posend; i++) { if (tlist[i].type == TokenType.COMMENT) continue; if (tlist[i].type == TokenType.PUNCTUATION && tlist[i].text == ";") continue; bMath = true; break; } if (bMath) { end = posend; //如果表达式一次搞不完,那肯定是优先级问题 value = Compiler_Expression_Math(tlist,environment, begin, posend); return true; } } int expend = end; int expbegin = begin; if (expbegin > expend) return true; if (expend == expbegin) { if (tlist[expbegin].type == TokenType.KEYWORD) { if (tlist[expbegin].text == "return") { ICLS_Expression subvalue = Compiler_Expression_Loop_Return(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); } else if (tlist[expbegin].text == "break") { ICLS_Expression subvalue = Compiler_Expression_Loop_Break(tlist, expbegin); if (null == subvalue) return false; else values.Add(subvalue); } else if (tlist[expbegin].text == "continue") { ICLS_Expression subvalue = Compiler_Expression_Loop_Continue(tlist, expbegin); if (null == subvalue) return false; else values.Add(subvalue); } else if (tlist[expbegin].text == "true") { values.Add(new CLS_True()); } else if (tlist[expbegin].text == "false") { values.Add(new CLS_False()); } else if (tlist[expbegin].text == "null") { values.Add(new CLS_Null()); } } else { ICLS_Expression subvalue = Compiler_Expression_Value(tlist[expbegin],expbegin); if (null == subvalue) return false; else values.Add(subvalue); } } else if (bdep == 1) //深层表达式 { expbegin++; expend--; ICLS_Expression subvalue; bool bsucc = Compiler_Expression(tlist, environment, expbegin, expend, out subvalue); if (bsucc) { if (subvalue != null) values.Add(subvalue); } else { return false; } } else //尝试各种表达式 { bool bTest = false; // 取反表达式 if (tlist[expbegin].type == TokenType.PUNCTUATION && tlist[expbegin].text == "-") { if (tlist[expend].type == TokenType.VALUE) { // 负数 if (expend == expbegin + 1) { ICLS_Expression subvalue = Compiler_Expression_SubValue(tlist[expend]); if (null == subvalue) return false; else values.Add(subvalue); } else { ICLS_Expression subvalue = Compiler_Expression_Math(tlist, environment, begin, posend); if (null == subvalue) return false; else values.Add(subvalue); } } else { // 负数表达式 ICLS_Expression subvalue = Compiler_Expression_NegativeValue(tlist, environment, expbegin + 1, expend); if (null == subvalue) return false; else values.Add(subvalue); } bTest = true; } // 按位取反表达式 else if (tlist[expbegin].type == TokenType.PUNCTUATION && tlist[expbegin].text == "~") { ICLS_Expression subvalue = Compiler_Expression_NegativeBit(tlist, environment, expbegin + 1, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } // 逻辑反表达式 else if (tlist[expbegin].type == TokenType.PUNCTUATION && tlist[expbegin].text == "!") { ICLS_Expression subvalue = Compiler_Expression_NegativeLogic(tlist, environment, expbegin + 1, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } if (!bTest && tlist[expbegin].type == TokenType.TYPE) { //定义表达式或者定义并赋值表达式 if (tlist[expbegin + 1].type == TokenType.IDENTIFIER) { if (expend == expbegin + 1) { //定义表达式 ICLS_Expression subvalue = Compiler_Expression_Define(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (expend > expbegin + 2 && tlist[expbegin + 2].type == TokenType.PUNCTUATION && tlist[expbegin + 2].text == "=") { //定义并赋值表达式 ICLS_Expression subvalue = Compiler_Expression_DefineAndSet(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else { LogError(tlist,"无法识别的表达式:", expbegin ,expend); return false; } } else if (tlist[expbegin + 1].text == "[" && tlist[expbegin + 2].text == "]" && tlist[expbegin + 3].type == TokenType.IDENTIFIER)//定义表达式或者定义并赋值表达式 { if (expend == expbegin + 3) { //定义表达式 ICLS_Expression subvalue = Compiler_Expression_DefineArray(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (expend > expbegin + 4 && tlist[expbegin + 4].type == TokenType.PUNCTUATION && tlist[expbegin + 4].text == "=") { //定义并赋值表达式 ICLS_Expression subvalue = Compiler_Expression_DefineAndSetArray(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } else if (tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == ".") { //静态调用表达式 ICLS_Expression subvalue = Compiler_Expression_Math(tlist, environment, expbegin, expend); if (subvalue != null) { values.Add(subvalue); bTest = true; } else { return false; } } } if (!bTest && tlist[expbegin].type == TokenType.IDENTIFIER) { if (expend == expbegin + 1)//一元表达式 { ICLS_Expression subvalue = Compiler_Expression_MathSelf(tlist, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } if (!bTest && tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == "=")//赋值表达式 { ICLS_Expression subvalue = Compiler_Expression_Set(tlist, environment,expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } } if (!bTest && (tlist[expbegin].type == TokenType.IDENTIFIER || tlist[expbegin].type == TokenType.VALUE || tlist[expbegin].type == TokenType.STRING)) { //算数表达式 ICLS_Expression subvalue = Compiler_Expression_Math(tlist, environment, expbegin, expend); if (null != subvalue) { values.Add(subvalue); bTest = true; } } if (!bTest && tlist[expbegin].type == TokenType.KEYWORD) { if (tlist[expbegin].text == "for") { ICLS_Expression subvalue = Compiler_Expression_Loop_For(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "foreach") { ICLS_Expression subvalue = Compiler_Expression_Loop_ForEach(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "while") { ICLS_Expression subvalue = Compiler_Expression_Loop_While(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "do") { ICLS_Expression subvalue = Compiler_Expression_Loop_Dowhile(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "if") { ICLS_Expression subvalue = Compiler_Expression_Loop_If(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "return") { ICLS_Expression subvalue = Compiler_Expression_Loop_Return(tlist, environment,expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "throw") { ICLS_Expression subvalue = Compiler_Expression_FunctionThrow(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if(tlist[expbegin].text=="true"||tlist[expbegin].text=="false"||tlist[expbegin].text=="null") { //算数表达式 ICLS_Expression subvalue = Compiler_Expression_Math(tlist, environment, expbegin, expend); if (null != subvalue) { values.Add(subvalue); bTest = true; } } else if(tlist[expbegin].text=="new") { //new 表达式 if (tlist[expbegin + 1].type == TokenType.TYPE) { ICLS_Expression subvalue = Compiler_Expression_FunctionNew(tlist, environment, pos, posend); values.Add(subvalue); bTest = true; } } else if (tlist[expbegin].text == "yield") { ICLS_Expression subvalue = Compiler_Expression_FunctionYield(tlist, environment, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "try") { LogError(tlist, "try 表达式不支持也没必要", expbegin, expend); return false; } else { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } if (!bTest) { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } begin = end + 1; } while (begin <= posend); if (values.Count == 1) { value = values[0]; } else if (values.Count > 1) { LogError(tlist, "异常表达式", pos, posend); } return true; }
public void OutStack(ICLS_Expression expr) { stackExpr.Pop(); }
public CLS_Content.Value ExecuteCode(string code, bool SimpleExpression = false, CLS_Content content = null) { ICLS_Expression expr = CompilerCode(code, SimpleExpression); return(Execute(expr, content)); }
public void InStack(ICLS_Expression expr) { if (!useDebug) return; if (stacklist.Count>0&&stacklist.Peek() == expr) { throw new Exception("InStack error"); } stacklist.Push(expr); }
public ICLS_Expression Optimize(ICLS_Expression old) { return(compiler.Optimize(old, contentGloabl)); }
//CLS_Content contentGloabl = null; public ICLS_Expression Expr_Optimize(ICLS_Expression old) { return compiler.Optimize(old, this); }
//不出Block,必须一次解析完,括号为优先级 public bool Compiler_Expression(IList<Token> tlist, ICLS_Environment content, int pos, int posend, out ICLS_Expression value) { if(pos>posend) { value = null; return false; } int begin = pos; value = null; List<ICLS_Expression> values = new List<ICLS_Expression>(); do { if (tlist[begin].type == TokenType.COMMENT) { begin++; continue; } if (tlist[begin].type == TokenType.PUNCTUATION && tlist[begin].text == ";") { begin++; continue; } int bdep; //脱一次壳 int end = FindCodeAny(tlist, ref begin, out bdep); if (end > posend) { end = posend; } else if (end < posend) { bool bMath = false; for (int i = end + 1; i <= posend; i++) { if (tlist[i].type == TokenType.COMMENT) continue; if (tlist[i].type == TokenType.PUNCTUATION && tlist[i].text == ";") continue; bMath = true; break; } if (bMath) { end = posend; //如果表达式一次搞不完,那肯定是优先级问题 value = Compiler_Expression_Math(tlist,content, begin, posend); return true; } } //else //{ // IList<int> i = SplitExpressionWithOp(tlist, begin, end); // if (i != null && i.Count > 0) // { // value = Compiler_Expression_Math(tlist, begin, posend); // return true; // } //} int expend = end; int expbegin = begin; if (expbegin > expend) return true; if (expend == expbegin) {//simple if (tlist[expbegin].type == TokenType.KEYWORD) { if (tlist[expbegin].text == "return") { ICLS_Expression subvalue = Compiler_Expression_Loop_Return(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "表达式编译失败", expbegin, expend); return false; } else values.Add(subvalue); } else if (tlist[expbegin].text == "break") { ICLS_Expression subvalue = Compiler_Expression_Loop_Break(tlist, expbegin); if (null == subvalue) { //LogError(tlist, "表达式编译失败", expbegin, expend); return false; } else values.Add(subvalue); } else if (tlist[expbegin].text == "continue") { ICLS_Expression subvalue = Compiler_Expression_Loop_Continue(tlist, expbegin); if (null == subvalue) return false; else values.Add(subvalue); } else if (tlist[expbegin].text == "true") { CLS_Value_Value<bool> subvalue = new CLS_Value_Value<bool>(); subvalue.value_value = true; values.Add(subvalue); } else if (tlist[expbegin].text == "false") { CLS_Value_Value<bool> subvalue = new CLS_Value_Value<bool>(); subvalue.value_value = false; values.Add(subvalue); } else if (tlist[expbegin].text == "null") { CLS_Value_Null subvalue = new CLS_Value_Null(); values.Add(subvalue); } } else { ICLS_Expression subvalue = Compiler_Expression_Value(tlist[expbegin],expbegin); if (null == subvalue) return false; else values.Add(subvalue); } } else if (bdep == 1) //深层表达式 { expbegin++; expend--; ICLS_Expression subvalue; bool bsucc = Compiler_Expression(tlist,content, expbegin, expend, out subvalue); if (bsucc) { if (subvalue != null) values.Add(subvalue); } else { LogError(tlist, "表达式编译失败", expbegin, expend); return false; } } else //尝试各种表达式 { bool bTest = false; //取反表达式 if (tlist[expbegin].type == TokenType.PUNCTUATION && tlist[expbegin].text == "-") { if (tlist[expend].type == TokenType.VALUE) {//负数 if (expend == expbegin + 1) { ICLS_Expression subvalue = Compiler_Expression_SubValue(tlist[expend]); if (null == subvalue) { return false; } else values.Add(subvalue); } else { ICLS_Expression subvalue = Compiler_Expression_Math(tlist, content, begin, posend); if (null == subvalue) { LogError(tlist, "表达式编译失败", begin, posend); return false; } else values.Add(subvalue); } } else {//负数表达式 ICLS_Expression subvalue = Compiler_Expression_NegativeValue(tlist, content, expbegin + 1, expend); if (null == subvalue) { LogError(tlist, "表达式编译失败", expbegin + 1, expend); return false; } else values.Add(subvalue); } bTest = true; } if (tlist[expbegin].type == TokenType.PUNCTUATION && tlist[expbegin].text == "!") {//逻辑反表达式 ICLS_Expression subvalue = Compiler_Expression_NegativeLogic(tlist, content, expbegin + 1, expend); if (null == subvalue) { LogError(tlist, "表达式编译失败", expbegin + 1, expend); return false; } else values.Add(subvalue); bTest = true; } if (!bTest && tlist[expbegin].type == TokenType.TYPE) { if (tlist[expbegin + 1].type == TokenType.IDENTIFIER)//定义表达式或者定义并赋值表达式 { if (expend == expbegin + 1)//定义表达式 { ICLS_Expression subvalue = Compiler_Expression_Define(tlist, content, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (expend > expbegin + 2 && tlist[expbegin + 2].type == TokenType.PUNCTUATION && tlist[expbegin + 2].text == "=") {//定义并赋值表达式 ICLS_Expression subvalue = Compiler_Expression_DefineAndSet(tlist, content, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else { LogError(tlist,"无法识别的表达式:", expbegin ,expend); return false; } } else if (tlist[expbegin + 1].text == "[" && tlist[expbegin + 2].text == "]" && tlist[expbegin + 3].type == TokenType.IDENTIFIER)//定义表达式或者定义并赋值表达式 { if (expend == expbegin + 3)//定义表达式 { ICLS_Expression subvalue = Compiler_Expression_DefineArray(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "无法识别的数组:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else if (expend > expbegin + 4 && tlist[expbegin + 4].type == TokenType.PUNCTUATION && tlist[expbegin + 4].text == "=") {//定义并赋值表达式 ICLS_Expression subvalue = Compiler_Expression_DefineAndSetArray(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "无法识别的数组:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } else if (tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == ".") {//静态调用表达式 //if (expend - expbegin > 2) { ICLS_Expression subvalue = Compiler_Expression_Math(tlist, content, expbegin, expend); if (subvalue != null) { //subvalue.listParam.Add(subparam); values.Add(subvalue); bTest = true; } else { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } } } if (!bTest && tlist[expbegin].type == TokenType.IDENTIFIER) { if (expend == expbegin + 1)//一元表达式 { ICLS_Expression subvalue = Compiler_Expression_MathSelf(tlist, expbegin, expend); if (null == subvalue) { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } if (!bTest && tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == "=")//赋值表达式 { ICLS_Expression subvalue = Compiler_Expression_Set(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } //if (!bTest && tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == "(")//函数表达式 //{ // ICLS_Expression subvalue = Compiler_Expression_Function(tlist,content, expbegin, expend); // if (null == subvalue) return false; // else // values.Add(subvalue); // bTest = true; //} //if (!bTest && tlist[expbegin + 1].type == TokenType.PUNCTUATION && tlist[expbegin + 1].text == "[")//函数表达式 //{ // ICLS_Expression subvalue = Compiler_Expression_IndexFind(tlist, content, expbegin, expend); // if (null == subvalue) return false; // else // values.Add(subvalue); // bTest = true; //} } if (!bTest && (tlist[expbegin].type == TokenType.IDENTIFIER || tlist[expbegin].type == TokenType.VALUE || tlist[expbegin].type == TokenType.STRING)) { //算数表达式 ICLS_Expression subvalue = Compiler_Expression_Math(tlist,content, expbegin, expend); if (null != subvalue) { values.Add(subvalue); bTest = true; } } if (!bTest && tlist[expbegin].type == TokenType.KEYWORD) { if (tlist[expbegin].text == "for") { ICLS_Expression subvalue = Compiler_Expression_Loop_For(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "不可识别的For头:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "foreach") { ICLS_Expression subvalue = Compiler_Expression_Loop_ForEach(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "不可识别的ForEach头:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "while") { ICLS_Expression subvalue = Compiler_Expression_Loop_While(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "不可识别的while头:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "do") { ICLS_Expression subvalue = Compiler_Expression_Loop_Dowhile(tlist, content, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "if") { ICLS_Expression subvalue = Compiler_Expression_Loop_If(tlist, content, expbegin, expend); if (null == subvalue) { LogError(tlist, "不可识别的if判断:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "try") { ICLS_Expression subvalue = Compiler_Expression_Loop_Try(tlist, content, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "return") { ICLS_Expression subvalue = Compiler_Expression_Loop_Return(tlist, content,expbegin, expend); if (null == subvalue) { LogError(tlist, "不可识别的return:", expbegin, expend); return false; } else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "trace") { ICLS_Expression subvalue = Compiler_Expression_FunctionTrace(tlist,content, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if (tlist[expbegin].text == "throw") { ICLS_Expression subvalue = Compiler_Expression_FunctionThrow(tlist, content, expbegin, expend); if (null == subvalue) return false; else values.Add(subvalue); bTest = true; } else if(tlist[expbegin].text=="true"||tlist[expbegin].text=="false"||tlist[expbegin].text=="null") { //算数表达式 ICLS_Expression subvalue = Compiler_Expression_Math(tlist, content, expbegin, expend); if (null != subvalue) { values.Add(subvalue); bTest = true; } } else if(tlist[expbegin].text=="new") { //new 表达式 if (tlist[expbegin + 1].type == TokenType.TYPE) { ICLS_Expression subvalue = Compiler_Expression_FunctionNew(tlist, content, pos, posend); values.Add(subvalue); bTest = true; } } else { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } if (!bTest) { LogError(tlist, "无法识别的表达式:", expbegin, expend); return false; } } begin = end + 1; } while (begin <= posend); if (values.Count == 1) { value = values[0]; } else if (values.Count > 1) { LogError(tlist, "异常表达式", pos, posend); } return true; }
public ICLS_Expression Optimize(ICLS_Expression value, CLS_Content content) { ICLS_Expression expr = value as ICLS_Expression; if (expr == null) return value; else return OptimizeDepth(expr, content); }
public void Restore(List<string> depth, ICLS_Expression expr) { while(tvalues.Peek()!=depth) { tvalues.Pop(); } while(stackExpr.Peek()!=expr) { stackExpr.Pop(); } }
public void InStack(ICLS_Expression expr) { stackExpr.Add(expr); }
ICLS_Expression OptimizeSingle(ICLS_Expression expr, CLS_Content content) { if (expr is CLS_Expression_Math2Value || expr is CLS_Expression_Math2ValueAndOr || expr is CLS_Expression_Math2ValueLogic) { if (expr.listParam[0] is ICLS_Value && expr.listParam[1] is ICLS_Value) { CLS_Content.Value result = expr.ComputeValue(content); if ((Type)result.type == typeof(bool)) { CLS_Value_Value<bool> value = new CLS_Value_Value<bool>(); value.value_value = (bool)result.value; value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return value; } else { ICLS_Type v = content.environment.GetType(result.type); ICLS_Value value = v.MakeValue(result.value); value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return value; } } } if (expr is CLS_Expression_Math3Value) { CLS_Content.Value result = expr.listParam[0].ComputeValue(content); if ((Type)result.type == typeof(bool)) { bool bv = (bool)result.value; if (bv) return expr.listParam[1]; else return expr.listParam[2]; } } return expr; }
public CLS_Content.Value Expr_Execute(ICLS_Expression expr) { CLS_Content content = CreateContent(); return expr.ComputeValue(content); }