recipe string-equal [
default-space:address:array:location <- new location:type, 30:literal
a:address:array:character <- next-ingredient
a-len:integer <- length a:address:array:character/deref
b:address:array:character <- next-ingredient
b-len:integer <- length b:address:array:character/deref
{
trace [string-equal], [comparing lengths]
length-equal?:boolean <- equal a-len:integer, b-len:integer
break-if length-equal?:boolean
reply 0:literal
}
trace [string-equal], [comparing characters]
i:integer <- copy 0:literal
{
done?:boolean <- greater-or-equal i:integer, a-len:integer
break-if done?:boolean
a2:character <- index a:address:array:character/deref, i:integer
b2:character <- index b:address:array:character/deref, i:integer
{
chars-match?:boolean <- equal a2:character, b2:character
break-if chars-match?:boolean
reply 0:literal
}
i:integer <- add i:integer, 1:literal
loop
}
reply 1:literal
]
scenario string-equal-reflexive [
run [
default-space:address:array:location <- new location:type, 30:literal
x:address:array:character <- new [abc]
3:boolean/raw <- string-equal x:address:array:character, x:address:array:character
]
memory-should-contain [
3 <- 1
]
]
scenario string-equal-identical [
run [
default-space:address:array:location <- new location:type, 30:literal
x:address:array:character <- new [abc]
y:address:array:character <- new [abc]
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
]
memory-should-contain [
3 <- 1
]
]
scenario string-equal-distinct-lengths [
run [
default-space:address:array:location <- new location:type, 30:literal
x:address:array:character <- new [abc]
y:address:array:character <- new [abcd]
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
]
memory-should-contain [
3 <- 0
]
trace-should-contain [
string-equal: comparing lengths
]
trace-should-not-contain [
string-equal: comparing characters
]
]
scenario string-equal-with-empty [
run [
default-space:address:array:location <- new location:type, 30:literal
x:address:array:character <- new []
y:address:array:character <- new [abcd]
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
]
memory-should-contain [
3 <- 0
]
]
scenario string-equal-common-lengths-but-distinct [
run [
default-space:address:array:location <- new location:type, 30:literal
x:address:array:character <- new [abc]
y:address:array:character <- new [abd]
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
]
memory-should-contain [
3 <- 0
]
]
container buffer [
length:integer
data:address:array:character
]
recipe init-buffer [
default-space:address:array:location <- new location:type, 30:literal
result:address:buffer <- new buffer:type
len:address:integer <- get-address result:address:buffer/deref, length:offset
len:address:integer/deref <- copy 0:literal
s:address:address:array:character <- get-address result:address:buffer/deref, data:offset
capacity:integer <- next-ingredient
s:address:address:array:character/deref <- new character:type, capacity:integer
reply result:address:buffer
]
recipe grow-buffer [
default-space:address:array:location <- new location:type, 30:literal
in:address:buffer <- next-ingredient
x:address:address:array:character <- get-address in:address:buffer/deref, data:offset
oldlen:integer <- length x:address:address:array:character/deref/deref
newlen:integer <- multiply oldlen:integer, 2:literal
olddata:address:array:character <- copy x:address:address:array:character/deref
x:address:address:array:character/deref <- new character:type, newlen:integer
i:integer <- copy 0:literal
{
done?:boolean <- greater-or-equal i:integer, oldlen:integer
break-if done?:boolean
src:character <- index olddata:address:array:character/deref, i:integer
dest:address:character <- index-address x:address:address:array:character/deref/deref, i:integer
dest:address:character/deref <- copy src:character
i:integer <- add i:integer, 1:literal
loop
}
reply in:address:buffer
]
recipe buffer-full? [
default-space:address:array:location <- new location:type, 30:literal
in:address:buffer <- next-ingredient
len:integer <- get in:address:buffer/deref, length:offset
s:address:array:character <- get in:address:buffer/deref, data:offset
capacity:integer <- length s:address:array:character/deref
result:boolean <- greater-or-equal len:integer, capacity:integer
reply result:boolean
]
recipe buffer-append [
default-space:address:array:location <- new location:type, 30:literal
in:address:buffer <- next-ingredient
c:character <- next-ingredient
{
full?:boolean <- buffer-full? in:address:buffer
break-unless full?:boolean
in:address:buffer <- grow-buffer in:address:buffer
}
len:address:integer <- get-address in:address:buffer/deref, length:offset
s:address:array:character <- get in:address:buffer/deref, data:offset
dest:address:character <- index-address s:address:array:character/deref, len:address:integer/deref
dest:address:character/deref <- copy c:character
len:address:integer/deref <- add len:address:integer/deref, 1:literal
reply in:address:buffer/same-as-ingredient:0
]
scenario buffer-append-works [
run [
default-space:address:array:location <- new location:type, 30:literal
x:address:buffer <- init-buffer 3:literal
s1:address:array:character <- get x:address:buffer/deref, data:offset
x:address:buffer <- buffer-append x:address:buffer, 97:literal
x:address:buffer <- buffer-append x:address:buffer, 98:literal
x:address:buffer <- buffer-append x:address:buffer, 99:literal
s2:address:array:character <- get x:address:buffer/deref, data:offset
1:boolean/raw <- equal s1:address:array:character, s2:address:array:character
2:array:character/raw <- copy s2:address:array:character/deref
+buffer-filled
x:address:buffer <- buffer-append x:address:buffer, 100:literal
s3:address:array:character <- get x:address:buffer/deref, data:offset
10:boolean/raw <- equal s1:address:array:character, s3:address:array:character
11:integer/raw <- get x:address:buffer/deref, length:offset
12:array:character/raw <- copy s3:address:array:character/deref
]
memory-should-contain [
1 <- 1
2 <- 3
3 <- 97
4 <- 98
5 <- 99
10 <- 0
11 <- 4
12 <- 6
13 <- 97
14 <- 98
15 <- 99
16 <- 100
17 <- 0
18 <- 0
]
]
recipe integer-to-decimal-string [
default-space:address:array:location <- new location:type, 30:literal
n:integer <- next-ingredient
{
break-if n:integer
result:address:array:character <- new [0]
reply result:address:array:character
}
negate-result:boolean <- copy 0:literal
{
negative?:boolean <- lesser-than n:integer, 0:literal
break-unless negative?:boolean
negate-result:boolean <- copy 1:literal
n:integer <- multiply n:integer, -1:literal
}
tmp:address:buffer <- init-buffer 30:literal
digit-base:integer <- copy 48:literal
{
done?:boolean <- equal n:integer, 0:literal
break-if done?:boolean
n:integer, digit:integer <- divide-with-remainder n:integer, 10:literal
c:character <- add digit-base:integer, digit:integer
tmp:address:buffer <- buffer-append tmp:address:buffer, c:character
loop
}
{
break-unless negate-result:boolean
tmp:address:buffer <- buffer-append tmp:address:buffer, 45:literal
}
len:integer <- get tmp:address:buffer/deref, length:offset
buf:address:array:character <- get tmp:address:buffer/deref, data:offset
result:address:array:character <- new character:type, len:integer
i:integer <- subtract len:integer, 1:literal
j:integer <- copy 0:literal
{
done?:boolean <- lesser-than i:integer, 0:literal
break-if done?:boolean
src:character <- index buf:address:array:character/deref, i:integer
dest:address:character <- index-address result:address:array:character/deref, j:integer
dest:address:character/deref <- copy src:character
i:integer <- subtract i:integer, 1:literal
j:integer <- add j:integer, 1:literal
loop
}
reply result:address:array:character
]
scenario integer-to-decimal-digit-zero [
run [
1:address:array:character/raw <- integer-to-decimal-string 0:literal
2:array:character/raw <- copy 1:address:array:character/deref/raw
]
memory-should-contain [
2:string <- [0]
]
]
scenario integer-to-decimal-digit-positive [
run [
1:address:array:character/raw <- integer-to-decimal-string 234:literal
2:array:character/raw <- copy 1:address:array:character/deref/raw
]
memory-should-contain [
2:string <- [234]
]
]
scenario integer-to-decimal-digit-negative [
run [
1:address:array:character/raw <- integer-to-decimal-string -1:literal
2:array:character/raw <- copy 1:address:array:character/deref/raw
]
memory-should-contain [
2 <- 2
3 <- 45
4 <- 49
]
]
recipe string-append [
default-space:address:array:location <- new location:type, 30:literal
a:address:array:character <- next-ingredient
a-len:integer <- length a:address:array:character/deref
b:address:array:character <- next-ingredient
b-len:integer <- length b:address:array:character/deref
result-len:integer <- add a-len:integer, b-len:integer
result:address:array:character <- new character:type, result-len:integer
result-idx:integer <- copy 0:literal
i:integer <- copy 0:literal
{
a-done?:boolean <- greater-or-equal i:integer, a-len:integer
break-if a-done?:boolean
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
in:character <- index a:address:array:character/deref, i:integer
out:address:character/deref <- copy in:character
i:integer <- add i:integer, 1:literal
result-idx:integer <- add result-idx:integer, 1:literal
loop
}
i:integer <- copy 0:literal
{
b-done?:boolean <- greater-or-equal i:integer, b-len:integer
break-if b-done?:boolean
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
in:character <- index b:address:array:character/deref, i:integer
out:address:character/deref <- copy in:character
i:integer <- add i:integer, 1:literal
result-idx:integer <- add result-idx:integer, 1:literal
loop
}
reply result:address:array:character
]
scenario string-append-1 [
run [
1:address:array:character/raw <- new [hello,]
2:address:array:character/raw <- new [ world!]
3:address:array:character/raw <- string-append 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy 3:address:array:character/raw/deref
]
memory-should-contain [
4:string <- [hello, world!]
]
]
recipe interpolate [
default-space:array:address:location <- new location:type, 60:literal
template:address:array:character <- next-ingredient
tem-len:integer <- length template:address:array:character/deref
result-len:integer <- copy tem-len:integer
{
a:address:array:character, arg-received?:boolean <- next-ingredient
break-unless arg-received?:boolean
a-len:integer <- length a:address:array:character/deref
result-len:integer <- add result-len:integer, a-len:integer
result-len:integer <- subtract result-len:integer, 1:literal
loop
}
rewind-ingredients
_ <- next-ingredient
result:address:array:character <- new character:type, result-len:integer
result-idx:integer <- copy 0:literal
i:integer <- copy 0:literal
{
a:address:array:character, arg-received?:boolean <- next-ingredient
break-unless arg-received?:boolean
{
tem-done?:boolean <- greater-or-equal i:integer, tem-len:integer
break-if tem-done?:boolean, 2:blocks
in:character <- index template:address:array:character/deref, i:integer
underscore?:boolean <- equal in:character, 95:literal
break-if underscore?:boolean
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
out:address:character/deref <- copy in:character
i:integer <- add i:integer, 1:literal
result-idx:integer <- add result-idx:integer, 1:literal
loop
}
j:integer <- copy 0:literal
{
arg-done?:boolean <- greater-or-equal j:integer, a-len:integer
break-if arg-done?:boolean
in:character <- index a:address:array:character/deref, j:integer
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
out:address:character/deref <- copy in:character
j:integer <- add j:integer, 1:literal
result-idx:integer <- add result-idx:integer, 1:literal
loop
}
i:integer <- add i:integer, 1:literal
loop
}
{
tem-done?:boolean <- greater-or-equal i:integer, tem-len:integer
break-if tem-done?:boolean
in:character <- index template:address:array:character/deref, i:integer
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
out:address:character/deref <- copy in:character
i:integer <- add i:integer, 1:literal
result-idx:integer <- add result-idx:integer, 1:literal
loop
}
reply result:address:array:character
]
scenario interpolate-works [
run [
1:address:array:character/raw <- new [abc _]
2:address:array:character/raw <- new [def]
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy 3:address:array:character/raw/deref
]
memory-should-contain [
4:string <- [abc def]
]
]
scenario interpolate-at-start [
run [
1:address:array:character/raw <- new [_, hello!]
2:address:array:character/raw <- new [abc]
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy 3:address:array:character/raw/deref
]
memory-should-contain [
4:string <- [abc, hello!]
16 <- 0
]
]
scenario interpolate-at-end [
run [
1:address:array:character/raw <- new [hello, _]
2:address:array:character/raw <- new [abc]
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
4:array:character/raw <- copy 3:address:array:character/raw/deref
]
memory-should-contain [
4:string <- [hello, abc]
]
]