-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain_cs.hs
97 lines (79 loc) · 2.59 KB
/
brain_cs.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{-# LANGUAGE QuasiQuotes #-}
module Brain_cs where
import Control.Applicative
import Str
before_main = [str|
using System;
class Brain{
static void Main(){
Brain b = new Brain();
b.run();
}
char[] tape;
int position;
Brain(){
tape = new char[1000];
for(int i = 0; i < tape.Length; i++)
tape[i] = (char)0;
position = 0;
}
void incl(){
tape[position]++;
}
void decl(){
tape[position]--;
}
void puts(){
Console.Write(tape[position]);
}
void next_var(){
position++;
if(position >= tape.Length){
char[] new_tape = new char[tape.Length + 1000];
for(int i = 0; i < new_tape.Length; i++){
if(i < tape.Length){
new_tape[i] = tape[i];
}else{
new_tape[i] = (char)0;
}
}
tape = new_tape;
}
}
void prev_var(){
if(position <= 0){
char[] new_tape = new char[tape.Length + 1000];
for(int i = 0; i < new_tape.Length; i++){
if(i < 1000){
new_tape[i] = (char)0;
}else{
new_tape[i] = tape[i - 1000];
}
}
tape = new_tape;
position += 1000;
}
position--;
}
void gets(){
tape[position] = (char)Console.Read();
}
|]
convert_brainfuck :: String -> String
convert_brainfuck "" = ""
convert_brainfuck ('+':rest) = "incl();" ++ convert_brainfuck rest
convert_brainfuck ('-':rest) = "decl();" ++ convert_brainfuck rest
convert_brainfuck ('>':rest) = "next_var();" ++ convert_brainfuck rest
convert_brainfuck ('<':rest) = "prev_var();" ++ convert_brainfuck rest
convert_brainfuck ('.':rest) = "puts();" ++ convert_brainfuck rest
convert_brainfuck (',':rest) = "gets();" ++ convert_brainfuck rest
convert_brainfuck ('[':rest) = "while(tape[position] != 0){" ++ convert_brainfuck rest
convert_brainfuck (']':rest) = "}" ++ convert_brainfuck rest
convert_brainfuck (_:rest) = convert_brainfuck rest
create_run :: String -> String
create_run input = "void run(){" ++ (convert_brainfuck input) ++ "Console.WriteLine();}}"
create_program :: String -> String
create_program input = before_main ++ (create_run input)
brainfuck :: String
brainfuck = ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[<++++>-]<+.[-]++++++++++."
--main = putStrLn $ create_program brainfuck