Balanced brackets: Difference between revisions
Content deleted Content added
Line 475: | Line 475: | ||
script mf |
script mf |
||
on bracket(_) |
on bracket(_) |
||
iff((random number) < 0.5, "[", "]") |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end bracket |
end bracket |
||
end script |
end script |
||
Line 492: | Line 488: | ||
set lngChars to length of xs |
set lngChars to length of xs |
||
if lngChars > 0 then |
if lngChars > 0 then |
||
set iNext to iDepth + iff(item 1 of xs = "[", 1, -1) |
|||
set iNext to iDepth + 1 |
|||
else |
|||
set iNext to iDepth - 1 |
|||
⚫ | |||
if iNext < 0 then -- closing bracket unmatched |
if iNext < 0 then -- closing bracket unmatched |
||
Line 504: | Line 496: | ||
errorIndex(items 2 thru -1 of xs, iNext, iIndex + 1) |
errorIndex(items 2 thru -1 of xs, iNext, iIndex + 1) |
||
else -- end of string |
else -- end of string |
||
iff(iNext = 0, -1, iIndex) |
|||
-1 |
|||
else |
|||
iIndex -- position of problem |
|||
end if |
|||
end if |
end if |
||
end if |
end if |
||
else |
else |
||
iff(iDepth = 0, -1, iIndex) |
|||
-1 |
|||
else |
|||
iIndex |
|||
end if |
|||
end if |
end if |
||
end errorIndex |
end errorIndex |
||
Line 539: | Line 523: | ||
set blnOK to (i = -1) |
set blnOK to (i = -1) |
||
set strStatus to iff(blnOK, "OK", "problem") |
|||
set strStatus to "OK" |
|||
⚫ | |||
set strStatus to "problem" |
|||
⚫ | |||
set strLine to "'" & s & "'" & ¬ |
set strLine to "'" & s & "'" & ¬ |
||
(items (w + 2) thru -1 of strPad) & strStatus |
(items (w + 2) thru -1 of strPad) & strStatus |
||
set strPointer to iff(blnOK, "", linefeed & nreps(space, i + 1) & "^") |
|||
set strPointer to linefeed & nreps(space, i + 1) & "^" |
|||
end if |
|||
intercalate("", {strLine, strPointer}) |
intercalate("", {strLine, strPointer}) |
||
end report |
end report |
||
Line 611: | Line 589: | ||
return o & s |
return o & s |
||
end nreps |
end nreps |
||
-- Value of one of two expressions |
|||
-- Bool -> a -> b -> c |
|||
on iff(bln, f, g) |
|||
if bln then |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if class of e is handler then |
|||
mReturn(e)'s lambda() |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end iff |
|||
-- Lift 2nd class function into 1st class wrapper |
-- Lift 2nd class function into 1st class wrapper |
||
Line 627: | Line 620: | ||
property lambda : f |
property lambda : f |
||
end script |
end script |
||
end mClosure |
end mClosure |
||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 634: | Line 628: | ||
'' OK |
'' OK |
||
'[]' OK |
'[]' OK |
||
'[[ |
'][[]' problem |
||
^ |
|||
'[]] |
'[]]]]]' problem |
||
^ |
^ |
||
' |
']]]]]]][' problem |
||
^ |
|||
⚫ | |||
^ |
^ |
||
' |
'[[][[[]]]]' OK |
||
⚫ | |||
</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |