Last active
December 11, 2015 01:18
-
-
Save mniip/4522011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[=[ | |
| example: | |
| #define mv{#} tonumber(%1)>0 and(">"):rep(%1)or("<"):rep(-(%1)) | |
| #define add{#} "mv{%1}[-mv{-(%1)}+mv{%1}]mv{-(%1)}" | |
| ,>,>,<<add{2}add{1}. | |
| preprocession: | |
| ,>,>,<<mv{2}[-mv{-2}+mv{2}]mv{-2}mv{1}[-mv{-1}+mv{1}]mv{-1}. | |
| preprocession: | |
| ,>,>,<<>>[-<<+>>]<<>[-<+>]<. | |
| optimization: | |
| ,>,>,[-<<+>>]<[-<+>]<. | |
| ]=] | |
| local code=io.read"*a" | |
| local def={} | |
| for n,r in code:gmatch"#define +([^ ]*) +([^\n]*)" do | |
| n=n:gsub("#","([0-9.+*/%%^()-]+)") | |
| def[n]=r | |
| end | |
| code=code:gsub("#[^\n]*","") | |
| local i=1 | |
| while i>0 do | |
| for n,r in pairs(def) do | |
| code,i=code:gsub(n, | |
| function(...) | |
| t={...} | |
| return loadstring("return "..r:gsub("%%(%d+)", | |
| function(i) | |
| return tostring(t[tonumber(i)]) | |
| end))(...) | |
| end) | |
| if i>0 then break end | |
| end | |
| end | |
| local annih={"<>","><","%+%-","%-%+","()[+-]+(%[[+-]%])","()%[[+-]](%[[+-]])","%[[+-]%]%[%]"} | |
| table.insert(annih,"[^.,><[%]+-]+") | |
| local i=1 | |
| while i>0 do | |
| for _,v in ipairs(annih) do | |
| code,i=code:gsub(v,function(_,a)return a or""end) | |
| if i>0 then break end | |
| end | |
| end | |
| print(code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment