Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.CSharp;
- using System;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Text;
- public class Rotor {
- 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);"}}};
- static object[] stack = new object[300];
- static string code;
- static StringBuilder literal = new StringBuilder();
- static RotorComparer comp = new RotorComparer();
- static int codePointer, stackPointer, wheelPointer;
- public static void Main() {
- code = File.ReadAllText(Environment.GetCommandLineArgs()[1]);
- parse();
- }
- private static unsafe void parse() {
- for(;codePointer < code.Length;codePointer++) {
- char instruction = code[codePointer];
- if(instruction == '<') {
- if(wheelPointer == 0) wheelPointer = wheels.Length-1;
- else wheelPointer--;
- } else if(instruction == '>') {
- if(wheelPointer == wheels.Length-1) wheelPointer = 0;
- else wheelPointer++;
- } else if(instruction == '"') {
- for(codePointer++;codePointer < code.Length;codePointer++) {
- char stringChar = code[codePointer];
- if(stringChar == '\\' && codePointer != code.Length-1) {
- literal.Append(stringChar.ToString()+code[++codePointer].ToString());
- } else if(stringChar == '"') {
- push(literal.ToString());
- break;
- } else {
- literal.Append(stringChar);
- }
- }
- } else if(instruction == '\'') {
- push(code[++codePointer]);
- } else if(instruction >= '0' && instruction <= '9') {
- push(Int32.Parse(code[codePointer].ToString()));
- } else {
- string[][] wheel = wheels[wheelPointer];
- int index = Array.BinarySearch(wheel, new[] { instruction.ToString() }, comp);
- if (index == -1) continue;
- eval(wheel[index][1]);
- }
- }
- }
- public static unsafe void push(object o) {
- stack[++stackPointer] = o;
- }
- public static unsafe object pop() {
- object p = stack[stackPointer];
- stack[stackPointer--] = null;
- Console.WriteLine("t");
- return p;
- }
- private static object eval(string expression) {
- CSharpCodeProvider c = new CSharpCodeProvider();
- CompilerParameters cp = new CompilerParameters();
- cp.ReferencedAssemblies.Add("system.dll");
- cp.CompilerOptions = "/t:library";
- cp.GenerateInMemory = true;
- StringBuilder sb = new StringBuilder("");
- sb.Append("using System;\n");
- sb.Append("public class CSCodeEvaler{ \n");
- sb.Append("public void EvalCode(){\n");
- sb.Append(expression);
- sb.Append("} \n");
- sb.Append("} \n");
- CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString());
- if (cr.Errors.Count > 0) {
- foreach(CompilerError err in cr.Errors) {Console.WriteLine(err.ErrorText);}
- }
- Assembly a = cr.CompiledAssembly;
- object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
- Type t = o.GetType();
- MethodInfo mi = t.GetMethod("EvalCode");
- object s = mi.Invoke(o, null);
- return s;
- }
- }
- class RotorComparer : IComparer<string[]> {
- public int Compare(string[] x, string[] y) {
- return Comparer<Char>.Default.Compare(x[0][0],y[0][0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement