Minor improvements to Tigersay codebase.
This commit is contained in:
parent
2683daf16c
commit
4aa6987ab5
169
tigersay.tig
169
tigersay.tig
|
@ -6,9 +6,10 @@ let
|
||||||
var false := 0
|
var false := 0
|
||||||
var true := 1
|
var true := 1
|
||||||
|
|
||||||
var INDENT := " "
|
var INDENT := " "
|
||||||
|
var MAXWIDTH := 40
|
||||||
|
|
||||||
|
/* The cow is unused, and taken directly from the cowsay program. */
|
||||||
var COW := "\
|
var COW := "\
|
||||||
\\\ ^__^ \n\
|
\\\ ^__^ \n\
|
||||||
\ \\ (oo)\\_______ \n\
|
\ \\ (oo)\\_______ \n\
|
||||||
|
@ -31,26 +32,56 @@ let
|
||||||
\ ((__.-'((___..-'' \\__.' "
|
\ ((__.-'((___..-'' \\__.' "
|
||||||
|
|
||||||
|
|
||||||
/*************/
|
/***********/
|
||||||
/* Utilility */
|
/* Utility */
|
||||||
|
|
||||||
|
/* I/O */
|
||||||
|
|
||||||
function getinput () : string =
|
function getinput () : string =
|
||||||
let var l := "" /* Full output */
|
let var l := "" /* Full output */
|
||||||
var c := "" /* Current char */
|
var c := "" /* Current char */
|
||||||
in while true do
|
in while true do
|
||||||
( c := getchar()
|
( c := getchar()
|
||||||
; if c = "" then break /* Hit EOF */
|
; if c = "" then break /* Hit EOF */
|
||||||
; l := concat(l, c) )
|
; l := concat(l, c) )
|
||||||
; l
|
; l
|
||||||
end
|
end
|
||||||
|
|
||||||
|
/* Number / Maths */
|
||||||
|
|
||||||
|
function max (a:int, b:int) : int =
|
||||||
|
if a > b then a else b
|
||||||
|
|
||||||
|
function modulo (num : int, mod : int) : int =
|
||||||
|
( while num >= mod do
|
||||||
|
num := num - mod
|
||||||
|
; num )
|
||||||
|
|
||||||
|
function exp (exponee : int, exponent : int) : int =
|
||||||
|
if exponent = 0 then 1
|
||||||
|
else exponee * exp(exponee, exponent - 1)
|
||||||
|
|
||||||
|
function get_digit (num : int, digit : int) : int =
|
||||||
|
modulo(num / exp(10, digit-1), 10)
|
||||||
|
|
||||||
|
function log10 (num : int) : int =
|
||||||
|
let var log10 := 1
|
||||||
|
in while num > 9 do
|
||||||
|
( num := num/10
|
||||||
|
; log10 := log10 + 1 )
|
||||||
|
; log10
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
/* String manipulation */
|
||||||
|
|
||||||
function concat3 (a:string, b:string, c:string) : string =
|
function concat3 (a:string, b:string, c:string) : string =
|
||||||
concat(a, concat(b, c))
|
concat(a, concat(b, c))
|
||||||
|
|
||||||
function concat4 (a:string, b:string, c:string, d:string) : string =
|
function concat4 (a:string, b:string, c:string, d:string) : string =
|
||||||
concat(a, concat(b, concat(c, d)))
|
concat(a, concat(b, concat(c, d)))
|
||||||
|
|
||||||
function indentstring (text : string, before : string, after:string) : string =
|
function indent_string (text : string, before : string, after:string) : string =
|
||||||
let var out := before
|
let var out := before
|
||||||
var char := ""
|
var char := ""
|
||||||
in for i := 0 to size(text) - 1 do
|
in for i := 0 to size(text) - 1 do
|
||||||
|
@ -61,6 +92,52 @@ let
|
||||||
concat(out, after)
|
concat(out, after)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function is_whitespace (char:string) : int =
|
||||||
|
(char = "" | char = "\n" | char = " " | char = "\t")
|
||||||
|
|
||||||
|
function substring_absolute (text:string, i1:int, i2:int) : string =
|
||||||
|
/* Uses absolute coordinates to determine substring */
|
||||||
|
substring(text, i1, i2 - i1 + 1)
|
||||||
|
|
||||||
|
function count_substring_occurances (text:string, char:string) : int =
|
||||||
|
let var count := 0
|
||||||
|
var charwidth := size(char)
|
||||||
|
in for i := 0 to size(text) - charwidth do
|
||||||
|
if substring(text, i, charwidth) = char then
|
||||||
|
count := count + 1
|
||||||
|
; count
|
||||||
|
end
|
||||||
|
|
||||||
|
function repeat_string (str:string, rep:int) : string =
|
||||||
|
let var out := ""
|
||||||
|
in for i := 1 to rep do
|
||||||
|
out := concat(out, str)
|
||||||
|
; out
|
||||||
|
end
|
||||||
|
|
||||||
|
function digit_to_s (n:int) :string =
|
||||||
|
if 0 <= n & n <= 9
|
||||||
|
then chr(48 + n)
|
||||||
|
else "?"
|
||||||
|
|
||||||
|
function int_to_s (num : int) : string =
|
||||||
|
let var l := ""
|
||||||
|
in for i := 1 to log10(num) do
|
||||||
|
l := concat(digit_to_s(get_digit(num, i)), l)
|
||||||
|
; l
|
||||||
|
end
|
||||||
|
|
||||||
|
function length_of_longest_line (text:string) : int =
|
||||||
|
let var longest := 0
|
||||||
|
var current := 0
|
||||||
|
in for i := 0 to size(text) - 1 do
|
||||||
|
if substring(text, i, 1) = "\n"
|
||||||
|
then ( longest := max(longest, current)
|
||||||
|
; current := 0)
|
||||||
|
else current := current + 1
|
||||||
|
; max(longest, current)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
/* Linked Lists */
|
/* Linked Lists */
|
||||||
|
|
||||||
|
@ -84,13 +161,7 @@ let
|
||||||
then 0
|
then 0
|
||||||
else 1 + ll_length(ll.next)
|
else 1 + ll_length(ll.next)
|
||||||
|
|
||||||
var MAXWIDTH := 40
|
/**/
|
||||||
|
|
||||||
function is_whitespace (char:string) : int =
|
|
||||||
(char = "" | char = "\n" | char = " " | char = "\t")
|
|
||||||
|
|
||||||
function substringabs (text:string, i1:int, i2:int) : string =
|
|
||||||
substring(text, i1, i2 - i1 + 1)
|
|
||||||
|
|
||||||
function split_words (text:string) : stringll =
|
function split_words (text:string) : stringll =
|
||||||
let var out := ll_new()
|
let var out := ll_new()
|
||||||
|
@ -99,7 +170,7 @@ let
|
||||||
in for i := 0 to size(text) - 1 do
|
in for i := 0 to size(text) - 1 do
|
||||||
( char := substring(text, i, 1)
|
( char := substring(text, i, 1)
|
||||||
; if is_whitespace(char) & prev_word_start_i <> -1
|
; if is_whitespace(char) & prev_word_start_i <> -1
|
||||||
then ll_append(out, substringabs(text, prev_word_start_i, i - 1))
|
then ll_append(out, substring_absolute(text, prev_word_start_i, i - 1))
|
||||||
else if not(is_whitespace(char)) & prev_word_start_i = -1
|
else if not(is_whitespace(char)) & prev_word_start_i = -1
|
||||||
then prev_word_start_i := i
|
then prev_word_start_i := i
|
||||||
; if is_whitespace(char) then prev_word_start_i := -1
|
; if is_whitespace(char) then prev_word_start_i := -1
|
||||||
|
@ -118,75 +189,37 @@ let
|
||||||
in help(words, 0)
|
in help(words, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
function wrapstring (text:string, linewidth: int) : string =
|
function wrap_string (text:string, linewidth: int) : string =
|
||||||
let var words := split_words(text)
|
let var words := split_words(text)
|
||||||
var splitstr := insert_whitespace_between_words(words, linewidth)
|
var splitstr := insert_whitespace_between_words(words, linewidth)
|
||||||
in
|
in
|
||||||
if ll_length(splitstr) > 2 then
|
ll_to_s( if ll_length(splitstr) > 2
|
||||||
ll_to_s(splitstr.next.next.next)
|
then splitstr.next.next.next
|
||||||
else
|
else splitstr )
|
||||||
ll_to_s(splitstr)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function max (a:int, b:int) : int =
|
|
||||||
if a > b then a else b
|
|
||||||
|
|
||||||
function longest_line_length (text:string) : int =
|
|
||||||
let var longest := 0
|
|
||||||
var current := 0
|
|
||||||
in for i := 0 to size(text) - 1 do
|
|
||||||
if substring(text, i, 1) = "\n"
|
|
||||||
then ( longest := max(longest, current)
|
|
||||||
; current := 0)
|
|
||||||
else current := current + 1
|
|
||||||
; max(longest, current)
|
|
||||||
end
|
|
||||||
|
|
||||||
function nr_char_in_str (text:string, char:string) : int =
|
|
||||||
let var count := 0
|
|
||||||
var charwidth := size(char)
|
|
||||||
in for i := 0 to size(text) - 1 do
|
|
||||||
if substring(text, i, charwidth) = char then count := count + 1
|
|
||||||
; count
|
|
||||||
end
|
|
||||||
|
|
||||||
function rep_string (str:string, rep:int) : string =
|
|
||||||
let var out := ""
|
|
||||||
in for i := 1 to rep do out := concat(out, str)
|
|
||||||
; out
|
|
||||||
end
|
|
||||||
|
|
||||||
function digit_to_s (n:int) :string =
|
|
||||||
if 0 <= n & n <= 9
|
|
||||||
then chr(48 + n)
|
|
||||||
else "?"
|
|
||||||
|
|
||||||
function int_to_s (n:int) : string =
|
|
||||||
concat(digit_to_s(n/10), digit_to_s(n - n/10*10))
|
|
||||||
|
|
||||||
/**************/
|
/**************/
|
||||||
/* Draw stuff */
|
/* Draw stuff */
|
||||||
|
|
||||||
function drawtextbubble (text:string) =
|
function draw_textbubble (text:string) =
|
||||||
let
|
let
|
||||||
var textwidth := longest_line_length(text)
|
var textwidth := length_of_longest_line(text)
|
||||||
var textheight := nr_char_in_str(text, "\n") + 1
|
var textheight := count_substring_occurances(text, "\n") + 1
|
||||||
var bubblewidth := textwidth + 4
|
var bubblewidth := textwidth + 4
|
||||||
var after_text := concat3("\013\027[",int_to_s(bubblewidth-1),"C|")
|
var after_text := concat3("\013\027[",int_to_s(bubblewidth-1),"C|")
|
||||||
in print(concat(" ", concat(rep_string("_", bubblewidth - 2), " \n")))
|
in print(concat3(" ", repeat_string("_", bubblewidth - 2), " \n"))
|
||||||
; print(concat(indentstring(text, "| ", after_text), "\n"))
|
; print(concat(indent_string(text, "| ", after_text), "\n"))
|
||||||
; print(concat(" ", concat(rep_string("-", bubblewidth - 2), " \n")))
|
; print(concat3(" ", repeat_string("-", bubblewidth - 2), " \n"))
|
||||||
end
|
end
|
||||||
|
|
||||||
function drawtiger () = print(indentstring(TIGER, INDENT, ""))
|
function draw_tiger () = print(indent_string(TIGER, INDENT, ""))
|
||||||
|
|
||||||
var text := getinput()
|
var text := getinput()
|
||||||
var wrappedtext := wrapstring(text, MAXWIDTH)
|
var wrappedtext := wrap_string(text, MAXWIDTH)
|
||||||
in
|
in
|
||||||
drawtextbubble(wrappedtext);
|
draw_textbubble(wrappedtext);
|
||||||
drawtiger();
|
draw_tiger();
|
||||||
print("\n");
|
print("\n");
|
||||||
flush()
|
flush()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user