Advertisement
Necr0

MoonScript Brainfuck Interpreter

Aug 13th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. class Brainfuck
  2.     maximum_runtime: 16384
  3.    
  4.     new: (code,input,output_callback) =>
  5.         @code=code
  6.         @input=input
  7.         @output_callback=output_callback
  8.         @matrix={0}
  9.         @memory_pointer=1
  10.         @code_pointer=1
  11.    
  12.     shift_right: =>
  13.         @memory_pointer += 1
  14.         if @matrix[@memory_pointer]==nil
  15.             @matrix[@memory_pointer]=0
  16.    
  17.     shift_left: =>
  18.         @memory_pointer -= 1
  19.         if @matrix[@memory_pointer]==nil
  20.             @matrix[@memory_pointer]=0
  21.    
  22.     increment: =>
  23.         @matrix[@memory_pointer]=(@matrix[@memory_pointer]+1)%256
  24.    
  25.     decrement: =>
  26.         @matrix[@memory_pointer]=(@matrix[@memory_pointer]-1)%256
  27.    
  28.     jez_forward: =>
  29.         if @get_value! == 0
  30.             depth = 0
  31.             while true
  32.                 @move_forward!
  33.                 switch @get_current_char!
  34.                     when "["
  35.                         depth += 1
  36.                     when "]"
  37.                         if depth == 0
  38.                             return nil
  39.                         else
  40.                             depth -= 1
  41.                     when nil
  42.                         return nil
  43.    
  44.     jnz_backward: =>
  45.         if @get_value! != 0
  46.             depth = 0
  47.             while true
  48.                 @move_backward!
  49.                 switch @get_current_char!
  50.                     when "["
  51.                         if depth == 0
  52.                             return nil
  53.                         else
  54.                             depth -= 1
  55.                     when "]"
  56.                         depth += 1
  57.                     when ""
  58.                         return nil
  59.    
  60.     read_input: =>
  61.         char = string.sub @input, 1, 1
  62.         if char!=""
  63.             @set_value string.byte char
  64.         else
  65.             @set_value 0
  66.         @input = string.sub @input, 2
  67.    
  68.     move_forward: => @code_pointer+=1
  69.     move_backward: => @code_pointer-=1
  70.     get_char: (n) => string.sub @code, n, n
  71.     get_current_char: => @get_char(@code_pointer)
  72.     get_value: => @matrix[@memory_pointer]
  73.     set_value: (value) => @matrix[@memory_pointer]=value
  74.    
  75.     append_input: (input) => @input ..= input
  76.    
  77.     step: =>
  78.         ret = nil
  79.         switch @get_current_char!
  80.             when "["
  81.                 @jez_forward!
  82.             when "]"
  83.                 @jnz_backward!
  84.             when "+"
  85.                 @increment!
  86.             when "-"
  87.                 @decrement!
  88.             when "."
  89.                 ret=string.char @get_value!
  90.                 if @output_callback != nil
  91.                     @output_callback ret
  92.             when ","
  93.                 @read_input!
  94.             when ">"
  95.                 @shift_right!
  96.             when "<"
  97.                 @shift_left!
  98.             when ""
  99.                 return false
  100.         @move_forward!
  101.         ret
  102.    
  103.     run: =>
  104.         output=""
  105.         for i=1,@maximum_runtime
  106.             step=@step!
  107.             if step==false
  108.                 break
  109.             elseif step!=nil
  110.                 output ..= step
  111.         output
  112.  
  113. print Brainfuck("--[>--->->->++>-<<<<<-------]>--.>---------.>--..+++.>----.>+++++++++.<<.+++.------.<-.>>+.","")\run!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement