Compare commits

..

2 Commits

Author SHA1 Message Date
Scott Richmond
de8f5c7cf4 baby's first eliza 2024-06-10 18:27:00 -04:00
Scott Richmond
5913f9b594 add string manipulation functions 2024-06-10 18:26:48 -04:00
5 changed files with 162 additions and 5 deletions

View File

@ -339,6 +339,29 @@ fn join {
)
}
fn split {
"Takes a string, and turns it into a list of strings, breaking on the separator."
(str as :string, break as :string) -> base :split (break, str)
}
fn trim {
"Trims whitespace from a string. Takes an optional argument, `:left` or `:right`, to trim only on the left or right."
(str as :string) -> base :trim (str)
(str as :string, :left) -> base :triml (str)
(str as :string, :right) -> base :trimr (str)
}
fn upcase {
"Takes a string and returns it in all uppercase. Works only for ascii characters."
(str as :string) -> base :upcase (str)
}
fn downcase {
"Takes a string and returns it in all lowercase. Works only for ascii characters."
(str as :string) -> base :downcase (str)
}
& in another prelude, with a better actual base language than Java (thanks, Rich), counting strings would be reasonable but complex: count/bytes, count/points, count/glyphs. Java's UTF16 strings make this unweildy.
& TODO: add trim, trim/left, trim/right; pad/left, pad/right
@ -618,6 +641,8 @@ fn slice {
base :into ([], slice)
}
}
(str as :string, end as :number) -> base :str_slice (str, 0, end)
(str as :string, start as :number, end as :number) -> base :str_slice (str, start, end)
}
&&& keywords: funny names
@ -1326,6 +1351,7 @@ pkg Prelude {
div/0
div/safe
doc!
downcase
each!
empty?
eq?
@ -1413,6 +1439,7 @@ pkg Prelude {
slice
some
some?
split
square
store!
string
@ -1421,6 +1448,7 @@ pkg Prelude {
sum_of_squares
tan
tau
trim
tuple?
turn/deg
turn/rad
@ -1431,6 +1459,7 @@ pkg Prelude {
unbox
unwrap!
unwrap_or
upcase
update
update!
values

114
sandbox.ld Normal file
View File

@ -0,0 +1,114 @@
let input = "I remember my mother"
print! ("DOCTOR")
print! ("> ", input)
let sanitized = do input > trim > downcase
& ensuring we have spaces at the beginning and end
& this lets us match patterns as written below
let padded = join ([" ", sanitized, " "])
fn switch_persons {
("i") -> "you"
("you") -> "i"
("am") -> "are"
("me") -> "you"
("my") -> "your"
(x) -> x
}
fn repersonalize (x) -> do x >
trim >
split (_, " ") >
map (switch_persons, _) >
join (_, " ")
let output = match padded with {
"{x} hello {y}" -> "How do you do. Please state your problem"
"{x} hi {y}" -> "How do you do. Please state your problem"
"{x} computer {y}" -> random ([
"Do computers worry you"
"What do you think about machines"
"Why do you mention computers"
"What do you think machines have to do with your problem"
])
"{x} name {y}" -> "I am not interested in names"
"{x} sorry {y}" -> random ([
"Please don't apologize"
"Apologies are not necessary"
"What feelings do you have when you apologize"
])
"{x} i remember {y}" -> {
let switched = repersonalize (y)
random ([
"Do you often think of {switched}"
"Does thinking of {switched} bring anything else to mind"
"What else do you remember"
"Why do you recall {switched} right now"
"What in the present situation reminds you of {switched}"
"What is the connection between me and {switched}"
])
}
"{x} do you remember {y}" -> {
let switched = repersonalize (y)
random ([
"Did you think I would forget {switched}"
"Why do you think I should recall {switched} now"
"What about {switched}"
"You mentioned {switched}"
])
}
"{x} if {y}" -> {
let switched = repersonalize (y)
random ([
"Do you reall think that its likely that {switched}"
"Do you wish that {switched}"
"What do you think about {switched}"
"Really--if {switched}"
])
}
"{x} i dreamt {y}" -> {
let switched = repersonlize (y)
random ([
"Really--{y}"
"Have you ever fantasized {y} while you were awake"
"Have you dreamt {y} before"
])
}
"{x} dream about {y}" -> {
let switched = repersonalize (y)
"How do you feel about {switched} in reality"
}
"{x} dream {y}" -> random ([
"What does this dream suggest to you"
"Do you dream often"
"What persons appear in your dreams"
"Don't you believe that dream has to do with your problem"
])
"{x} my mother {y}" -> {
let switched = repersonalize (y)
random ([
"Who else in your family {y}"
"Tell me more about your family"
])
}
"{x} my father {y}" -> random ([
"Your father"
"Does he influence you strongly"
"What else comes to mind when you think of your father"
])
_ -> random ([
"Very interesting"
"I am not sure I understand you fully"
"What does that suggest to you"
"Please continue"
"Go on"
"Do you feel strongly about discussing such things"
])
}
print! (">>> ", upcase (output))

View File

@ -281,6 +281,13 @@
"range" range
"unbox" unbox
"store!" store!
"split" string/split
"upcase" string/ascii-upper
"downcase" string/ascii-lower
"trim" string/trim
"trimr" string/trimr
"triml" string/triml
"str_slice" string/slice
})
(def base (let [b @{}]

View File

@ -36,7 +36,7 @@
# (set (out :errors) (validated :errors))
(each err (validated :errors)
(e/validation-error err))
(break out))
(break (-> out j/encode string)))
(setdyn :out console)
(try
(set result (i/interpret (parsed :ast) ctx))
@ -55,6 +55,13 @@
# (comment
(do
(-> (ludus `
doc! (mod)
`)))
(def source `let foo = foo bar baz`)
(def out (-> source
ludus
j/decode))
(def console (out "console"))
(print console)
(def result (out "result"))
(print result)
)