Skip to content

Instantly share code, notes, and snippets.

@Theoistic
Created February 6, 2021 19:08
Show Gist options
  • Save Theoistic/cc785992a9bbf7793e91b13e80c40d69 to your computer and use it in GitHub Desktop.
Save Theoistic/cc785992a9bbf7793e91b13e80c40d69 to your computer and use it in GitHub Desktop.
The QuBit interpreter ..
using System;
using System.IO;
using System.Linq;
namespace qubit
{
public class QuBitInterpreter {
public static byte[] stack;
public static int pointer;
public static void Run(string code) {
code = File.ReadAllText(code).Replace("\r", "").Replace("\n", "").Replace("\t", "");
int unmatchedBracketCounter = 0;
string[] blocks = Enumerable.Range(0, code.Length / 4).Select(i => code.Substring(i * 4, 4)).ToArray();
for(int i = 0; i < blocks.Length; i++) {
switch(blocks[i]) {
case "0000": // >
pointer++;
break;
case "1111": // <
pointer--;
break;
case "0110": // +
stack[pointer]++;
break;
case "1001": // -
stack[pointer]--;
break;
case "1100": // .
Console.Write(System.Convert.ToChar(stack[pointer]));
break;
case "0011": // ,
var key = Console.ReadKey();
stack[pointer] = (byte)key.KeyChar;
break;
case "1010": // [
if (stack[pointer] == 0) {
unmatchedBracketCounter++;
while (blocks[i] != "0101" || unmatchedBracketCounter != 0) {
i++;
if (blocks[i] == "1010") {
unmatchedBracketCounter++;
} else if (blocks[i] == "0101") {
unmatchedBracketCounter--;
}
}
}
break;
case "0101": // ]
if (stack[pointer] != 0) {
unmatchedBracketCounter++;
while (blocks[i] != "1010" || unmatchedBracketCounter != 0) {
i--;
if (blocks[i] == "0101") {
unmatchedBracketCounter++;
} else if (blocks[i] == "1010") {
unmatchedBracketCounter--;
}
}
}
break;
}
}
}
public static void Convert(string bf)
{
string code = File.ReadAllText(bf);
File.WriteAllText(Path.GetFileNameWithoutExtension(bf) + ".qb",
code.Replace(" ", "")
.Replace(">", "0000")
.Replace("<", "1111")
.Replace("+", "0110")
.Replace("-", "1001")
.Replace(".", "1100")
.Replace(",", "0011")
.Replace("[", "1010")
.Replace("]", "0101")
);
}
public static void Main(string[] args) {
stack = new byte[1000];
if(File.Exists(args[0]) && Path.GetExtension(args[0]).ToLower() == ".qb") {
Run(args[0]);
} else if(File.Exists(args[0])) {
Convert(args[0]);
} else {
Console.WriteLine($"{args} is not a file that i can find..");
}
}
}
}
000001100110011001100110011001100110011001101010111101100110011001100110011001100110011001100000100101011111100100000000000000000000011001100110101000000110011001100000011001100110111111111001010111111111111111110110111110100000101000000110
000001101111111110010101000000001010100111111111011000000000010101100110011001100000011011111010100111111001000001011111101010101001010100000000100111111111010100000000101010101001010111111111011000000000010111111111101010101001010100000000
000000000000000010101010100101011111011001100110011001100110011001100110011011111001000000000101111110011010000001100000011011111111100101010000101011110110000010010101011000001010101010010101111110010000010111111111111111111111111111111111
111110010000000001011111101000000110000001101111111110010101000000001010100111111111011000000000010101100000011011111010100111111001000001011111101010101001010100000000100111111111010100000000101010101001010111111111011000000000010111111111
111110100000000001100000011011111111111110010101000000000000101010011111111111110110000000000000010101100110000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111
111110100000011011111010100101010101111110100000000001101111111110101001010101010000000010101111111101100000000010101001010101011111111111111010000000000110000001101111111111111001010100000000000010101001111111111111011000000000000001010110
011001100110000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111110100000011011111010100101010101111110100000000001101111111110101001010101010000000010101111
111101100000000010101001010101011111111110101010100101010000000000000110011001100110011001100110011010100000000001100110011001100110011011111111100101010000101011110110011001100110011001100110011010100000011001100110011001100110111110010101
000011001111011001100110011001100110011001101010000010011001100110011001100111111001010100001010111111110110000000001001010101010000110011111111011001100110011001100110011001101010000000001001100110011001100110011111111110010101111110101001
000000000110111111110101111101100110011001100110011001100110101011110110011001100110000010010101111111000000011001100110011001100110011010100000011001100110011001100110011001100110111110010101000001100110011011001111011001100110011001101010
000001100110011001100110011001100110011011111001010100001100011001100110011001101100110010011001100110011001100110011001110010011001100110011001100110011100011001100110011001100110011001100110011001100110011001100000000010100000000000000110
000001101111111111111111100101010000000000000000101010011111111111111111011000000000000000000101000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111111111111
101000000000000001100000011011111111111111111001010100000000000000001010100111111111111111110110000000000000000001010110000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000
000001011111111111111010000000000110111111111010100101010101000010100000011011111010100101010101011001100000000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111
011011111010101010010101000010011111010100001010111111111111111111111111111111000000000000000000000000000000101010010101010111111111111111111111111111111111111111000000000010011001100110011100100110011001100110011001100110011001110011111111
110000000000100110011001100111000110011001101100110001100110011001100110011001100110011001100110011001101100101010010101111111111010100101010101111110100000011000000110111111111001010100000000101010011111111101100000000001010110000001101111
101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111111111010000000000110000001101111111111111001010100000000000010101001111111111111011000000000000001010110011001100110
000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111110100000011011111010100101010101111110100000000001101111111110101001010101010000000010101111111101100000
000010101001010101011111111110101010100101010000011001100110011001100110011001101010111101100110011001100000100101011111110000000110011001100110011001100110011001100110101000000110011001100110011001100110011001100110011011111001010100000110
110010011100111111111100000000000110011001100110011001101100100110011001100110011001100110011001100110011001110010011001100111001111111111000000011001100110011001100110101000000110011001101111100101010000110011110110011001100110011001101010
000010011001100110011111100101010000011001101100011001100110011001100110011001100110011001101100110010101001010111111111101010010101011001100110011001100110011001100110011011001010100101010101111110100000011000000110111111111001010100000000
101010011111111101100000000001010110011001100000011011111010100111111001000001011111101010101001010100000000100111111111010100000000101010101001010111111111011000000000010111111111101010101001010101100110011001100110011001100110011001101100
000001100110011001100110011001100110011010100000011001100110011001100110011001100110111110010101000001100110011011000110011001100110011001100110011001100110011001100110110001100110011001100110011001100110011001101100100110011001100110011001
110011110110011001100110011001100110011010100000000001100110011001101111111110010101000000001100111101100110011001100110011001100110011001101100100111001001100110011001100110011001100110011100000011001111100111000110011001100110011001100110
011001100110011011000110011001100110011001100110011011001001100110011001100110011001100110011100000011001111100110011001100110011001100110011001100110011001100111000110011001100110011001100110011001100110011001100110110010011001100110011001
100110011001100110011100000011001111011001100110011001100110011001100110011001100110110010011001100110011001100110011001100110011001100110011001100111001111011001100110101000000110011001100110011001101111100101010000110011000000110011111001
100110011001100110011001100110011001110001100110011001100110011001100110011001100110110000001100111111110110011001101010000010011001100110011001100111111001010100001001110001100110011001100110011001100110011001100110011001100110011001100110
110010011001100111000110011001100110011001101100100110011001100110011001100111001001100110011001100110011001100110011001110010101001010100001010100101011111111111111100101010010101010111111010000001100000011011111111100101010000000010101001
111111110110000000000101011001100110011000000110111110101001111110010000010111111010101010010101000000001001111111110101000000001010101010010101111111110110000000000101111111111010101010010101011001100110011001100110011001100110011011001010
100101011111101010010101000001011111011011110101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment