Code:

function bta($a, $b)

dim $,$i,$m,$t

$a = $a+#+$b ; delim operands with #

$b = 1

for $i = 1 to len($a)

$t = left(right($a,$i),1)

if $t = "-" ; was n minus?

$ = $ - $m * 2 ; subtract last $m (*2 because we already added it previous)

else

$m = $b * $t ; multiply base x num (the # in stream has no effect)

$ = $ + $m ; accumulate n

$b = iif($t=#,1,$b*3) ; next base or reset to 1 on delim

endif

next

do

$b = $ mod 3

if $b/2 ; ternary 2 ?

$b = $b/-2 ; change 2 to -1 and -2 to 1

$ = $ - $b ; carry forward

endif

$bta = "" + $b + $bta ; answer (coerce b to string with "")

$ = $ / 3 ; next base

until $=0 ; done?

endfunction