Advertisement
Guest User

BLML

a guest
Aug 28th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.11 KB | None | 0 0
  1. tArgs={ ... }
  2.  
  3. term.setCursorPos(1, 1)
  4. term.clear()
  5. print("BLML [ BasicLuaMadeLanguage ]\n")
  6.  
  7. symbols={}
  8. functions = {}
  9.  
  10. local __THIS={}
  11. __THIS.TERM=false
  12.  
  13. local function openFile(file)
  14.     local f=fs.open(file, "r")
  15.     local __str=f.readAll()
  16.     f.close()
  17.     return __str
  18. end
  19.  
  20. local function append(tokens)
  21.     local __str="[ "
  22.     for n, v in pairs(tokens) do
  23.         __str=__str..n.." = "..v..", "
  24.     end
  25.     __str=__str.."]"
  26.     return __str
  27. end
  28.  
  29. local function doASSIGN(varName, varValue)
  30.     local __varNm=varName:sub(5)
  31.     if __varNm:len()>=2 then
  32.         symbols[__varNm]=varValue
  33.     else
  34.         error("Cant define the variable.")
  35.         __THIS.TERM=true
  36.     end
  37. end
  38.  
  39. local function getVARIABLE(varName)
  40.     local __VAR=varName:sub(5)
  41.     if symbols[__VAR] and __VAR:len()>=1 then
  42.         return symbols[varName:sub(5)]
  43.     else
  44.         __THIS.TERM=true
  45.         error("Cant print BLANK/NOT ASSIGNED variables.")
  46.     end
  47. end
  48.  
  49. local function getINPUT(str, varName)
  50.     local __str=str:sub(2, str:len()-1)
  51.     write(__str)
  52.     local inp=read()
  53.     symbols[varName]=inp
  54. end
  55.  
  56. local function parse(tokens)
  57.     local i=1
  58.     local __parseBelowCount=1
  59.     while i <= #tokens and __THIS.TERM==false do
  60.         if __parseBelowCount==1 then
  61.             if tokens[i]=="END" then
  62.                 i=i+1
  63.             elseif tokens[i]..tokens[i+1]:sub(1,6)=="PRINTSTRING" or tokens[i]..tokens[i+1]:sub(1,3)=="PRINTNUM" or tokens[i]..tokens[i+1]:sub(1,4)=="PRINTEXPR" or tokens[i]..tokens[i+1]:sub(1,3)=="PRINTVAR" then
  64.                 if tokens[i+1]:sub(1, 6) == "STRING" then
  65.                     print(tokens[i+1]:sub(9, tokens[i+1]:len()-1))
  66.                 elseif tokens[i+1]:sub(1, 3) == "NUM" then
  67.                     print(tokens[i+1]:sub(5))
  68.                 elseif tokens[i+1]:sub(1, 4) == "EXPR" then
  69.                     print(loadstring("return "..tokens[i+1]:sub(6))())
  70.                 elseif tokens[i+1]:sub(1, 3) == "VAR" then
  71.                     print(getVARIABLE(tokens[i+1]))
  72.                 end
  73.                 i=i+2
  74.             elseif tokens[i]:sub(1, 3)..tokens[i+1]..tokens[i+2]:sub(1, 6)=="VAREQUALSSTRING" or tokens[i]:sub(1, 3)..tokens[i+1]..tokens[i+2]:sub(1, 3)=="VAREQUALSNUM" or tokens[i]:sub(1, 3)..tokens[i+1]..tokens[i+2]:sub(1, 4)=="VAREQUALSEXPR" or tokens[i]:sub(1, 3)..tokens[i+1]..tokens[i+2]:sub(1, 3)=="VAREQUALSVAR" then
  75.                 if tokens[i+2]:sub(1, 6) == "STRING" then
  76.                     doASSIGN(tokens[i], tokens[i+2]:sub(9, tokens[i+2]:len()-1))
  77.                 elseif tokens[i+2]:sub(1, 3) == "NUM" then
  78.                     doASSIGN(tokens[i], tokens[i+2]:sub(5))
  79.                 elseif tokens[i+2]:sub(1, 4) == "EXPR" then
  80.                     local __retStr=tokens[i+2]:sub(6)
  81.                     doASSIGN(tokens[i], loadstring("return "..__retStr)())
  82.                 elseif tokens[i+2]:sub(1, 3) == "VAR" then
  83.                     doASSIGN(tokens[i], getVARIABLE(tokens[i+2]))
  84.                 end
  85.                 i=i+3
  86.             elseif tokens[i]..tokens[i+1]:sub(1, 6)..tokens[i+2]:sub(1, 3)=="INPUTSTRINGVAR" then
  87.                 getINPUT(tokens[i+1]:sub(8), tokens[i+2]:sub(5))
  88.                 i=i+3
  89.             elseif tokens[i]..tokens[i+1]:sub(1, 3)..tokens[i+2]..tokens[i+3]:sub(1, 3)..tokens[i+4]=="IFNUMEQEQNUMTHEN" then
  90.                 if not (tokens[i+1]:sub(5) == tokens[i+3]:sub(5)) then
  91.                     __parseBelowCount=__parseBelowCount+1
  92.                 end
  93.                 i=i+5
  94.             elseif tokens[i]..tokens[i+1]:sub(1, 3)..tokens[i+2]..tokens[i+3]:sub(1, 3)..tokens[i+4]=="IFVAREQEQNUMTHEN" then
  95.                 if not (getVARIABLE(tokens[i+1]) == tokens[i+3]:sub(5)) then
  96.                     __parseBelowCount=__parseBelowCount+1
  97.                 end
  98.                 i=i+5
  99.             end
  100.         elseif __parseBelowCount>1 then
  101.             if tokens[i] == "IF" then
  102.                 __parseBelowCount=__parseBelowCount+1
  103.             end
  104.             if tokens[i] == "END" then
  105.                 i=i+1
  106.                 __parseBelowCount=__parseBelowCount-1
  107.             else
  108.                 i=i+1
  109.             end
  110.         end
  111.     end
  112.     print(append(symbols))
  113. end
  114.  
  115. local function lex(filecontents)
  116.     local tok=""
  117.     local state=0
  118.     local varStarted=0
  119.     local expr=""
  120.     local var=""
  121.     local __str=""
  122.     local isexpr=0
  123.     local tokens={}
  124.     local n = ""
  125.     for i=1, #filecontents do
  126.         char = filecontents:sub(i, i)
  127.         tok = tok..char
  128.         if tok==" " then
  129.             if state==0 then
  130.                 tok=""
  131.             else
  132.                 tok=" "
  133.             end
  134.         elseif tok=="\n" or tok=="<EOF>" then
  135.             if #expr>0 and isexpr==1 then
  136.                 tokens[#tokens+1]="EXPR:"..expr
  137.                 expr=""
  138.                 isexpr=0
  139.             elseif #expr>0 and isexpr==0 then
  140.                 tokens[#tokens+1]="NUM:"..expr
  141.                 expr=""
  142.             elseif #var>0 then
  143.                 tokens[#tokens+1]="VAR:"..var
  144.                 var = ""
  145.                 varStarted=0
  146.             end
  147.             tok=""
  148.         elseif tok=="=" and state==0 then
  149.             if #expr>0 and isexpr==0 then
  150.                 tokens[#tokens+1]="NUM:"..expr
  151.                 expr=""
  152.             end
  153.             if #var>0 then
  154.                 tokens[#tokens+1]="VAR:"..var
  155.                 var = ""
  156.                 varStarted=0
  157.             end
  158.             if tokens[#tokens] == "EQUALS" then
  159.                 tokens[#tokens]="EQEQ"
  160.             else
  161.                 tokens[#tokens+1]="EQUALS"
  162.             end
  163.             tok=""
  164.         elseif tok=="$" and state==0 then
  165.             varStarted=1
  166.             var = var..tok
  167.             tok=""
  168.         elseif varStarted==1 then
  169.             if tok=="<" or tok==">" then
  170.                 if #var>0 then
  171.                     tokens[#tokens+1]="VAR:"..var
  172.                     var = ""
  173.                     varStarted=0
  174.                 end
  175.             end
  176.             var=var..tok
  177.             tok=""
  178.         elseif tok=="print" or tok=="PRINT" then
  179.             tokens[#tokens+1]="PRINT"
  180.             tok=""
  181.         elseif tok=="INPUT" or tok=="input" then
  182.             tokens[#tokens+1]="INPUT"
  183.             tok=""
  184.         elseif tok=="THEN" or tok=="then" then
  185.             if #expr>0 and isexpr==0 then
  186.                 tokens[#tokens+1]="NUM:"..expr
  187.                 expr=""
  188.             end
  189.             if #var>0 then
  190.                 tokens[#tokens+1]="VAR:"..var
  191.                 var = ""
  192.                 varStarted=0
  193.             end
  194.             tokens[#tokens+1]="THEN"
  195.             tok=""
  196.         elseif tok=="STOPIF" or tok=="stopif" then
  197.             tokens[#tokens+1]="END"
  198.             tok=""
  199.         elseif tok=="IF" or tok=="if" then
  200.             tokens[#tokens+1]="IF"
  201.             tok=""
  202.         elseif tok=="0" or tok=="1" or tok=="2" or tok=="3" or tok=="4" or tok=="5" or tok=="6" or tok=="7" or tok=="8" or tok=="9" then
  203.             expr=expr..tok
  204.             tok=""
  205.         elseif tok=="+" or tok=="-" or tok=="/" or tok=="*" or tok=="(" or tok==")" or tok=="%" then
  206.             isexpr=1
  207.             expr=expr..tok
  208.             tok=""
  209.         elseif tok=="\t" then
  210.             tok=""
  211.         elseif tok=="\"" or tok==" \"" then
  212.             if state==0 then
  213.                 state=1
  214.             elseif state==1 then
  215.                 tokens[#tokens+1]="STRING:"..__str.."\""
  216.                 __str=""
  217.                 state=0
  218.                 tok=""
  219.             end
  220.         elseif state==1 then
  221.             __str=__str..tok
  222.             tok=""
  223.         end
  224.     end
  225.     --print(append(tokens))
  226.     return tokens
  227.     --return ""
  228. end
  229.  
  230. local function main()
  231.     local content=openFile(tArgs[1])
  232.     content=content.."<EOF>"
  233.     local toks=lex(content)
  234.     parse(toks)
  235. end
  236.  
  237. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement