fork download
  1. BEGIN {
  2. print pretty_format("3 19 [ + -2 / ] $")
  3. print pretty_format("[ 1 2 ] [ 3 ]")
  4. print pretty_format("1")
  5. print pretty_format("[ 1")
  6. print pretty_format("1 ]")
  7. }
  8.  
  9. function pretty_format(str, b, i, q, ql, r, s, t) {
  10.  
  11. ql = split("[ " str " ]", q)
  12.  
  13. r = ""
  14. s = ""
  15. b = 0
  16. for(i = 1; i <= ql; i++) {
  17. t = q[i]
  18. if (t == "[") {
  19. r = r s "["
  20. s = ""
  21. b++
  22. }
  23. else if (t == "]") {
  24. if (--b < 0) return "error"
  25. r = r "]"
  26. s = ", "
  27. }
  28. else {
  29. r = r s "'" t "'"
  30. s = ", "
  31. }
  32. }
  33. if (b > 0) return "error"
  34.  
  35. return r
  36. }
  37.  
Success #stdin #stdout 0.01s 4276KB
stdin
Standard input is empty
stdout
['3', '19', ['+', '-2', '/'], '$']
[['1', '2'], ['3']]
['1']
error
error