about summary refs log tree commit diff stats
path: root/fucktrain.awk
blob: 4ab5f5bcb8806870277d4ba9c04081b643b3aaba (plain)
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
#!/bin/awk -f
# FUCKTRAIN: FUCKAMUTHA CHOO CHOO -*- awk -*-
# Author: Case Duckworth <acdw@acdw.net>
# License: WTFPL
# Version: #9

### Commentary:

# FUCKTRAIN turns brainfuck code into trainfuck code.

### Code:
BEGIN {
	buffer = ""
	fold_width = 70
	width = 0
}

{
	buffer = buffer $0 "\n"
}

END {
	sub(/^[ \t\n\r\f]+/, "", buffer)
	sub(/[ \t\n\r\f]+$/, "", buffer)
	split(buffer, bufa, "")
	boarded = 0
	header = 1
	for (c in bufa) {
		if (bufa[c] == "[" && header) {
			header++
			if (header == 2) {
				c++
			}
		}
		if (bufa[c] == "]" && header) {
			header--
			if (header == 1) {
				header = 0
				c++
			}
		}
		if (! boarded && ! header && bufa[c] ~ /[-+<>.,\[\]]/) {
			printf "\n%s\n", "ALL ABOARD"
			boarded = 1
		}
		if (boarded) {
			if (width > fold_width) {
				print ""
				width = 0
			}
			if (bufa[c] == ">") {
				printf "%s", "choo\n"
			} else if (bufa[c] == "<") {
				printf "%s", "choo choo\n"
			} else if (bufa[c] == "+") {
				printf "%s", "chug "
				width += 5
			} else if (bufa[c] == "-") {
				printf "%s", "chugga "
				width += 7
			} else if (bufa[c] == ".") {
				click = clickety_click()
				printf "%s", click
				width += length(click)
			} else if (bufa[c] == ",") {
				printf "%s", "clack "
				width += 6
			} else if (bufa[c] == "[") {
				printf "%s", "tickets please "
				width += 15
			} else if (bufa[c] == "]") {
				printf "%s", "your ticket please "
				width += 19
			} else if (bufa[c] ~ /[\n]/) {
				printf "%s", bufa[c]
				width = 0
			} else {
				printf "\n%s\n%s", "END OF THE LINE", bufa[c]
				boarded = 0
			}
		} else {
			width = 0
			printf "%s", bufa[c]
		}
	}
	print ""
}


function clickety_click()
{
	if (rand() <= 0.5) {
		return "clickety "
	} else {
		return "click "
	}
}