SHARE
TWEET

Compiler

a guest Nov 5th, 2018 132 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. io = require("stdio")
  2. print = console.log
  3. gen_num = 1
  4. gen = ->
  5.   v = Math.round(Math.random()*5000)
  6.   v = gen_num + 1
  7.   gen_num = v
  8.   v
  9.  
  10. p =
  11.   acc: " "
  12.   stack: []
  13.   cob: (a,b) ->
  14.     print("\tCOB #{a} #{b}")
  15.     @
  16.  
  17.   if: (a, state) ->
  18.     print("\tST#{state}  #{a}")
  19.     @
  20.   if_do: (a, state, run) ->
  21.     print("\tST#{state}  #{a}")
  22.     id = "if_over_#{gen()}"
  23.     @jmp("L", id) if state=="H"
  24.     @jmp("H", id) if state=="L"
  25.     run() if run?
  26.     print "#{id}:"
  27.     @
  28.  
  29.   acc_set: (state) ->
  30.     print("\tACC #{state}")
  31.     @
  32.   com: (a, force=false) ->
  33.     @acc_set("H") if force
  34.     print "\tcom #{a}"
  35.     @
  36.   set: (a, state, force=false) ->
  37.     print("\tACC #{state}") if @acc != state or force
  38.     @acc = state
  39.     print("\tOUT #{a}")
  40.     @
  41.  
  42.   jmp: (state, label) ->
  43.     print("\tJR #{state} #{label}")
  44.     @
  45.  
  46.   jmp_if: (a, state, label) ->
  47.     print("\tSTH #{a}")
  48.     print("\tJR #{state} #{label}")
  49.     @
  50.  
  51.   label: (name) ->
  52.     print("#{name}:")
  53.     @
  54.  
  55.   while: (a, state, run) ->
  56.     id = gen()
  57.     begin = "while_#{id}"
  58.     end = "while_end_#{id}"
  59.     print("#{begin}:")
  60.     @stack.push([begin, end])
  61.     @jmp_if(a,state,end)
  62.     run() if run?
  63.     @stack.pop()
  64.     @acc_set("H")
  65.     @jmp("H", begin)
  66.     print("#{end}:")
  67.     @
  68.   break: ->
  69.     @acc_set("H")
  70.     @jmp("H", @stack[@stack.length-1][1])
  71.     @
  72.  
  73.   start_timer: (name, secs) ->
  74.     print "\tLD #{name} #{secs*100}"
  75.     @
  76.  
  77.   end: ->
  78.     print "ECOB"
  79. print("-----------------------------------")
  80.  
  81. p.cob(0,0).
  82.   if_do("button", "H", -> p.
  83.     start_timer("timer", 1).
  84.     while("timer", "H", -> p.
  85.       jmp_if("button", "L", "end")
  86.       ).
  87.     com("output")
  88.     ).
  89.   label("end").
  90. end()
  91.  
  92. print("-----------------------------------")
  93.  
  94. Times=
  95.   wait_on_green: 1
  96.   wait_on_yellow: 2
  97.   wait_on_red: 2
  98.   go_on_green: 3
  99.   run_on_green: 1
  100.   back_to_red: 1
  101.   back_to_yellow: 1
  102.  
  103.   blink: 0.2
  104.  
  105.  
  106.  
  107. # p.cob(0, 0).
  108. # set("car_red", "L").
  109. # set("car_yellow", "L").
  110. # set("ped_green", "L").
  111. # set("ped_red", "H").
  112. # set("car_red", "H").
  113. # while("button", "L", -> p.
  114. #   set("gp", "L")
  115. # ).
  116. #
  117. # start_timer("timer", Times.wait_on_green).
  118. # while("time_wait", "H").
  119. #
  120. # start_timer("timer", Times.wait_on_yellow).
  121. # set("car_red", "L", true).
  122. # set("car_yellow", "H").
  123. # while("time_wait", "H").
  124. #
  125. # start_timer("timer", Times.wait_on_red).
  126. # set("car_yellow", "L", true).
  127. # set("car_red", "H").
  128. # while("time_wait", "H").
  129. #
  130. # start_timer("timer", Times.go_on_green).
  131. # set("ped_red", "L", true).
  132. # set("ped_green", "H").
  133. # while("time_wait", "H").
  134. #
  135. # start_timer("timer", Times.run_on_green).
  136. # start_timer("blink", Times.blink).
  137. # while("time_wait", "H", -> p.
  138. #   if_do("blink", "L", -> p.
  139. #     start_timer("blink", Times.blink).
  140. #   com("ped_green")
  141. #   )
  142. # ).
  143. # start_timer("timer", Times.back_to_red).
  144. # set("ped_red", "H", true).
  145. # set("ped_green", "L").
  146. # while("time_wait", "H").
  147. #
  148. # start_timer("timer", Times.back_to_yellow).
  149. # set("car_yellow", "H", true).
  150. # while("time_wait", "H").
  151. #
  152. # start_timer("timer", Times.back_to_green).
  153. # set("ped_red", "L", true).
  154. # set("ped_yellow", "L").
  155. # set("ped_green", "H").
  156. # while("time_wait", "H").
  157. #
  158. # end()
  159. #
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top