Snippets

marinus Brainflak interpreter in APL, ungolfed

Created by marinus last modified
brainflak{
   ⍝ set up stacks
   stks() 

   ⍝ get the first command from a string
   getfst{
      bgns'([<{'
      ends')]>}'
      cmd
      idx((+\cmd=)-+\ends[bgnscmd]=)0
      (idx), idx
   }

   ⍝ evaluate list of commands
   evallst{
      0=⍴⍵:0
      rest valeval 
      val+rest
   }

   ⍝ evaluate code
   eval{

      ⍝ nilad?
      nilad2
      rest2

      '()'nilad:(rest),1
      '[]'nilad:(rest),¯1
      '{}'nilad:(rest),{
          item⊃⊃⊃stks
          stks[1]1
          item
      }
      '<>'nilad:(rest),0stks1

      ⍝ monads: get inner command
      cmd restgetfst 
      inner1¯1cmd

      ⍝ evaluate command
      '('=⊃cmd:(rest), stks[1],evallst inner
      '['=⊃cmd:(rest), +evallst inner
      '{'=⊃cmd:(rest), {0≠⊃⊃⊃stks:∇evallst inner0}
      '<':cmd:(rest), 0evallst inner

      ⍝ error
      ('INVALID CMD: ',⊃cmd) ⎕SIGNAL 11
   }

   ⍝ evaluate all the commands in order
   _evallst 

   ⍝ print active stack
   ⍪⊃stks
}

Comments (0)