Advertisement
chemoelectric

Guess what this program does.

Mar 29th, 2023
2,885
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Icon 10.57 KB | None | 0 0
  1. global strings
  2. global variables
  3. global stack
  4. global continuations
  5.  
  6. procedure main ()
  7.   initialize_globals ()
  8.   @continuations[1]
  9. end
  10.  
  11. procedure initialize_globals ()
  12.   stack := []
  13.  
  14.   variables := []
  15.   put (variables, 0)
  16.   put (variables, 0)
  17.   put (variables, 0)
  18.   put (variables, 0)
  19.   put (variables, 0)
  20.   put (variables, 0)
  21.   put (variables, 0)
  22.   put (variables, 0)
  23.   put (variables, 0)
  24.   put (variables, 0)
  25.   put (variables, 0)
  26.   put (variables, 0)
  27.   put (variables, 0)
  28.   put (variables, 0)
  29.   put (variables, 0)
  30.  
  31.   continuations := list (104)
  32.   continuations[1] := create (routine_push (420, 2))
  33.   continuations[2] := create (routine_unary ("unary_neg", 3))
  34.   continuations[3] := create (routine_store (1, 4))
  35.   continuations[4] := create (routine_push (300, 5))
  36.   continuations[5] := create (routine_store (2, 6))
  37.   continuations[6] := create (routine_push (300, 7))
  38.   continuations[7] := create (routine_store (3, 8))
  39.   continuations[8] := create (routine_push (300, 9))
  40.   continuations[9] := create (routine_unary ("unary_neg", 10))
  41.   continuations[10] := create (routine_store (4, 11))
  42.   continuations[11] := create (routine_push (7, 12))
  43.   continuations[12] := create (routine_store (5, 13))
  44.   continuations[13] := create (routine_push (15, 14))
  45.   continuations[14] := create (routine_store (6, 15))
  46.   continuations[15] := create (routine_push (200, 16))
  47.   continuations[16] := create (routine_store (7, 17))
  48.   continuations[17] := create (routine_fetch (3, 18))
  49.   continuations[18] := create (routine_store (8, 19))
  50.   continuations[19] := create (routine_fetch (8, 20))
  51.   continuations[20] := create (routine_fetch (4, 21))
  52.   continuations[21] := create (routine_binary ("binary_gt", 22))
  53.   continuations[22] := create (routine_jz (23, 24))
  54.   continuations[24] := create (routine_fetch (1, 25))
  55.   continuations[25] := create (routine_store (9, 26))
  56.   continuations[26] := create (routine_fetch (9, 27))
  57.   continuations[27] := create (routine_fetch (2, 28))
  58.   continuations[28] := create (routine_binary ("binary_lt", 29))
  59.   continuations[29] := create (routine_jz (30, 31))
  60.   continuations[31] := create (routine_push (0, 32))
  61.   continuations[32] := create (routine_store (10, 33))
  62.   continuations[33] := create (routine_push (0, 34))
  63.   continuations[34] := create (routine_store (11, 35))
  64.   continuations[35] := create (routine_push (32, 36))
  65.   continuations[36] := create (routine_store (12, 37))
  66.   continuations[37] := create (routine_push (0, 38))
  67.   continuations[38] := create (routine_store (13, 39))
  68.   continuations[39] := create (routine_fetch (13, 40))
  69.   continuations[40] := create (routine_fetch (7, 41))
  70.   continuations[41] := create (routine_binary ("binary_lt", 42))
  71.   continuations[42] := create (routine_jz (43, 44))
  72.   continuations[44] := create (routine_fetch (11, 45))
  73.   continuations[45] := create (routine_fetch (11, 46))
  74.   continuations[46] := create (routine_binary ("binary_mul", 47))
  75.   continuations[47] := create (routine_push (200, 48))
  76.   continuations[48] := create (routine_binary ("binary_div", 49))
  77.   continuations[49] := create (routine_store (14, 50))
  78.   continuations[50] := create (routine_fetch (10, 51))
  79.   continuations[51] := create (routine_fetch (10, 52))
  80.   continuations[52] := create (routine_binary ("binary_mul", 53))
  81.   continuations[53] := create (routine_push (200, 54))
  82.   continuations[54] := create (routine_binary ("binary_div", 55))
  83.   continuations[55] := create (routine_store (15, 56))
  84.   continuations[56] := create (routine_fetch (14, 57))
  85.   continuations[57] := create (routine_fetch (15, 58))
  86.   continuations[58] := create (routine_binary ("binary_add", 59))
  87.   continuations[59] := create (routine_push (800, 60))
  88.   continuations[60] := create (routine_binary ("binary_gt", 61))
  89.   continuations[61] := create (routine_jz (62, 63))
  90.   continuations[63] := create (routine_push (48, 64))
  91.   continuations[64] := create (routine_fetch (13, 65))
  92.   continuations[65] := create (routine_binary ("binary_add", 66))
  93.   continuations[66] := create (routine_store (12, 67))
  94.   continuations[67] := create (routine_fetch (13, 68))
  95.   continuations[68] := create (routine_push (9, 69))
  96.   continuations[69] := create (routine_binary ("binary_gt", 70))
  97.   continuations[70] := create (routine_jz (71, 72))
  98.   continuations[72] := create (routine_push (64, 73))
  99.   continuations[73] := create (routine_store (12, 71))
  100.   continuations[71] := create (routine_fetch (7, 74))
  101.   continuations[74] := create (routine_store (13, 62))
  102.   continuations[62] := create (routine_fetch (11, 75))
  103.   continuations[75] := create (routine_fetch (10, 76))
  104.   continuations[76] := create (routine_binary ("binary_mul", 77))
  105.   continuations[77] := create (routine_push (100, 78))
  106.   continuations[78] := create (routine_binary ("binary_div", 79))
  107.   continuations[79] := create (routine_fetch (8, 80))
  108.   continuations[80] := create (routine_binary ("binary_add", 81))
  109.   continuations[81] := create (routine_store (10, 82))
  110.   continuations[82] := create (routine_fetch (14, 83))
  111.   continuations[83] := create (routine_fetch (15, 84))
  112.   continuations[84] := create (routine_binary ("binary_sub", 85))
  113.   continuations[85] := create (routine_fetch (9, 86))
  114.   continuations[86] := create (routine_binary ("binary_add", 87))
  115.   continuations[87] := create (routine_store (11, 88))
  116.   continuations[88] := create (routine_fetch (13, 89))
  117.   continuations[89] := create (routine_push (1, 90))
  118.   continuations[90] := create (routine_binary ("binary_add", 91))
  119.   continuations[91] := create (routine_store (13, 92))
  120.   continuations[92] := create (routine_jmp (39))
  121.   continuations[43] := create (routine_fetch (12, 93))
  122.   continuations[93] := create (routine_prtc (94))
  123.   continuations[94] := create (routine_fetch (9, 95))
  124.   continuations[95] := create (routine_fetch (5, 96))
  125.   continuations[96] := create (routine_binary ("binary_add", 97))
  126.   continuations[97] := create (routine_store (9, 98))
  127.   continuations[98] := create (routine_jmp (26))
  128.   continuations[30] := create (routine_push (10, 99))
  129.   continuations[99] := create (routine_prtc (100))
  130.   continuations[100] := create (routine_fetch (8, 101))
  131.   continuations[101] := create (routine_fetch (6, 102))
  132.   continuations[102] := create (routine_binary ("binary_sub", 103))
  133.   continuations[103] := create (routine_store (8, 104))
  134.   continuations[104] := create (routine_jmp (19))
  135.   continuations[23] := create (routine_halt ())
  136.  
  137.   return
  138. end
  139.  
  140. procedure routine_unary_not (i_kont)
  141.   repeat
  142.   {
  143.     stack[1] := (if stack[1] = 0 then 1 else 0)
  144.     @continuations[i_kont]
  145.   }
  146. end
  147.  
  148. procedure routine_unary_neg (i_kont)
  149.   repeat { stack[1] := -stack[1]; @continuations[i_kont] }
  150. end
  151.  
  152. procedure routine_unary (operation, i_kont)
  153.   case operation of
  154.   {
  155.     "unary_not" : routine_unary_not (i_kont)
  156.     "unary_neg" : routine_unary_neg (i_kont)
  157.     default : fail
  158.   }
  159. end
  160.  
  161. procedure routine_binary_and (i_kont)
  162.   local y
  163.   repeat {
  164.     y := pop (stack)
  165.     stack[1] := (if (stack[1] ~= 0 & y ~= 0) then 1 else 0)
  166.     @continuations[i_kont]
  167.   }
  168. end
  169.  
  170. procedure routine_binary_or (i_kont)
  171.   local y
  172.   repeat {
  173.     y := pop (stack)
  174.     stack[1] := (if (stack[1] ~= 0 | y ~= 0) then 1 else 0)
  175.     @continuations[i_kont]
  176.   }
  177. end
  178.  
  179. procedure routine_binary_lt (i_kont)
  180.   local y
  181.   repeat {
  182.     y := pop (stack)
  183.     stack[1] := (if stack[1] < y then 1 else 0)
  184.     @continuations[i_kont]
  185.   }
  186. end
  187.  
  188. procedure routine_binary_le (i_kont)
  189.   local y
  190.   repeat {
  191.     y := pop (stack)
  192.     stack[1] := (if stack[1] <= y then 1 else 0)
  193.     @continuations[i_kont]
  194.   }
  195. end
  196.  
  197. procedure routine_binary_gt (i_kont)
  198.   local y
  199.   repeat {
  200.     y := pop (stack)
  201.     stack[1] := (if stack[1] > y then 1 else 0)
  202.     @continuations[i_kont]
  203.   }
  204. end
  205.  
  206. procedure routine_binary_ge (i_kont)
  207.   local y
  208.   repeat {
  209.     y := pop (stack)
  210.     stack[1] := (if stack[1] >= y then 1 else 0)
  211.     @continuations[i_kont]
  212.   }
  213. end
  214.  
  215. procedure routine_binary_eq (i_kont)
  216.   local y
  217.   repeat {
  218.     y := pop (stack)
  219.     stack[1] := (if stack[1] = y then 1 else 0)
  220.     @continuations[i_kont]
  221.   }
  222. end
  223.  
  224. procedure routine_binary_ne (i_kont)
  225.   local y
  226.   repeat {
  227.     y := pop (stack)
  228.     stack[1] := (if stack[1] ~= y then 1 else 0)
  229.     @continuations[i_kont]
  230.   }
  231. end
  232.  
  233. procedure routine_binary_add (i_kont)
  234.   local y
  235.   repeat {
  236.     y := pop (stack)
  237.     stack[1] := stack[1] + y
  238.     @continuations[i_kont]
  239.   }
  240. end
  241.  
  242. procedure routine_binary_sub (i_kont)
  243.   local y
  244.   repeat {
  245.     y := pop (stack)
  246.     stack[1] := stack[1] - y
  247.     @continuations[i_kont]
  248.   }
  249. end
  250.  
  251. procedure routine_binary_mul (i_kont)
  252.   local y
  253.   repeat {
  254.     y := pop (stack)
  255.     stack[1] := stack[1] * y
  256.     @continuations[i_kont]
  257.   }
  258. end
  259.  
  260. procedure routine_binary_div (i_kont)
  261.   local y
  262.   repeat {
  263.     y := pop (stack)
  264.     stack[1] := stack[1] / y
  265.     @continuations[i_kont]
  266.   }
  267. end
  268.  
  269. procedure routine_binary_mod (i_kont)
  270.   local y
  271.   repeat {
  272.     y := pop (stack)
  273.     stack[1] := stack[1] % y
  274.     @continuations[i_kont]
  275.   }
  276. end
  277.  
  278. procedure routine_binary (operation, i_kont)
  279.   case operation of
  280.   {
  281.     "binary_and" : routine_binary_and (i_kont)
  282.     "binary_or" : routine_binary_or (i_kont)
  283.     "binary_lt" : routine_binary_lt (i_kont)
  284.     "binary_le" : routine_binary_le (i_kont)
  285.     "binary_gt" : routine_binary_gt (i_kont)
  286.     "binary_ge" : routine_binary_ge (i_kont)
  287.     "binary_eq" : routine_binary_eq (i_kont)
  288.     "binary_ne" : routine_binary_ne (i_kont)
  289.     "binary_add" : routine_binary_add (i_kont)
  290.     "binary_sub" : routine_binary_sub (i_kont)
  291.     "binary_mul" : routine_binary_mul (i_kont)
  292.     "binary_div" : routine_binary_div (i_kont)
  293.     "binary_mod" : routine_binary_mod (i_kont)
  294.     default : fail
  295.   }
  296. end
  297.  
  298. procedure routine_jmp (i_kont)
  299.   repeat @continuations[i_kont]
  300. end
  301.  
  302. procedure routine_jz (i_kont_if, i_kont_else)
  303.   repeat
  304.   {
  305.     if pop (stack) = 0 then
  306.       @continuations[i_kont_if]
  307.     else
  308.       @continuations[i_kont_else]
  309.   }
  310. end
  311.  
  312. procedure routine_push (pushval, i_kont)
  313.   repeat { push (stack, pushval); @continuations[i_kont] }
  314. end
  315.  
  316. procedure routine_store (i_var, i_kont)
  317.   repeat { variables[i_var] := pop (stack); @continuations[i_kont] }
  318. end
  319.  
  320. procedure routine_fetch (i_var, i_kont)
  321.   repeat { push (stack, variables[i_var]); @continuations[i_kont] }
  322. end
  323.  
  324. procedure routine_prtc (i_kont)
  325.   repeat { writes (char (pop (stack))); @continuations[i_kont] }
  326. end
  327.  
  328. procedure routine_prti (i_kont)
  329.   repeat { writes (pop (stack)); @continuations[i_kont] }
  330. end
  331.  
  332. procedure routine_prts (i_kont)
  333.   repeat { writes (strings[pop (stack) + 1]); @continuations[i_kont] }
  334. end
  335.  
  336. procedure routine_halt ()
  337.   @&main
  338. end
  339.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement