We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

User:InLuaIKnow/luacm2

From Esolang
Jump to navigation Jump to search

Did you know that nasal demons (demons flying out of your nose) is actually real and it happened to a person once?

#!/usr/bin/env lua

do
    local sep = package.config:sub(1,1)
    if sep == '\\' then
        os.execute("chcp 65001 > nul")
    end
end
local stck={}
local v=0
local store = 0
local pc = 1
local debt = 0
local function pu()stck[#stck+1]=v end
local function po()if #stck<1 then
    error("stackunderflow")
else store=stck[#stck] stck[#stck]=nil end end
local inst = {
    ["㎠"]=pu,
    ["㎟"]=po;

    ["㏥"]=function()v=v+1 end;
    ["㏦"]=function()v=v-1 end;
    ["㎭"]=function()io.write(string.char(store)) end;
    ["㎮"]=function()v=io.read(1):byte() end;
    ["㏂"]=function(bcode)if store==0 then local d=0 repeat pc=pc+1 if bcode[pc]=="㏂" then d=d+1 elseif bcode[pc]=="㏘" then if d==0 then break end d=d-1 end until pc>#bcode if pc==#bcode then error("unclosed ㏂") end end end;
    ["㏘"]=function(bcode)if store~=0 then local d=0 repeat pc=pc-1 if bcode[pc]=="㏘" then d=d+1 elseif bcode[pc]=="㏂" then if d==0 then break end d=d-1 end until pc<=1 end if pc==0 then error("unclosed ㏘") end  end;
    ["㏌"]=function()if debt > 5 then return end debt=debt+1 local v2=v;v=store;store=v2;end;
    ["ඞ"]=1
}        
local gen = {
    ["㎠"]=function()local code = "st[#st+1]=v;d=mx(0,d-1)\n" return code end;
    ["㎟"]=function()local code = "s=st[#st];st[#st]=nil;d=mx(0,d-1)\n" return code end;
    ["㏥"]=function(bcode,pos)
        if bcode[pos-1]=="㏥" then return "" end
        local n = 0
        for i=pos,#bcode do
            if bcode[i]=="㏥" then n=n+1 else break end
        end
        local code = "v=v+"..n.."\n" 
        return code
    end;
    ["㏦"]=function(bcode,pos)
        if bcode[pos-1]=="㏦" then return "" end
        local n = 0
        for i=pos,#bcode do
            if bcode[i]=="㏦" then n=n+1 else break end
        end
        local code = "v=v-"..n.."\n" 
        return code
    end;
    ["㎭"]=function()local code = "wr(ch(s))\n" return code end;
    ["㎮"]=function()local code = "v=rd(1):byte()\n" return code end;
    ["㏂"]=function()local code = "while s~=0 do\n" return code end;
    ["㏘"]=function()local code = "end\n" return code end;
    ["㏌"]=function()local code = "if d<=5 then do local s2=s;s=v;v=s2;d=d+1;end;end\n" return code end;
    ["ඞ"]=function()
        return "return nil"
    end
}
function compile(str)
    local c = {}
    for _,cp in utf8.codes(str) do
        local s = utf8.char(cp)
        if inst[s] then c[#c+1]=s end
    end
    return c
end

function interpret(c)
    local terminate = false
    while not terminate do
        local s = c[pc]
        if s == "ඞ" then terminate = true else
            inst[s](c)
            if s == "㎠" or s=="㎟" then
                debt=math.max(0, debt-1)
            end
            pc=pc+1
        end
    end
end

if arg[1] and arg[2]~="-g" then
    local f = io.open(arg[1],"r")
    local c = compile(f:read("*a"))
    interpret(c)
    f:close()
elseif arg[2]=="-g" then
    local f = io.open(arg[1],"r")
    local c = compile(f:read("*a"))
    f:close()
    local ft = io.open(((arg[1])..".lua"),"wb")
    local code = {}
    for i,s in ipairs(c) do
        code[#code+1]=gen[s](c,i)
    end
    ft:write("#!/usr/bin/env lua\nlocal v=0;local s=0;local st={};local d=0;local mx=math.max;local ch=string.char;local wr=io.write;local rd=io.read\n"..table.concat(code))
    ft:close()

else
    print("㎠, the esoteric programming language")
    print()
    while true do
        pc=1
        stck={}
        v=0
        store=0
        io.write(">")
        local f = io.stdin
        local c = compile(f:read())
        interpret(c)
    end
end