From b974b28fc25c4278f8133deec50b431508b165c1 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sat, 2 Dec 2017 20:42:30 +0100 Subject: [PATCH] Added reactions to some specific strings. --- tigersay.tig | 74 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/tigersay.tig b/tigersay.tig index 4276453..e98ecca 100644 --- a/tigersay.tig +++ b/tigersay.tig @@ -9,6 +9,8 @@ let var INDENT := " " var MAXWIDTH := 40 + var VERSION := "1.1.0 (02/12/2017)" + /* The cow is unused, and taken directly from the cowsay program. */ var COW := "\ \\\ ^__^ \n\ @@ -31,6 +33,10 @@ let \ __/ /_..-' ` ),' // \n\ \ ((__.-'((___..-'' \\__.' " + var VERSION_STRING := concat("I am Tigersay, version ",concat(VERSION,".")) + var HELP_STRING := concat("I am Tigersay, version ",concat(VERSION,". Fear my claws and dopy face. I was made by Jon Michael Aanes (aanes.xyz). I do not like command-line options. Instead, please feed me by pipe.")) + + var ASLAN_QUOTE := "Laugh and fear not, creatures. Now that you are no longer dumb and witless, you need not always be grave. For jokes as well as justice come in with speech. -- Aslan, The Chronicles of Narnia" /***********/ /* Utility */ @@ -49,6 +55,9 @@ let /* Number / Maths */ + function min (a:int, b:int) : int = + if a < b then a else b + function max (a:int, b:int) : int = if a > b then a else b @@ -97,7 +106,8 @@ let function substring_absolute (text:string, i1:int, i2:int) : string = /* Uses absolute coordinates to determine substring */ - substring(text, i1, i2 - i1 + 1) + if i2 < i1 then "" + else substring(text, i1, i2 - i1 + 1) function count_substring_occurances (text:string, char:string) : int = let var count := 0 @@ -108,6 +118,21 @@ let ; count end + function strip (text:string) : string = + let var start_i := -1 + var stop_i := 0 + var char := "" + in for i := 0 to size(text) - 1 do + ( char := substring(text, i, 1) + ; if not(is_whitespace(char)) & start_i = -1 then + start_i := i + ; if not(is_whitespace(char)) then + stop_i := i ) + ; if start_i = -1 + then "" + else substring_absolute(text, start_i, stop_i) + end + function repeat_string (str:string, rep:int) : string = let var out := "" in for i := 1 to rep do @@ -161,11 +186,11 @@ let then 0 else 1 + ll_length(ll.next) - /**/ + /* String operations using linked lists */ function split_words (text:string) : stringll = let var out := ll_new() - var prev_word_start_i := -1 + var prev_word_start_i := 0 var char := "" in for i := 0 to size(text) - 1 do ( char := substring(text, i, 1) @@ -175,6 +200,7 @@ let then prev_word_start_i := i ; if is_whitespace(char) then prev_word_start_i := -1 ) + ; ll_append(out, substring_absolute(text, prev_word_start_i, size(text) - 1)) ; out end @@ -198,6 +224,46 @@ let else splitstr ) end + /* Fun stuff */ + + type map = { key: string, value: string, next: map } + type string_option = { some: string } + + function map_lookup ( map : map, key : string ) : string_option = + ( while map <> nil & key <> map.key do + map := map.next + ; if map = nil then nil + else string_option { some = map.value } ) + + var REACTIONS := + map { key = "", value = "Quiet type. Has the cat got your tongue?", next = + map { key = "aslan", value = ASLAN_QUOTE, next = + map { key = "Aslan", value = ASLAN_QUOTE, next = + map { key = "version", value = VERSION_STRING, next = + map { key = "help", value = HELP_STRING, next = + + nil }}}}} + + function is_moo (input : string) : string_option = + if size(input) <> 0 + & (substring(input, 0, 1) = "M" | substring(input, 0, 1) = "m") + & count_substring_occurances(input, "o") = size(input) - 1 then + string_option { + some = concat( if "m" = substring(input, 0, 1) then "g" else "G" + , repeat_string("r", count_substring_occurances(input, "o"))) } + else + nil + + function get_reaction_or_text (input: string): string = + let var maybe_string := map_lookup(REACTIONS, input) + var maybe_moo := is_moo(input) + in if maybe_string <> nil + then maybe_string.some + else if maybe_moo <> nil + then maybe_moo.some + else input + end + /**************/ /* Draw stuff */ @@ -214,7 +280,7 @@ let function draw_tiger () = print(indent_string(TIGER, INDENT, "")) - var text := getinput() + var text := get_reaction_or_text(strip(getinput())) var wrappedtext := wrap_string(text, MAXWIDTH) in draw_textbubble(wrappedtext);