1
0

Added reactions to some specific strings.

This commit is contained in:
Jon Michael Aanes 2017-12-02 20:42:30 +01:00
parent 4aa6987ab5
commit b974b28fc2

View File

@ -9,6 +9,8 @@ let
var INDENT := " " var INDENT := " "
var MAXWIDTH := 40 var MAXWIDTH := 40
var VERSION := "1.1.0 (02/12/2017)"
/* The cow is unused, and taken directly from the cowsay program. */ /* The cow is unused, and taken directly from the cowsay program. */
var COW := "\ var COW := "\
\\\ ^__^ \n\ \\\ ^__^ \n\
@ -31,6 +33,10 @@ let
\ __/ /_..-' ` ),' // \n\ \ __/ /_..-' ` ),' // \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 */ /* Utility */
@ -49,6 +55,9 @@ let
/* Number / Maths */ /* Number / Maths */
function min (a:int, b:int) : int =
if a < b then a else b
function max (a:int, b:int) : int = function max (a:int, b:int) : int =
if a > b then a else b if a > b then a else b
@ -97,7 +106,8 @@ let
function substring_absolute (text:string, i1:int, i2:int) : string = function substring_absolute (text:string, i1:int, i2:int) : string =
/* Uses absolute coordinates to determine substring */ /* 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 = function count_substring_occurances (text:string, char:string) : int =
let var count := 0 let var count := 0
@ -108,6 +118,21 @@ let
; count ; count
end 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 = function repeat_string (str:string, rep:int) : string =
let var out := "" let var out := ""
in for i := 1 to rep do in for i := 1 to rep do
@ -161,11 +186,11 @@ let
then 0 then 0
else 1 + ll_length(ll.next) else 1 + ll_length(ll.next)
/**/ /* String operations using linked lists */
function split_words (text:string) : stringll = function split_words (text:string) : stringll =
let var out := ll_new() let var out := ll_new()
var prev_word_start_i := -1 var prev_word_start_i := 0
var char := "" var char := ""
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)
@ -175,6 +200,7 @@ let
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
) )
; ll_append(out, substring_absolute(text, prev_word_start_i, size(text) - 1))
; out ; out
end end
@ -198,6 +224,46 @@ let
else splitstr ) else splitstr )
end 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 */ /* Draw stuff */
@ -214,7 +280,7 @@ let
function draw_tiger () = print(indent_string(TIGER, INDENT, "")) 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) var wrappedtext := wrap_string(text, MAXWIDTH)
in in
draw_textbubble(wrappedtext); draw_textbubble(wrappedtext);