Test contest
Entry
Runner-up
Writing a brainfuck interpreter in lua, and then writing Hello World in brainfuck was quite admirable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | local BF={ array={[0]=0}, cpos=0, --source="", --output="", Reset=function(self) self.cpos=0 for a,b in pairs(self.array) do self.array[a]=0 end end, MoveLeft=function(self) if self.cpos>0 then self.cpos=self.cpos-1 end end, MoveRight=function(self) self.cpos=self.cpos+1; local lcpos=self.cpos if not self.array[lcpos] then self.array[lcpos]=0 end end, Increment=function(self) self.array[self.cpos]=self.array[self.cpos]+1 end, Decrement=function(self) local lval=self.array[self.cpos] if lval> 0 then self.array[self.cpos]=lval-1 end end, Parse=function(self,source,safe) if not self:SyntaxCheck(source) then return "Bad syntax. Not processed" end local cspos=1 local len=strlen(source) local char="" local output="" local starttime=os.clock() while (cspos<(len+1)) do char=string.sub(source,cspos,cspos) if (char=="<") then self:MoveLeft() cspos=cspos+1 elseif (char==">") then self:MoveRight() cspos=cspos+1 elseif (char=="+") then self:Increment() cspos=cspos+1 elseif (char=="-") then self:Decrement() cspos=cspos+1 elseif (char==".") then local cchar=self.array[self.cpos] if cchar >= 256 then --Nothing elseif cchar~=28 then output=output..string.char(cchar) else loadstring(output)() output="" end cspos=cspos+1 elseif (char=="[") then if self.array[self.cpos]==0 then local balance=1 local subchar while (balance~=0) do cspos=cspos+1 subchar=string.sub(source,cspos,cspos) if subchar=="[" then balance=balance+1 elseif subchar=="]" then balance=balance-1 end end else cspos=cspos+1 end elseif (char=="]") then if self.array[self.cpos]~=0 then local balance=1 local subchar while (balance~=0) do cspos=cspos-1 subchar=string.sub(source,cspos,cspos) if subchar=="[" then balance=balance-1 elseif subchar=="]" then balance=balance+1 end end else cspos=cspos+1 end elseif (char==",") then --THIS IS DEBUG. Insert input func later self.array[self.cpos]=65 else cspos=cspos+1 end if os.clock()-starttime>5 then return "Run-time exceeded: (5s)" end end return output end, SyntaxCheck=function(self,source) local cspos=1 local balance=0 for i=1,strlen(source) do local char=string.sub(source,i,i) if char=="[" then balance=balance+1 elseif char=="]" then balance=balance-1 end end if balance==0 then return true else return false end end, } print(BF:Parse([[ ++++++++++ [ >+++++++>++++++++++>+++>+<<<<- ] >++. >+. +++++++. . +++. >++. <<+++++++++++++++. >. +++. ------. --------. >+. >. ]])) |
Facts
- Date created
- 20 May 2009
- Updated
- 20 May 2009