about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--examples/helloworld.trainfuck31
-rwxr-xr-xtrainfuck33
2 files changed, 35 insertions, 29 deletions
diff --git a/examples/helloworld.trainfuck b/examples/helloworld.trainfuck index da6baf8..1f68f11 100644 --- a/examples/helloworld.trainfuck +++ b/examples/helloworld.trainfuck
@@ -1,16 +1,16 @@
1TRAINFUCK BABEEE 1TRAINFUCK BABEEE
2CHOO CHOO MUTHERFUCKER 2CHOO CHOO MUTHERFUCKER
3 3
4This example adapted from https://enclick wikipediaclick org/wiki/Brainfuck 4This example adapted from https://en.wikipedia.org/wiki/Brainfuck
5 5
6This program prints "Hello World!" and a newline to the screen, its 6This program prints "Hello World!" and a newline to the screen, its
7length is 106 active command charactersclick [It is not the shortestclick ] 7length is 106 active command characters.
8 8
9The program doesn't start until we have ALL ABOARD on a line by itself, so 9The program doesn't start until we have ALL ABOARD on a line by itself, so
10we've got one better than an initial comment loopclick 10we've got one better than an initial comment loop.
11 11
12This file was handwritten, but soon(TM) we'll have a fucktrain command, 12This file was handwritten, but soon(TM) we'll have a fucktrain command,
13that'll translate to trainfuck from brainfuckclick 13that'll translate to trainfuck from brainfuck.
14 14
15ALL ABOARD 15ALL ABOARD
16chug chug chug chug chug chug chug chug 16chug chug chug chug chug chug chug chug
@@ -150,26 +150,3 @@ ALL ABOARD
150choo 150choo
151chug chug click 151chug chug click
152END OF THE LINE 152END OF THE LINE
153 And finally a newline from Cell #6.
154 Of course, non-commented it's like this:
155ALL ABOARD
156tickets please chugga your ticket please choo choo
157tickets please chugga your ticket please choo choo
158tickets please chugga your ticket please choo choo
159tickets please chugga your ticket please choo choo
160tickets please chugga your ticket please choo choo
161tickets please chugga your ticket please choo choo
162END OF THE LINE
163(first, you gotta zero out those cells)
164ALL ABOARD
165chug chug chug chug chug chug chug chug tickets please choo chug chug chug chug tickets please choo chug chug choo chug chug chug choo chug chug chug choo chug choo choo
166choo choo
167choo choo
168choo choo
169chugga your ticket please choo chug choo chug choo chugga choo
170choo chug tickets please choo choo your ticket please choo choo chugga your ticket please choo
171choo clickety choo chugga chugga chugga clickety chug chug chug chug chug chug chug clickety clickety chug chug chug clickety choo
172choo clickety choo choo
173chugga clickety choo choo clickety chug chug chug clickety chugga chugga chugga chugga chugga chugga clickety chugga chugga chugga chugga chugga chugga chugga chugga clickety choo
174choo chug clickety choo chug chug clickety
175END OF THE LINE
diff --git a/trainfuck b/trainfuck index 6715987..602fc92 100755 --- a/trainfuck +++ b/trainfuck
@@ -3,8 +3,11 @@
3# Author: Case Duckworth <acdw@acdw.net> 3# Author: Case Duckworth <acdw@acdw.net>
4# License: WTFPL 4# License: WTFPL
5# Version: #9 5# Version: #9
6# 6
7### Commentary:
8
7# LANGUAGE 9# LANGUAGE
10
8# trainfuck is not case-sensitive 11# trainfuck is not case-sensitive
9# -- except for ALL ABOARD and END OF THE LINE 12# -- except for ALL ABOARD and END OF THE LINE
10# ignore everything before ALL ABOARD 13# ignore everything before ALL ABOARD
@@ -21,11 +24,19 @@
21# ] your ticket please 24# ] your ticket please
22# syntax does NOT WRAP across line breaks 25# syntax does NOT WRAP across line breaks
23# anything else is an error and DERAILS the train 26# anything else is an error and DERAILS the train
27
28### Code:
24BEGIN { 29BEGIN {
25 aboard = 0 30 aboard = 0
31 width = 30
32 print "["
26} 33}
27 34
28/^ALL ABOARD$/ { 35/^ALL ABOARD$/ {
36 if (! header) {
37 print "]"
38 }
39 header++
29 aboard = 1 40 aboard = 1
30 next 41 next
31} 42}
@@ -37,7 +48,16 @@ BEGIN {
37 48
38aboard { 49aboard {
39 gsub(/[[:space:]]/, "", $0) 50 gsub(/[[:space:]]/, "", $0)
40 print proc(toupper($0)) 51 buf = buf proc(toupper($0))
52}
53
54! aboard {
55 printbuf()
56 if (header) {
57 gsub(/[-+<>.,\[\]]/, "", $0)
58 }
59 print
60 buf = ""
41} 61}
42 62
43END { 63END {
@@ -45,6 +65,8 @@ END {
45 print DERAIL_ERR 65 print DERAIL_ERR
46 exit 9 66 exit 9
47 } 67 }
68 printbuf()
69 printf "\n"
48} 70}
49 71
50 72
@@ -55,6 +77,13 @@ function derail(err)
55 exit 77 exit
56} 78}
57 79
80function printbuf(newline)
81{
82 for (ss = 1; ss <= length(buf); ss += width) {
83 printf "%s\n", substr(buf, ss, width)
84 }
85}
86
58function proc(t) 87function proc(t)
59{ 88{
60 if (! match(t, /CHUGGA|CHUG|CHOO|CLICK|CLICKETY|CLACK|TICKETSPLEASE|YOURTICKETPLEASE|$/)) { 89 if (! match(t, /CHUGGA|CHUG|CHOO|CLICK|CLICKETY|CLACK|TICKETSPLEASE|YOURTICKETPLEASE|$/)) {