fork download
  1. constructs = {
  2. 1: {
  3. 1: {
  4. 1: print
  5. }
  6. }
  7. }
  8.  
  9. def parse(code):
  10. def parse_expr(index):
  11. while index < size and code[index] == 0:
  12. index += 1 # skip 0s
  13. try:
  14. call = []
  15. while True:
  16. key1, key2, key3, pos = code[index: (next_index := index + 4)]
  17. if next_index < size and code[next_index] == 0:
  18. raise ValueError # escape the token
  19. if not call:
  20. call.append(constructs[key1][key2][key3])
  21. if pos == -1:
  22. return call, next_index
  23. obj, index = parse_expr(next_index)
  24. call.append(obj)
  25. except (ValueError, KeyError):
  26. return code[index], index + 1
  27. size = len(code)
  28. index = 0
  29. result = []
  30. while index < size:
  31. obj, index = parse_expr(index)
  32. result.append(obj)
  33. return result
  34.  
  35. print(parse([1, 1, 1, 1, 'Hello world', 1, 1, 1, 2, 'etc', 1, 1, 1, -1]))
  36. print(parse([1, 1, 1, 1, 1, 1, 1, 1, 'Hello world', 1, 1, 1, -1, 1, 1, 1, 2, 'etc', 1, 1, 1, -1]))
  37. print(parse([1, 1, 1, 1, 'a', 1, 1, 1, 2, 1, 1, 1, 1, 'b', 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 'c', 1, 1, 1, -1]))
Success #stdin #stdout 0.1s 14092KB
stdin
Standard input is empty
stdout
[[<built-in function print>, 'Hello world', 'etc']]
[[<built-in function print>, [<built-in function print>, 'Hello world'], 'etc']]
[[<built-in function print>, 'a', [<built-in function print>, 'b']], [<built-in function print>, 'c']]