Advertisement
quartata

Untitled

Oct 27th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. using Microsoft.CSharp;
  2. using System;
  3. using System.CodeDom.Compiler;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Text;
  8.  
  9. public class Rotor {
  10. static string[][][] wheels = {new[]{new[] {"p","Console.WriteLine(Rotor.pop().ToString());"},new[] {"]","int i=Rotor.stack.Length;object[] array = new object[i];for(;i>0;i++)array[i]=Rotor.pop();push(array);"}}};
  11. static object[] stack = new object[300];
  12. static string code;
  13. static StringBuilder literal = new StringBuilder();
  14. static RotorComparer comp = new RotorComparer();
  15. static int codePointer, stackPointer, wheelPointer;
  16.  
  17. public static void Main() {
  18. code = File.ReadAllText(Environment.GetCommandLineArgs()[1]);
  19. parse();
  20. }
  21.  
  22. private static unsafe void parse() {
  23. for(;codePointer < code.Length;codePointer++) {
  24. char instruction = code[codePointer];
  25. if(instruction == '<') {
  26. if(wheelPointer == 0) wheelPointer = wheels.Length-1;
  27. else wheelPointer--;
  28. } else if(instruction == '>') {
  29. if(wheelPointer == wheels.Length-1) wheelPointer = 0;
  30. else wheelPointer++;
  31. } else if(instruction == '"') {
  32. for(codePointer++;codePointer < code.Length;codePointer++) {
  33. char stringChar = code[codePointer];
  34. if(stringChar == '\\' && codePointer != code.Length-1) {
  35. literal.Append(stringChar.ToString()+code[++codePointer].ToString());
  36. } else if(stringChar == '"') {
  37. push(literal.ToString());
  38. break;
  39. } else {
  40. literal.Append(stringChar);
  41. }
  42. }
  43. } else if(instruction == '\'') {
  44. push(code[++codePointer]);
  45. } else if(instruction >= '0' && instruction <= '9') {
  46. push(Int32.Parse(code[codePointer].ToString()));
  47. } else {
  48. string[][] wheel = wheels[wheelPointer];
  49. int index = Array.BinarySearch(wheel, new[] { instruction.ToString() }, comp);
  50. if (index == -1) continue;
  51. eval(wheel[index][1]);
  52. }
  53. }
  54. }
  55.  
  56. public static unsafe void push(object o) {
  57. stack[++stackPointer] = o;
  58. }
  59.  
  60. public static unsafe object pop() {
  61. object p = stack[stackPointer];
  62. stack[stackPointer--] = null;
  63. Console.WriteLine("t");
  64. return p;
  65. }
  66.  
  67. private static object eval(string expression) {
  68. CSharpCodeProvider c = new CSharpCodeProvider();
  69. CompilerParameters cp = new CompilerParameters();
  70. cp.ReferencedAssemblies.Add("system.dll");
  71. cp.CompilerOptions = "/t:library";
  72. cp.GenerateInMemory = true;
  73. StringBuilder sb = new StringBuilder("");
  74. sb.Append("using System;\n");
  75. sb.Append("public class CSCodeEvaler{ \n");
  76. sb.Append("public void EvalCode(){\n");
  77. sb.Append(expression);
  78. sb.Append("} \n");
  79. sb.Append("} \n");
  80. CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString());
  81. if (cr.Errors.Count > 0) {
  82. foreach(CompilerError err in cr.Errors) {Console.WriteLine(err.ErrorText);}
  83. }
  84. Assembly a = cr.CompiledAssembly;
  85. object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
  86. Type t = o.GetType();
  87. MethodInfo mi = t.GetMethod("EvalCode");
  88. object s = mi.Invoke(o, null);
  89. return s;
  90. }
  91. }
  92.  
  93. class RotorComparer : IComparer<string[]> {
  94. public int Compare(string[] x, string[] y) {
  95. return Comparer<Char>.Default.Compare(x[0][0],y[0][0]);
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement