/// <summary> /// Main Lexxer Function /// </summary> /// <param name="Code">Code from user.</param> /// <returns>Inner_EasyC Output</returns> public SudoC_Pair[] Lex(string Code) { foreach (char Char in Code) { iCounter++; sBuilder += Char; if (sBuilder.EndsWith(";") && iTaskIndex == 0) { string[] Arguments = sBuilder.Split('='); Arguments[0] = Arguments[0].TrimStart(new char[] { '\n', ')', ';', '}', ' ', }); Arguments[0] = Arguments[0].TrimEnd(new char[] { '\r', ' ' }); Arguments[1] = Arguments[1].TrimEnd(new char[] { '\r', ' ', ';' }); Lexxed_Code.Add(new SudoC_Pair(SudoCInnerCode.equalize, Arguments)); sBuilder = ""; } if ((sBuilder.Contains("</") && iTaskIndex == 0) || iTaskIndex == 1) { iTaskIndex = 1; if (Char == '\\' && Code[iCounter] == '>') { bInScope = false; sConstructor = sConstructor.TrimStart(new char[] { ')', ';', '}' }); Lexxed_Code.Add(new SudoC_Pair(SudoCInnerCode.copy, new string[1] { sConstructor })); sConstructor = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (bInScope) { sConstructor += Char; } if (Char == '/' && Code[iCounter - 2] == '<') { bInScope = true; } goto cycleEnd; } if ((sBuilder.Contains("print") && iTaskIndex == 0) || iTaskIndex == 2) { iTaskIndex = 2; if (Char == ')') { bInScope = false; PrintModule printModule = new PrintModule(); foreach (SudoC_Pair SudoC_Pair in printModule.pPrintBracksLexxer(sConstructor)) { Lexxed_Code.Add(SudoC_Pair); } sConstructor = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (bInScope) { sConstructor += Char; } if (Char == '(') { bInScope = true; } } if ((sBuilder.Contains("var") && iTaskIndex == 0) || iTaskIndex == 3) { iTaskIndex = 3; if (Char == ';') { sCompleteName = sCompleteName.TrimStart(new char[] { ')', ';', '}' }); sCompleteName = sCompleteName.TrimStart('r'); sCompleteName = sCompleteName.TrimEnd('='); VarModule varModule = new VarModule(); foreach (SudoC_Pair sudoC_Pair in varModule.pStringProccessor_Var(new string[] { sConstructor, sCompleteName }, SudoCInnerCode.vstring)) { Lexxed_Code.Add(sudoC_Pair); } bCaptureValue = false; bCaptureSpaces = false; sConstructor = string.Empty; sCompleteName = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (Char != ' ' || bCaptureSpaces) { sConstructor += Char; } if (bCaptureValue) { sCompleteName = sConstructor; sConstructor = string.Empty; bCaptureSpaces = true; bCaptureValue = false; } if (Char == '=') { bCaptureValue = true; } goto cycleEnd; } if ((sBuilder.Contains("def") && iTaskIndex == 0) || iTaskIndex == 4) { iTaskIndex = 4; if (Char == ';') { sCompleteName = sCompleteName.TrimStart(new char[] { ')', ';', '}' }); sCompleteName = sCompleteName.TrimStart('f'); sCompleteName = sCompleteName.TrimEnd('='); // sConstructor.Replace(@"\;",";"); Lexxed_Code.Add(new SudoC_Pair(SudoCInnerCode.def, new string[2] { sConstructor, sCompleteName })); bCaptureValue = false; bCaptureSpaces = false; sConstructor = string.Empty; sCompleteName = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (Char != ' ' || bCaptureSpaces) { sConstructor += Char; } if (bCaptureValue) { sCompleteName = sConstructor; sConstructor = string.Empty; bCaptureSpaces = true; bCaptureValue = false; } if (Char == '=') { bCaptureValue = true; } goto cycleEnd; } if ((sBuilder.Contains("context") && iTaskIndex == 0) || iTaskIndex == 5) { iTaskIndex = 5; if (Char == '{' && iBracketsLevel >= 1) { iBracketsLevel++; } if (Char == '}' && iBracketsLevel == 1) { sCompleteName = sCompleteName.TrimStart(new char[] { ')', ';', '}' }); sCompleteName = sCompleteName.TrimStart('t'); sCompleteName = sCompleteName.TrimEnd('\r', '\n', '{'); // sConstructor.Replace(@"\;", ";"); var sudoc_lexxerSecondary = new SudoC_Lexxer(); var soduc_assemblerSecondary = new SudoC_Assembler(); sConstructor = soduc_assemblerSecondary.Assemble(sudoc_lexxerSecondary.Lex(sConstructor), true); Statics.dContexts.Add(sCompleteName, sConstructor); Lexxed_Code.Add(new SudoC_Pair(SudoCInnerCode.context, new string[2] { sConstructor, sCompleteName })); bCaptureValue = false; bCaptureSpaces = false; sConstructor = string.Empty; sCompleteName = string.Empty; sBuilder = string.Empty; iBracketsLevel = 0; iTaskIndex = 0; } if (Char != ' ' || bCaptureSpaces) { sConstructor += Char; } if (bCaptureValue) { sCompleteName = sConstructor; sConstructor = string.Empty; bCaptureSpaces = true; bCaptureValue = false; } if (Char == '}' && iBracketsLevel > 1) { iBracketsLevel--; } if (Char == '{' && iBracketsLevel == 0) { bCaptureValue = true; iBracketsLevel++; } goto cycleEnd; } if ((sBuilder.Contains("fetch") && iTaskIndex == 0) || iTaskIndex == 6) { iTaskIndex = 6; if (Char == ')') { sConstructor = sConstructor.TrimStart(new char[] { ')', ';', '}' }); bInScope = false; Lexxed_Code.Add(new SudoC_Pair(SudoCInnerCode.fetch, new string[1] { sConstructor })); sConstructor = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (bInScope) { sConstructor += Char; } if (Char == '(') { bInScope = true; } } if ((sBuilder.Contains("include") && iTaskIndex == 0) || iTaskIndex == 7) { iTaskIndex = 7; if (Char == ')') { sConstructor = sConstructor.TrimStart(new char[] { ')', ';', '}' }); bInScope = false; var sudoc_lexxerSecondary = new SudoC_Lexxer(); var soduc_assemblerSecondary = new SudoC_Assembler(); soduc_assemblerSecondary.Assemble(sudoc_lexxerSecondary.Lex(File.ReadAllText(sConstructor)), true); sConstructor = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (bInScope) { sConstructor += Char; } if (Char == '(') { bInScope = true; } } if ((sBuilder.Contains(@"//") && iTaskIndex == 0) || iTaskIndex == 8) { iTaskIndex = 8; if (Char == '\n') { sBuilder = string.Empty; iTaskIndex = 0; } goto cycleEnd; } if ((sBuilder.Contains("repeat") && iTaskIndex == 0) || iTaskIndex == 9) { iTaskIndex = 9; if (Char == ')' && !bFinishedCapture) { sConstructor = sConstructor.TrimStart(new char[] { ')', ';', '}' }); sCompleteName = sCompleteName.TrimStart('t'); bInScope = false; bFinishedCapture = true; sRepeater = sConstructor; sConstructor = string.Empty; } if (bInScope && !bFinishedCapture) { sConstructor += Char; } if (Char == '(' && !bFinishedCapture) { bInScope = true; } if (bFinishedCapture) { if (Char == '{' && iBracketsLevel >= 1) { iBracketsLevel++; } if (Char == '}' && iBracketsLevel == 1) { sCompleteName = sCompleteName.TrimStart(new char[] { ')', ';', '}' }); sCompleteName = sCompleteName.TrimStart(sRepeater[sRepeater.Length - 1]); sCompleteName = sCompleteName.TrimEnd('\r', '\n', '{'); LoopModule loopModule = new LoopModule(); foreach (SudoC_Pair sudoC_Pair in loopModule.Loop(sCompleteName, sRepeater, sConstructor)) { Lexxed_Code.Add(sudoC_Pair); } bFinishedCapture = false; bCaptureValue = false; bCaptureSpaces = false; sRepeater = string.Empty; sConstructor = string.Empty; sCompleteName = string.Empty; sBuilder = string.Empty; iBracketsLevel = 0; iTaskIndex = 0; } if ((Char != ' ' || bCaptureSpaces)) { sConstructor += Char; } if (bCaptureValue) { sCompleteName = sConstructor; sConstructor = string.Empty; bCaptureSpaces = true; bCaptureValue = false; } if (Char == '}' && iBracketsLevel > 1) { iBracketsLevel--; } if (Char == '{' && iBracketsLevel == 0) { bCaptureValue = true; iBracketsLevel++; } } goto cycleEnd; } if ((sBuilder.Contains("use") && iTaskIndex == 0) || iTaskIndex == 10) { iTaskIndex = 10; if (Char == ')') { bInScope = false; Statics.AddImports(sConstructor); sConstructor = string.Empty; sBuilder = string.Empty; iTaskIndex = 0; } if (bInScope) { sConstructor += Char; } if (Char == '(') { bInScope = true; } } cycleEnd :; } return(Lexxed_Code.ToArray()); }
/// <summary> /// Converts InnerEzC to C /// </summary> /// <param name="Lexxed_Code"></param> /// <returns>C Code</returns> public string Assemble(SudoC_Pair[] Lexxed_Code, bool disableImports) { foreach (SudoC_Pair Pair in Lexxed_Code) { switch (Pair.InnerCode) { case SudoCInnerCode.copy: Assembled += Pair.Args[0]; break; ///Print Function case SudoCInnerCode.print: if (Pair.Args[0].StartsWith("\"") && Pair.Args[0].EndsWith("\"")) { Statics.AddImports("<stdio.h>"); Assembled += "printf(" + Pair.Args[0] + ");"; } // printf("%s", variable); else { Assembled += "printf(\"%s\", " + Pair.Args[0] + ");"; } Assembled += Environment.NewLine; break; case SudoCInnerCode.vstring: if (Pair.Args[1] != string.Empty) { Assembled += "char " + Pair.Args[0] + "[2056] = \"" + Pair.Args[1] + "\";"; } else { Assembled += "char " + Pair.Args[0] + "[2056];"; } Assembled += Environment.NewLine; break; case SudoCInnerCode.scan: //scanf("%s", myString); Statics.AddImports("<stdio.h>"); Assembled += "scanf(\"%s\"," + Pair.Args[0] + ");"; Assembled += Environment.NewLine; break; case SudoCInnerCode.equalize: Statics.AddImports("<string.h>"); Assembled += "strcpy(" + Pair.Args[0] + ", " + Pair.Args[1] + ");"; Assembled += Environment.NewLine; break; case SudoCInnerCode.def: Statics.dDefines.Add(Pair.Args[1], Pair.Args[0]); break; case SudoCInnerCode.context: if (Pair.Args[1] == "program") { Assembled += "\nint main()\n{\n"; Assembled += Pair.Args[0]; Assembled += "\nreturn 0;\n}\n"; } break; case SudoCInnerCode.fetch: Assembled += Statics.dContexts[Pair.Args[0]]; break; case SudoCInnerCode.foreachloop: if (!Statics.lVars.Contains(Pair.Args[0])) { if (Pair.Args[0] == "infinite") { Assembled += "int _" + Pair.Args[1] + " = 0;\nwhile(1)\n{\nchar " + Pair.Args[1] + "[2056];\n _" + Pair.Args[1] + "++;\n sprintf(" + Pair.Args[1] + ", \"%d\", _" + Pair.Args[1] + ");\n" + Pair.Args[2] + "\n}\n"; } else { Assembled += "for(int _" + Pair.Args[1] + " = 0;_" + Pair.Args[1] + " < " + Pair.Args[0] + ";_" + Pair.Args[1] + "++)\n{\n char " + Pair.Args[1] + "[2056];\n sprintf(" + Pair.Args[1] + ", \"%d\", _" + Pair.Args[1] + ");\n" + Pair.Args[2] + "\n}\n"; } } else { Statics.AddImports("<stdlib.h>"); Assembled += "for(int _" + Pair.Args[1] + " = 0;_" + Pair.Args[1] + " < atoi(" + Pair.Args[0] + ");_" + Pair.Args[1] + "++)\n{\n char " + Pair.Args[1] + "[2056];\n sprintf(" + Pair.Args[1] + ", \"%d\", _" + Pair.Args[1] + ");\n" + Pair.Args[2] + "\n}\n"; } break; } } if (!disableImports) { foreach (string Import in Statics.lImports) { Assembled = ("#include " + Import + "\n") + Assembled; } } foreach (KeyValuePair <string, string> defs in Statics.dDefines) { Assembled = Assembled.Replace(defs.Key, defs.Value); } return(Assembled); }