From 70fa89de8c6dd08748e1708f99d519bf48b25358 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Aug 2017 20:53:49 -0400 Subject: [PATCH] Added threading support and fixed bugs Internal functions fixed! now empty args on internal functions will not bug out! Added some new methods --- parseManager/Program.cs | 9 +- parseManager/bin/Debug/test.dat | 3 - parseManager/bin/Debug/test.txt | 61 +++++++++ parseManager/bin/Release/test.txt | 16 ++- parseManager/bin/Release/test3.txt | 20 ++- parseManager/parseManager.cs | 195 ++++++++++++++++++++++++++--- 6 files changed, 274 insertions(+), 30 deletions(-) delete mode 100644 parseManager/bin/Debug/test.dat create mode 100644 parseManager/bin/Debug/test.txt diff --git a/parseManager/Program.cs b/parseManager/Program.cs index ca4221e..e6bd5a9 100644 --- a/parseManager/Program.cs +++ b/parseManager/Program.cs @@ -79,11 +79,8 @@ namespace parseManagerCS Console.ReadLine(); Environment.Exit(0); } - parseManager test = new parseManager(args[0], "define"); // define is where your methods will be held - - //parseManager test = new parseManager("parsetest2.txt","define"); - - nextType next = test.Next(); // TODO implement the next method + parseManager PM = new parseManager(args[0], "define"); + nextType next = PM.Next(); string type; while (next.GetCMDType() != "EOF") { type = next.GetCMDType(); @@ -91,7 +88,7 @@ namespace parseManagerCS Console.Write(next.GetText()); Console.ReadLine(); } - next = test.Next(); + next = PM.Next(); } } } diff --git a/parseManager/bin/Debug/test.dat b/parseManager/bin/Debug/test.dat deleted file mode 100644 index 88c92a7..0000000 --- a/parseManager/bin/Debug/test.dat +++ /dev/null @@ -1,3 +0,0 @@ -[TESTER]{ - "Hello World" -} \ No newline at end of file diff --git a/parseManager/bin/Debug/test.txt b/parseManager/bin/Debug/test.txt new file mode 100644 index 0000000..de2b710 --- /dev/null +++ b/parseManager/bin/Debug/test.txt @@ -0,0 +1,61 @@ +ENTRY TESTSTART +[TESTSTART]{ + SetFG(Color_Blue) + write("This is blue ") + SetFG(Color_Red) + print("This is red") + ResetColor() + "Hello (Press Enter)" + print("PLAY SONG (1)") + print("MESSAGE (2)") + print("An Adventure (3)") + print("QUIT (4)") + ::choice:: + write("Choose: ") + choice=GetInput() + if choice=="1" then JUMP("SONG")|SKIP(0) + if choice=="2" then JUMP("YO")|SKIP(0) + if choice=="4" then QUIT()|SKIP(0) + if choice=="3" then SKIP(2)|SKIP(0) + GOTO("choice") + "We are here now! Time for some fun..." + write("Please enter your name: ") + name=GetInput() + setCC() + if name=="" then SKIP(-4)|SKIP(0) + ClearLine() + print("So your name is $name$ huh...") + "I won't judge haha" + "Anyway let's get controls for that song" + print("Stop (s)") + print("Play (t)") + print("Pause (a)") + print("Resume (r)") + print("Quit (q)") + ::control:: + write("Choose: ") + choice=GetInput() + if choice=="s" then STOP(song)|SKIP(0) + if choice=="t" then JUMP("PLAYS")|SKIP(0) + if choice=="a" then PAUSE(song)|SKIP(0) + if choice=="r" then RESUME(song)|SKIP(0) + if choice=="q" then QUIT()|SKIP(0) + GOTO("control") +} +[ClearLine:function()]{ + whiteOut() + setCC() +} +[PLAYS]{ + PAUSE(song) + song=PLAY("test.flac") + GOTO("control") +} +[SONG]{ + song=PLAY("test.flac") + GOTO("choice") +} +[YO]{ + "How are you doing?" + GOTO("choice") +} diff --git a/parseManager/bin/Release/test.txt b/parseManager/bin/Release/test.txt index 8517ada..560f27a 100644 --- a/parseManager/bin/Release/test.txt +++ b/parseManager/bin/Release/test.txt @@ -1,5 +1,10 @@ ENTRY TESTSTART [TESTSTART]{ + SetFG(Color_Blue) + write("This is blue ") + SetFG(Color_Red) + print("This is red") + ResetColor() "Hello (Press Enter)" print("PLAY SONG (1)") print("MESSAGE (2)") @@ -11,12 +16,15 @@ ENTRY TESTSTART if choice=="1" then JUMP("SONG")|SKIP(0) if choice=="2" then JUMP("YO")|SKIP(0) if choice=="4" then QUIT()|SKIP(0) - if choice=="3" then SKIP(2)|SKIP(0) + if choice=="3" then SKIP(3)|SKIP(0) + setCC() GOTO("choice") "We are here now! Time for some fun..." write("Please enter your name: ") name=GetInput() - if name=="" then SKIP(-3)|SKIP(0) + setCC() + if name=="" then SKIP(-4)|SKIP(0) + ClearLine() print("So your name is $name$ huh...") "I won't judge haha" "Anyway let's get controls for that song" @@ -35,6 +43,10 @@ ENTRY TESTSTART if choice=="q" then QUIT()|SKIP(0) GOTO("control") } +[ClearLine:function()]{ + whiteOut() + setCC() +} [PLAYS]{ PAUSE(song) song=PLAY("test.flac") diff --git a/parseManager/bin/Release/test3.txt b/parseManager/bin/Release/test3.txt index f221cb6..6331789 100644 --- a/parseManager/bin/Release/test3.txt +++ b/parseManager/bin/Release/test3.txt @@ -1,4 +1,20 @@ +[THREAD]{ + ::loop:: + n=random(1,4) + if n==1 then SetFG(Color_Blue)|SKIP(0) + if n==2 then SetFG(Color_Red)|SKIP(0) + if n==3 then SetFG(Color_Green)|SKIP(0) + if n==4 then SetFG(Color_Yellow)|SKIP(0) + sleep(.1) + GOTO("loop") +} [START]{ - print("Testing") - "Done!" + print("Testing threading!") + n==0 + newThread("THREAD") + c=0 + ::loop:: + c=c+1 + "c = $c$ n = $n$" + GOTO("loop") } \ No newline at end of file diff --git a/parseManager/parseManager.cs b/parseManager/parseManager.cs index ccbd670..95b0c5c 100644 --- a/parseManager/parseManager.cs +++ b/parseManager/parseManager.cs @@ -20,8 +20,9 @@ namespace parseManagerCS standardDefine _invoke = new standardDefine(); string _filepath; bool _active = true; - string _define; + string _define = "NO_DEFINE"; string _entry = "START"; + bool _isInternal; Type _defineType; standardDefine def = new standardDefine(); MethodInfo _defineMethod; @@ -34,6 +35,25 @@ namespace parseManagerCS Dictionary _flags = new Dictionary(); Dictionary _chunks = new Dictionary(); Dictionary _methods = new Dictionary(); + void INITENV() + { + _mainENV["Color_Black"] = ConsoleColor.Black; + _mainENV["Color_Blue"] = ConsoleColor.Blue; + _mainENV["Color_Cyan"] = ConsoleColor.Cyan; + _mainENV["Color_DarkBlue"] = ConsoleColor.DarkBlue; + _mainENV["Color_DarkCyan"] = ConsoleColor.DarkCyan; + _mainENV["Color_DarkGray"] = ConsoleColor.DarkGray; + _mainENV["Color_DarkGreen"] = ConsoleColor.DarkGreen; + _mainENV["Color_DarkMagenta"] = ConsoleColor.DarkMagenta; + _mainENV["Color_DarkRed"] = ConsoleColor.DarkRed; + _mainENV["Color_DarkYellow"] = ConsoleColor.DarkYellow; + _mainENV["Color_Gray"] = ConsoleColor.Gray; + _mainENV["Color_Green"] = ConsoleColor.Green; + _mainENV["Color_Magenta"] = ConsoleColor.Magenta; + _mainENV["Color_Red"] = ConsoleColor.Red; + _mainENV["Color_White"] = ConsoleColor.White; + _mainENV["Color_Yellow"] = ConsoleColor.Yellow; + } public void _SetDENV(ENV env) { _mainENV = env; @@ -46,6 +66,7 @@ namespace parseManagerCS ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); _defineClassObject = defineConstructor.Invoke(new object[]{ }); _defualtENV = _mainENV; + INITENV(); Parse(); } public parseManager(string filepath, string define) @@ -57,8 +78,46 @@ namespace parseManagerCS ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); _defineClassObject = defineConstructor.Invoke(new object[]{ }); _defualtENV = _mainENV; + INITENV(); Parse(); } + public parseManager(string code, string define, bool c) + { + InitFlags(); + _define = define; + _filepath = code; + _isInternal = true; + _defineType = Type.GetType(define); + ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); + _defineClassObject = defineConstructor.Invoke(new object[]{ }); + _defualtENV = _mainENV; + INITENV(); + Parse(code, c); + } + public parseManager(string code, bool c) + { + _isInternal = true; + InitFlags(); + _filepath = code; + _defineType = Type.GetType("standardDefine"); + ConstructorInfo defineConstructor = _defineType.GetConstructor(Type.EmptyTypes); + _defineClassObject = defineConstructor.Invoke(new object[]{ }); + _defualtENV = _mainENV; + INITENV(); + Parse(code, c); + } + public bool IsInternal() + { + return _isInternal; + } + public string GetFilepath() + { + return _filepath; + } + public string GetDefine() + { + return _define; + } void InitFlags() { _flags.Add("leaking", false); @@ -125,6 +184,10 @@ namespace parseManagerCS Console.WriteLine("File '" + _filepath + "' does not exist! Loading failled!"); } } + void Parse(string code, bool c) + { + _Parse(code); + } void Parse(string filename) { try { @@ -175,8 +238,10 @@ namespace parseManagerCS var argsN = c.GetArgs(); var fEnv = new ENV(); fEnv.SetParent(_defualtENV); - for (int i = 0; i < argsN.Length; i++) { - fEnv[argsN[i]] = argsV[i]; + if (!(argsN.Length == 1 && argsN[0] == "")) { + for (int i = 0; i < argsN.Length; i++) { + fEnv[argsN[i]] = argsV[i]; + } } var tempEnv = new ENV(); tempEnv[0] = ccN; @@ -187,7 +252,7 @@ namespace parseManagerCS PushError("Stack Overflow!"); } _defualtENV = fEnv; - def.JUMP(this,method); + def.JUMP(this, method); return fEnv; // TODO Handle returns } public object InvokeR(string method, object[] args) @@ -199,8 +264,8 @@ namespace parseManagerCS try { _defineMethod = _defineType.GetMethod(method); return _defineMethod.Invoke(_defineClassObject, tackBArgs(this, args)); - } catch { - PushError("Invalid method: " + method); + } catch (Exception e) { + PushError("Invalid method: " + method + "\n\n" + e); return null; } } @@ -215,18 +280,19 @@ namespace parseManagerCS _defineMethod = _defineType.GetMethod(method); _defineMethod.Invoke(_defineClassObject, tackBArgs(this, args)); return 0; - } catch { - PushError("Invalid method: " + method); + } catch (Exception e) { + PushError("Invalid method: " + method + "\n\n" + e); + return -1; } - return -1; } - public object[] tackBArgs(object o,object[] args){ + public object[] tackBArgs(object o, object[] args) + { var len = args.Length; - var newargs=new object[len+1]; - for(int i = 0;i _THREAD(Block, PM)); + thread.Start(); + } + public void _THREAD(string block, parseManager _PM) + { + var define = _PM.GetDefine(); + var path = _PM.GetFilepath(); + parseManager PM; + if (_PM.IsInternal()) { + if (define == "NO_DEFINE") { + PM = new parseManager(path, true); + } else { + PM = new parseManager(path, define, true); + } + } else { + if (define == "NO_DEFINE") { + PM = new parseManager(path); + } else { + PM = new parseManager(path, define); + } + } + PM._SetDENV(_PM.GetDENV()); + PM.SetENV(_PM.GetENV()); + nextType next = PM.Next(block); + string type; + while (next.GetCMDType() != "EOF") { + type = next.GetCMDType(); + next = PM.Next(); + } + } public void SAVE(parseManager PM) { var env = PM.GetDENV(); @@ -1149,6 +1248,10 @@ public class standardDefine env["__DefualtENV"] = PM.GetENV(); GLOBALS.WriteToBinaryFile("savedata.dat", env); } + public void save(parseManager PM) + { + SAVE(PM); + } public bool LOAD(parseManager PM) { try { @@ -1165,18 +1268,26 @@ public class standardDefine return false; } } + public void load(parseManager PM) + { + LOAD(PM); + } public void TRACEBACK(parseManager PM) { ENV env = PM.Pop(); PM.SetBlock((string)env[0]); var c = PM.GetCurrentChunk(); c.SetPos((int)env[1]); - SetENV(PM,(ENV)env[3]); + SetENV(PM, (ENV)env[3]); } public void EXIT(parseManager PM) { PM.Deactivate(); } + public void exit(parseManager PM) + { + EXIT(PM); + } public void QUIT(parseManager PM) { Environment.Exit(0); @@ -1203,6 +1314,28 @@ public class standardDefine { return Console.ReadLine(); } + public void setCC(parseManager PM) + { + Console.SetCursorPosition(0, Console.CursorTop - 1); + } + public void whiteOut(parseManager PM) + { + for (int i = 0; i < Console.BufferWidth; i++) { + Console.Write(" "); + } + } + public void SetBG(parseManager PM, ConsoleColor BG) + { + Console.BackgroundColor = BG; + } + public void SetFG(parseManager PM, ConsoleColor FG) + { + Console.ForegroundColor = FG; + } + public void ResetColor(parseManager PM) + { + Console.ResetColor(); + } public int GOTO(parseManager PM, string label) { var c = PM.GetCurrentChunk(); @@ -1234,18 +1367,30 @@ public class standardDefine } return 0; } + public double len(parseManager PM, object o) + { + return LEN(PM, o); + } public void JUMP(parseManager PM, string block) { var c = PM.GetCurrentChunk(); c.ResetPos(); PM.SetBlock(block); } + public void jump(parseManager PM, string block) + { + JUMP(PM, block); + } public void SKIP(parseManager PM, double n) { var c = PM.GetCurrentChunk(); var pos = c.GetPos(); c.SetPos(pos + (int)n); } + public void skip(parseManager PM, double n) + { + SKIP(PM, n); + } public double tonumber(parseManager PM, string strn) { double d; @@ -1255,9 +1400,9 @@ public class standardDefine PM.debug("Cannot convert to a number!"); return double.NaN; } - public void SLEEP(parseManager PM, double n) + public void sleep(parseManager PM, double n) { - int i = int.Parse(n.ToString()) * 1000; + int i = (int)n * 1000; Thread.Sleep(i); } public double ADD(parseManager PM, double a, double b) @@ -1284,10 +1429,26 @@ public class standardDefine { return evaluater.Evaluate(ex); } + public void pause(parseManager PM) + { + Console.ReadLine(); + } public void print(parseManager PM, object o) { Console.WriteLine(o); } + public double random(parseManager PM, double s, double e) + { + return (double)rnd.Next((int)s, (int)e); + } + public double rand(parseManager PM) + { + return rnd.NextDouble(); + } + public double round(parseManager PM, double num, double n) + { + return Math.Round(num, (int)n); + } public void write(parseManager PM, object o) { Console.Write(o);