Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- alias parsecalc {
- var %x 1
- var %open $false
- var %count 0
- var %ret
- while (%x < $len($1-)) {
- if ($mid($1-,%x,1) == $chr(40)) {
- if %open == $false {
- var %open $true
- var %ret $+(%ret,%x,$chr(1))
- }
- inc %count
- }
- else if ($v1 == $chr(41)) {
- dec %count
- if (%count == 0 && %open) {
- var %open $false
- var %ret $+(%ret,%x,$chr(4))
- }
- }
- inc %x
- }
- var %ret $+(%ret,$iif($right(%ret,1) == $chr(1),%x))
- return %ret
- }
- alias calctok {
- hinc -m CALCTOKEN COUNT
- var %htable CALCTOKEN $+ $hget(CALCTOKEN,COUNT)
- hadd -m %htable COUNT 0
- var %tokens $1-
- var %numberOfTokens $pos(%tokens,$chr(40),0)
- var %currentCalculation
- while %numberOfTokens {
- var %numberOfOpeners $pos(%tokens,$chr(40),0)
- var %currentOpenerOffset $pos(%tokens, $chr(40), %numberOfOpeners)
- var %currentCloserOffset $calc(%currentOpenerOffset + $pos($mid(%tokens,%currentOpenerOffset $+ -),$chr(41),1))
- var %length $calc(%currentCloserOffset - %currentOpenerOffset)
- var %currentCalculation $mid($mid(%tokens,$calc(%currentOpenerOffset),%length),2,-1)
- var %tokens $mid(%tokens,1,$calc(%currentOpenerOffset - 1)) $mid(%tokens, %currentCloserOffset $+ -)
- if ( !isin %currentCalculation) spush %htable %currentCalculation
- else {
- var %z $pos(%currentCalculation,,0)
- while %z {
- var %currTok $spop(%htable)
- var %currentCalculation $replacetok(%currentCalculation,,-1,$calc(%currTok))
- if ( !isin %currentCalculation) spush %htable %currentCalculation
- dec %z
- }
- }
- dec %numberOfTokens
- }
- hfree %htable
- return $calc(%currentCalculation)
- }
- alias replacetok {
- var %pos $pos($1,$2,$iif($3 > 0,$3,$calc($pos($1,$2,0) + $3 + 1)))
- return $+($left($1,$calc(%pos - 1)),$$4,$right($1,- $+ $calc(%pos + $len($2) - 1)))
- }
- alias -l spush {
- var %htable $1
- hinc -m %htable COUNT
- hadd -m %htable $hget(%htable,COUNT) $2-
- }
- alias -l spop {
- var %htable $1, %count $hget(%htable,COUNT)
- hdec -m %htable COUNT
- return $hget(%htable,%count)
- }
- alias parseidentifiers {
- var %x 1
- var %inIdentifier
- var %currentIdentifier
- var %return $1-
- while %x <= $len($1-) {
- var %currentChar $mid($1-,%x,1)
- if %currentChar == $ {
- var %inIdentifier $true
- var %currentIdentifier $
- }
- else if %currentChar == $chr(40) || %currentChar == $chr(32) {
- if (%inIdentifier) && %currentChar == $chr(40) {
- var %z $token($parsecalc($right($1-,- $+ $calc(%x - 1))),1,4)
- var %start $calc($token(%z,1,1) - 1)
- var %end $token(%z,2,1))
- var %length $calc(%end - %start + 1)
- var %currentIdentifier %currentIdentifier $+ $mid($right($1-, - $+ $calc(%x - 1)),%start,%length)
- var %return $replacetok(%return,%currentIdentifier,1,$(%currentIdentifier,2))
- var %inIdentifier $false
- }
- else if %inIdentifier && %currentChar == $chr(32) {
- var %return $replacetok(%return,%currentIdentifier,1,$(%currentIdentifier,2))
- var %inIdentifier $false
- }
- }
- else if %inIdentifier {
- if (%currentChar != $chr(41)) {
- var %currentIdentifier %currentIdentifier $+ %currentChar
- }
- }
- inc %x
- }
- if (%inIdentifier) {
- var %return $replacetok(%return,%currentIdentifier,1$(%currentIdentifier,2))
- }
- return %return
- }
- on 1:input:@DEBUG: {
- if ($left($1-,1) != /) {
- tokenize 32 $parseidentifiers($1-)
- var %x $parsecalc($1-)
- var %y $numtok(%x,4)
- var %ret $1-
- while %y {
- var %currTok $token(%x,%y,4)
- var %start $token(%currTok,1,1)
- var %end $token(%currTok,2,1)
- var %len $calc(%end - %start + 1)
- var %substr $mid($1-,%start,%len)
- var %calc $calctok(%substr)
- var %ret $replacetok(%ret,%substr,1,$+($chr(40),%substr == %calc,$chr(41)))
- dec %y
- }
- echo @DEBUG %ret
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement