diff --git a/parseManager/Program.cs b/parseManager/Program.cs index f79500a..4eb310b 100644 --- a/parseManager/Program.cs +++ b/parseManager/Program.cs @@ -8,6 +8,7 @@ */ using System; using parseManager; // IMPORTANT +using NCalc; public class define : standardDefine // If you want the standard methods you must include this, Also this class cannot be static! { public void testM(object arg1) @@ -42,7 +43,8 @@ namespace parseManager { public static void Main(string[] args) { - parseManager test = new parseManager("parsetest2.txt", "define"); // define is where your methods will be held + + parseManager test = new parseManager("parsetest2.txt"); // define is where your methods will be held var env = test.GetENV(); env["test"]="TEST!"; env["test2"]=12345; @@ -56,7 +58,6 @@ namespace parseManager } next = test.Next(); } - //var temp=test.InvokeR("TEST",new object[]{}); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } diff --git a/parseManager/bin/Debug/parsetest2.txt b/parseManager/bin/Debug/parsetest2.txt index 053f31e..0128c1c 100644 --- a/parseManager/bin/Debug/parsetest2.txt +++ b/parseManager/bin/Debug/parsetest2.txt @@ -5,7 +5,9 @@ ENABLE leaking } [START]{ "Test 1:" - num=ADD(5,5) + t=15 + test=2+t + "test = $test$" "num = $num$" c=5 ::HERE:: diff --git a/parseManager/parseManager.cs b/parseManager/parseManager.cs index 983bcb1..627ed93 100644 --- a/parseManager/parseManager.cs +++ b/parseManager/parseManager.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; using System.Reflection; using parseManager; +using NCalc; namespace parseManager { /// @@ -19,8 +20,8 @@ namespace parseManager /// public class parseManager { + standardDefine _invoke = new standardDefine(); string _filepath; - bool _hasDefine; bool _active = true; string _define; string _entry = "START"; @@ -34,6 +35,27 @@ namespace parseManager Dictionary _flags = new Dictionary(); Dictionary _chunks = new Dictionary(); Dictionary _methods = new Dictionary(); + public parseManager(string filepath) + { + InitFlags(); + _filepath = filepath; + _defineType = Type.GetType("standardDefine"); + ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); + _defineClassObject = defineConstructor.Invoke(new object[]{ }); + _defualtENV = _mainENV; + Parse(); + } + public parseManager(string filepath, string define) + { + InitFlags(); + _define = define; + _filepath = filepath; + _defineType = Type.GetType(define); + ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); + _defineClassObject = defineConstructor.Invoke(new object[]{ }); + _defualtENV = _mainENV; + Parse(); + } void InitFlags() { _flags.Add("leaking", false); @@ -41,9 +63,10 @@ namespace parseManager _flags.Add("debugging", false); _flags.Add("topdown", true); } - public bool GetFlag(string flag){ + public bool GetFlag(string flag) + { bool f; - if(_flags.TryGetValue(flag,out f)){ + if (_flags.TryGetValue(flag, out f)) { return f; } return false; @@ -65,7 +88,7 @@ namespace parseManager _entry = m.Groups[1].ToString(); } var match = Regex.Matches(data, @"\[(.+)\][\r\n]*?\{([^\0]+?)\}"); - var count=0; + var count = 0; foreach (Match m in match) { string Blck = m.Groups[1].ToString(); string Cont = m.Groups[2].ToString(); @@ -104,13 +127,13 @@ namespace parseManager { _Parse(code); } - public chunk[] GetChunks(){ + public chunk[] GetChunks() + { var chunks = _chunks.Values; var temp = new chunk[_chunks.Count]; - var i=0; - foreach(var item in _chunks) - { - temp[i]=item.Value; + var i = 0; + foreach (var item in _chunks) { + temp[i] = item.Value; i++; } return temp; @@ -123,40 +146,26 @@ namespace parseManager { _active = false; } - public parseManager(string filepath) - { - InitFlags(); - _filepath = filepath; - _hasDefine = false; - _defualtENV = _mainENV; - Parse(); - } - public parseManager(string filepath, string define) - { - InitFlags(); - _define = define; - _hasDefine = true; - _filepath = filepath; - _defineType = Type.GetType(define); - ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); - _defineClassObject = defineConstructor.Invoke(new object[]{ }); - _defualtENV = _mainENV; - Parse(); - } public object InvokeR(string method, object[] args) - { // TODO collect the returned arguments if any - if (!_hasDefine) - return null; + { + //try{ _defineMethod = _defineType.GetMethod(method); return _defineMethod.Invoke(_defineClassObject, args); +// } catch { +// PushError("Invalid method: "+method); +// return null; +// } } public int InvokeNR(string method, object[] args) - { // Simple Invoking! - if (!_hasDefine) - return -1; - _defineMethod = _defineType.GetMethod(method); - _defineMethod.Invoke(_defineClassObject, args); - return 0; + { + try { + _defineMethod = _defineType.GetMethod(method); + _defineMethod.Invoke(_defineClassObject, args); + return 0; + } catch { + PushError("Invalid method: " + method); + } + return -1; } public void SetBlock(string BLOCK) { @@ -217,8 +226,8 @@ namespace parseManager object[] stuff; if (cCMD == null) { if (_flags["leaking"] && _active) { - var test=_currentChunk.GetNextChunk(); - if(test!=null){ + var test = _currentChunk.GetNextChunk(); + if (test != null) { SetBlock(_currentChunk.GetNextChunk()); return Next(); } @@ -229,64 +238,62 @@ namespace parseManager } var type = cCMD.GetCMDType(); stuff = cCMD.GetArgs(); - if(type=="LOGIC"){//{conds,andors,_funcif,_resultif,_funcelse,_resultelse} - var conds=(string[])stuff[0]; - var andors=(string[])stuff[1]; - var funcif=(string)stuff[2]; - var argsif=(string[])stuff[3]; - var funcelse=(string)stuff[4]; - var argselse=(string[])stuff[5]; - var objs=new object[conds.Length]; // contain the actual values of what is in the env - var truths= new bool[conds.Length/3]; - var c=0; - //Console.WriteLine(string.Join(",",conds)); - //Console.WriteLine(string.Join(",",andors)); - for(int i=0;i="){ - truths[c] = (double)condA>=(double)condB; - } else if(e=="<="){ - truths[c] = (double)condA<=(double)condB; - } else if(e=="!=" || e=="~="){ - truths[c] = condA.ToString()!=condB.ToString(); - } else if(e==">"){ - truths[c] = (double)condA>(double)condB; - } else if(e=="<"){ - truths[c] = (double)condA<(double)condB; + if (type == "LOGIC") {//{conds,andors,_funcif,_resultif,_funcelse,_resultelse} + var conds = (string[])stuff[0]; + var andors = (string[])stuff[1]; + var funcif = (string)stuff[2]; + var argsif = (string[])stuff[3]; + var funcelse = (string)stuff[4]; + var argselse = (string[])stuff[5]; + var objs = new object[conds.Length]; // contain the actual values of what is in the env + var truths = new bool[conds.Length / 3]; + var c = 0; + for (int i = 0; i < conds.Length; i += 3) { + var condA = (object)ResolveVar(new []{ conds[i] })[0]; + var e = conds[i + 1]; + var condB = (object)ResolveVar(new []{ conds[i + 2] })[0]; + if (e == "==") { + truths[c] = condA.ToString() == condB.ToString(); + } else if (e == ">=") { + truths[c] = (double)condA >= (double)condB; + } else if (e == "<=") { + truths[c] = (double)condA <= (double)condB; + } else if (e == "!=" || e == "~=") { + truths[c] = condA.ToString() != condB.ToString(); + } else if (e == ">") { + truths[c] = (double)condA > (double)condB; + } else if (e == "<") { + truths[c] = (double)condA < (double)condB; } else { - PushError("Invalid conditional test! "+e+" is not valid!"); + PushError("Invalid conditional test! " + e + " is not valid!"); } c++; } - var truth=truths[0]; - if(truths.Length==1 && truth){ + var truth = truths[0]; + if (truths.Length == 1 && truth) { InvokeNR(funcif, ResolveVar(argsif)); - } else if(truths.Length==1) { + } else if (truths.Length == 1) { InvokeNR(funcelse, ResolveVar(argselse)); } else { - for(int i=1;i<=]+)+(.+)"); - var s1a=s1p.Groups[1].ToString(); - var s1b=s1p.Groups[2].ToString(); - var s1c=s1p.Groups[3].ToString(); + var s1p = Regex.Match(s1, "(.+?)([~!><=]+)+(.+)"); + var s1a = s1p.Groups[1].ToString(); + var s1b = s1p.Groups[2].ToString(); + var s1c = s1p.Groups[3].ToString(); var s2 = m.Groups[2].ToString(); - conds[p++]=s1a; - conds[p++]=s1b; - conds[p++]=s1c; - andors[p2++]=s2.Substring(1,1); - count+=s1.Length+s2.Length; + conds[p++] = s1a; + conds[p++] = s1b; + conds[p++] = s1c; + andors[p2++] = s2.Substring(1, 1); + count += s1.Length + s2.Length; } - var s1p2 = Regex.Match(condition.Substring(count,condition.Length-count-1),"(.+?)([~!><=]+)+(.+)"); - var s1a2=s1p2.Groups[1].ToString(); - var s1b2=s1p2.Groups[2].ToString(); - var s1c2=s1p2.Groups[3].ToString(); + var s1p2 = Regex.Match(condition.Substring(count, condition.Length - count - 1), "(.+?)([~!><=]+)+(.+)"); + var s1a2 = s1p2.Groups[1].ToString(); + var s1b2 = s1p2.Groups[2].ToString(); + var s1c2 = s1p2.Groups[3].ToString(); //Console.WriteLine(s1a2+"|"+s1b2+"|"+s1c2); - conds[p++]=s1a2; - conds[p++]=s1b2; - conds[p++]=s1c2; - _compiledlines.Add(new CMD("LOGIC", new object[]{conds,andors,_funcif,_resultif,_funcelse,_resultelse})); + conds[p++] = s1a2; + conds[p++] = s1b2; + conds[p++] = s1c2; + _compiledlines.Add(new CMD("LOGIC", new object[] { + conds, + andors, + _funcif, + _resultif, + _funcelse, + _resultelse + })); } else if (label.ToString() != "") { _labels[label.Groups[1].ToString()] = i; _compiledlines.Add(new CMD("LABEL", new object[]{ })); @@ -483,7 +506,11 @@ namespace parseManager var args = (FuncWReturn.Groups[3]).ToString(); var retargs = var1.Split(','); var result = Regex.Split(args, ",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)"); - _compiledlines.Add(new CMD("FUNC_R", new object[] { retargs, func, result })); + _compiledlines.Add(new CMD("FUNC_R", new object[] { + retargs, + func, + result + })); } else if (FuncWOReturn.ToString() != "") { var func = (FuncWOReturn.Groups[1]).ToString(); var args = (FuncWOReturn.Groups[2]).ToString(); @@ -512,7 +539,8 @@ namespace parseManager _type = "CODEBLOCK"; _clean(cont); } - public string GetName(){ + public string GetName() + { return _BLOCK; } public int GetLabel(string lab) @@ -522,14 +550,15 @@ namespace parseManager public bool TryGetLabel(string lab, out int pos) { int p; - if(_labels.TryGetValue(lab, out p)){ - pos=p; + if (_labels.TryGetValue(lab, out p)) { + pos = p; return true; } - pos=-1; + pos = -1; return false; } - public void RemoveNextChunk(){ + public void RemoveNextChunk() + { _next = null; } public void SetNextChunk(chunk next) @@ -705,23 +734,43 @@ namespace parseManager */ static class GLOBALS { - static parseManager current; - static readonly ENV env = new ENV(); + static standardDefine _define = new standardDefine(); + static parseManager _current; + static readonly ENV _env = new ENV(); + static List _numvars = new List(); public static object GetData(string ind) { - return env[ind]; + return _env[ind]; + } + public static standardDefine GetDefine() + { + return _define; } public static void AddData(string ind, object data) { - env[ind] = data; + _env[ind] = data; } public static void SetPM(parseManager o) { - current = o; + _current = o; } public static parseManager GetPM() { - return current; + return _current; + } + public static void Add_Var(string var) + { + if (!_numvars.Contains(var)) { + _numvars.Add(var); + } + } + public static void Remove_Var(string var) + { + _numvars.Remove(var); + } + public static string[] GetVars() + { + return _numvars.ToArray(); } } } @@ -740,20 +789,20 @@ public class standardDefine var test = GLOBALS.GetPM(); var c = test.GetCurrentChunk(); int pos; - if(c.TryGetLabel(label, out pos)){ + if (c.TryGetLabel(label, out pos)) { c.SetPos(pos); return 0; - } else if(test.GetFlag("forseelabels")){ + } else if (test.GetFlag("forseelabels")) { var chunks = test.GetChunks(); - for(int i=0;i 4.0 + + bin\Debug\NCalc.dll + 3.5