public override Expression.EvalResult Execute(MakeState s) { // run initializer Expression.EvalResult ret = init.Execute(s); if (ret.AsInt != 0) { return(ret); } while (true) { // check condition if (test.Evaluate(s).AsInt == 0) { break; } // exec code ret = code.Execute(s); if (ret.AsInt != 0) { return(ret); } if (s.returns != null) { return(new Expression.EvalResult(0)); } // run incrementer incr.Execute(s); } return(new Expression.EvalResult(0)); }
private List <string> MangleAll(MakeState s) { var cache_key = Mangle(s); List <string> ret; if (mangle_all_cache.TryGetValue(cache_key, out ret)) { return(ret); } List <Expression.EvalResult> ers = new List <EvalResult>(); foreach (var arg in args) { ers.Add(arg.Evaluate(s)); } ret = new List <string>(); for (int i = 0; i <= args.Count; i++) { MangleAllLine(i, 0, ret, ers); } mangle_all_cache[cache_key] = ret; return(ret); }
public override EvalResult Evaluate(MakeState s) { EvalResult elabel = label.Evaluate(s); EvalResult eindex = index.Evaluate(s); switch (elabel.Type) { case EvalResult.ResultType.String: return(new EvalResult(new string(elabel.strval[(int)eindex.AsInt], 1))); case EvalResult.ResultType.Array: { var idx = eindex.AsInt; if (idx < 0 || idx >= elabel.arrval.Count) { return new EvalResult { Type = EvalResult.ResultType.Null } } ; else { return(elabel.arrval[(int)idx]); } } case EvalResult.ResultType.Object: return(elabel.objval[eindex.strval]); default: throw new Statement.SyntaxException("indexing cannot be applied to object of type: " + elabel.Type.ToString()); } }
public override Expression.EvalResult Execute(MakeState s) { Expression.EvalResult e = enumeration.Evaluate(s); if (e.Type != Expression.EvalResult.ResultType.Array) { throw new Exception("does not evaluate to array"); } foreach (Expression.EvalResult i in e.arrval) { //MakeState cur_s = s.Clone(); var cur_s = s; cur_s.SetLocalDefine(val, i); Expression.EvalResult ret = code.Execute(cur_s); if (ret.AsInt != 0) { return(ret); } if (cur_s.returns != null) { s.returns = cur_s.returns; return(new Expression.EvalResult(0)); } } return(new Expression.EvalResult(0)); }
public override EvalResult Evaluate(MakeState s) { FunctionStatement fs = new FunctionStatement(); fs.name = name; fs.args = new List <FunctionStatement.FunctionArg>(); foreach (var arg in args) { fs.args.Add(new FunctionStatement.FunctionArg { argtype = arg }); } var mangled = fs.Mangle(); if (s.funcs.ContainsKey(mangled)) { fs.args = s.funcs[mangled].args; fs.code = s.funcs[mangled].code; return(new EvalResult(fs)); } else { return(new EvalResult()); } }
public virtual Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args) { MakeState new_s = s.Clone(); new_s.ClearLocalDefines(); for (int i = 0; i < args.Count; i++) { if (args[i].argtype == Expression.EvalResult.ResultType.Function) { FunctionStatement fs = new FunctionStatement(); var ofs = passed_args[i].funcval; fs.name = args[i].name; fs.args = ofs.args; fs.code = ofs.code; new_s.funcs[fs.Mangle()] = fs; } else { new_s.SetLocalDefine(args[i].name, passed_args[i]); } } code.Execute(new_s); if (new_s.returns != null) { return(new_s.returns); } return(new Expression.EvalResult()); }
public override Expression.EvalResult Execute(MakeState s) { var f = include_file.Evaluate(s).strval; MakeState new_s = s.Clone(); return(Program.ExecuteFile(f, new_s)); }
public override Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args) { var n = passed_args[0].strval; s.SetDefine(n, passed_args[1], true); all_defs[n] = passed_args[1]; return(passed_args[1]); }
public override EvalResult Evaluate(MakeState s) { List <EvalResult> ret = new List <EvalResult>(); foreach (Expression e in val) { ret.Add(e.Evaluate(s)); } return(new EvalResult(ret)); }
public string Mangle(MakeState s) { List <Expression.EvalResult> ers = new List <EvalResult>(); foreach (var arg in args) { ers.Add(arg.Evaluate(s)); } return(Mangle(target, ers)); }
public override EvalResult Evaluate(MakeState s) { Dictionary <string, EvalResult> ret = new Dictionary <string, EvalResult>(); foreach (ObjDef o in val) { ret[o.name] = o.val.Evaluate(s); } return(new EvalResult(ret)); }
public override Expression.EvalResult Execute(MakeState s) { Expression.EvalResult e = new Expression.EvalResult(val); s.SetDefine(tok_name, e, assignop); if (export) { ExportDef(tok_name, s); } return(new Expression.EvalResult(0)); }
internal static void ExportDef(string tag, MakeState s) { Expression.EvalResult e = s.GetDefine(tag); MakeState cur_s = s.parent; while (cur_s != null) { cur_s.SetDefine(tag, e); cur_s = cur_s.parent; } }
public override Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args) { var obj_type = passed_args[0].strval; var msg = passed_args[1].strval; var obj_ts = Type.GetType(obj_type); var obj_ctor = obj_ts.GetConstructor(new Type[] { typeof(string) }); var obj = obj_ctor.Invoke(new object[] { msg }); throw obj as Exception; }
internal static Expression.EvalResult ExecuteFile(string name, MakeState s) { // find the file by using search paths FileInfo fi = null; foreach (var sp in s.search_paths) { var test = sp + "/" + name; var test2 = sp + name; try { fi = new FileInfo(test); if (fi.Exists) { break; } } catch (Exception) { } try { fi = new FileInfo(test2); if (fi.Exists) { break; } } catch (Exception) { } } if (fi == null || fi.Exists == false) { throw new Exception("included file: " + name + " not found"); } // add included files location to search paths s.search_paths.Insert(0, fi.DirectoryName); FileStream f = fi.OpenRead(); Parser p = new Parser(new Scanner(f, fi.FullName)); bool res = p.Parse(); if (res == false) { throw new Exception("Parse error"); } var ret = p.output.Execute(s); f.Close(); return(ret); }
public override Expression.EvalResult Execute(MakeState s) { LabelExpression le = new LabelExpression { val = val }; Expression.EvalResult e = le.Evaluate(s); s.SetDefine(tok_name, e, assignop); if (export) { ExportDef(tok_name, s); } return(new Expression.EvalResult(0)); }
public override EvalResult Evaluate(MakeState s) { /* First, parse variables in the shell command */ StringBuilder sb = new StringBuilder(); int i = 0; while (i < val.Length) { if (val[i] == '\\' && i < (val.Length - 1)) { sb.Append(val[i++]); sb.Append(val[i++]); } else if (val[i] == '$') { /* Now try and match the largest possible variable we can */ int j = i + 1; string match = null; while (j < (val.Length) && val[j] != '$') { string test = val.Substring(i + 1, j - i); if (s.IsDefined(test)) { match = test; } j++; } if (match != null) { sb.Append(s.GetDefine(match).strval); i++; i += match.Length; } else { sb.Append(val[i++]); } } else { sb.Append(val[i++]); } } return(new EvalResult(sb.ToString())); }
public void SetDefine(string tag, Expression.EvalResult e, bool export) { if (export == false) { SetLocalDefine(tag, e); } else { MakeState s = this; while (s != null) { s.SetDefine(tag, e); s = s.parent; } } }
public override Expression.EvalResult Execute(MakeState s) { string mangledname = Mangle(); s.funcs[mangledname] = this; if (export) { MakeState cur_s = s.parent; while (cur_s != null) { cur_s.funcs[mangledname] = this; cur_s = cur_s.parent; } } return(new Expression.EvalResult(0)); }
public override Expression.EvalResult Execute(MakeState s) { if (test.Evaluate(s).AsInt == 0) { if (else_block != null) { return(else_block.Execute(s)); } } else { if (if_block != null) { return(if_block.Execute(s)); } } return(new Expression.EvalResult(0)); }
public override Expression.EvalResult Execute(MakeState s) { if (list != null) { foreach (Statement st in list) { Expression.EvalResult er = st.Execute(s); if (!(st is ExpressionStatement) && er.AsInt != 0) { return(er); } if (s.returns != null) { return(new Expression.EvalResult(0)); } } } return(new Expression.EvalResult(0)); }
public override Expression.EvalResult Execute(MakeState s) { if (s.IsDefined(v) == false) { throw new Exception("export: variable " + v + " is not defined in this scope"); } Expression.EvalResult e = s.GetDefine(v); s.SetDefine(v, e); MakeState cur_s = s.parent; while (cur_s != null) { cur_s.SetDefine(v, e); cur_s = cur_s.parent; } return(new Expression.EvalResult(0)); }
public override EvalResult Evaluate(MakeState s) { List <string> mangle_all = MangleAll(s); foreach (var mangled_name in mangle_all) { if (s.funcs.ContainsKey(mangled_name)) { List <EvalResult> args_to_pass = new List <EvalResult>(); foreach (Expression arg in args) { args_to_pass.Add(arg.Evaluate(s)); } return(s.funcs[mangled_name].Run(s, args_to_pass)); } } throw new Statement.SyntaxException("unable to find function " + Mangle(s)); }
public override Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args) { var name = passed_args[0].strval; var key = passed_args[1].arrval; var value = passed_args[2].arrval; /* Coerce key and value to byte arrays */ var key_b = ToByteArray(key); var value_b = ToByteArray(value); /* Get hash code */ var hc = Hash(key_b); /* Get hash table */ HTable ht; if (tables.TryGetValue(name, out ht) == false) { ht = new HTable(); tables[name] = ht; } /* Build table entry */ HTable.KeyIndex k = new HTable.KeyIndex(); k.hc = hc; k.idx = ht.data.Count; k.key = key_b; ht.keys.Add(k); /* Add data entry */ if (key_b.Count > 255) { throw new Exception("key too large"); } ht.data.Add((byte)key_b.Count); ht.data.AddRange(key_b); ht.data.AddRange(value_b); return(new Expression.EvalResult()); }
private Expression.EvalResult follow_chain(Expression.EvalResult o, List <Expression> chain, int cur_idx, int max_idx, MakeState s) { var next_member = chain[cur_idx]; Expression.EvalResult next_result = null; if (next_member is LabelIndexedExpression) { var lie = next_member as LabelIndexedExpression; if (o.Type != Expression.EvalResult.ResultType.Array) { throw new Exception(); } next_result = o.arrval[(int)lie.index.Evaluate(s).AsInt]; } else if (next_member is LabelMemberExpression) { var lme = next_member as LabelMemberExpression; if (o.Type != Expression.EvalResult.ResultType.Object) { throw new Exception(); } next_result = o.objval[((LabelExpression)lme.member).val]; } else { throw new NotImplementedException(); } cur_idx++; if (cur_idx == max_idx) { return(next_result); } return(follow_chain(next_result, chain, cur_idx, max_idx, s)); }
public override EvalResult Evaluate(MakeState s) { if (s.IsDefined(val)) { return(s.GetDefine(val)); } else if (s.funcs.ContainsKey(val)) { return new EvalResult { Type = EvalResult.ResultType.Function, funcval = new FunctionStatement { name = val } } } ; else { return(new EvalResult()); } }
public MakeState Clone() { MakeState other = new MakeState(); foreach (KeyValuePair <string, Expression.EvalResult> kvp in defs) { other.defs[kvp.Key] = kvp.Value; } foreach (KeyValuePair <string, Expression.EvalResult> kvp in local_defs) { other.local_defs[kvp.Key] = kvp.Value; } foreach (KeyValuePair <string, FunctionStatement> kvp in funcs) { other.funcs[kvp.Key] = kvp.Value; } other.parent = this; other.search_paths = new List <string>(search_paths); return(other); }
public override Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args) { IList <byte> ret = GetBytes(passed_args[0]); if (ret == null) { throw new Exception("Cannot call " + name + " with " + passed_args[0].ToString()); } List <Expression.EvalResult> r = new List <Expression.EvalResult>(); for (int i = 0; i < bc; i++) { if (i < ret.Count) { r.Add(new Expression.EvalResult(ret[i])); } else { r.Add(new Expression.EvalResult(0)); } } return(new Expression.EvalResult(r)); }
public override Expression.EvalResult Execute(MakeState s) { throw new NotImplementedException(); }
public override Expression.EvalResult Execute(MakeState s) { return(expr.Evaluate(s)); }