Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs={ ... }
- term.setCursorPos(1, 1)
- term.clear()
- print("BLML [ BasicLuaMadeLanguage ]\n")
- symbols={}
- functions = {}
- local __THIS={}
- __THIS.TERM=false
- local function openFile(file)
- local f=fs.open(file, "r")
- local __str=f.readAll()
- f.close()
- return __str
- end
- local function append(tokens)
- local __str="[ "
- for n, v in pairs(tokens) do
- __str=__str..n.." = "..v..", "
- end
- __str=__str.."]"
- return __str
- end
- local function doASSIGN(varName, varValue)
- local __varNm=varName:sub(5)
- if __varNm:len()>=2 then
- symbols[__varNm]=varValue
- else
- error("Cant define the variable.")
- __THIS.TERM=true
- end
- end
- local function getVARIABLE(varName)
- local __VAR=varName:sub(5)
- if symbols[__VAR] and __VAR:len()>=1 then
- return symbols[varName:sub(5)]
- else
- __THIS.TERM=true
- error("Cant print BLANK/NOT ASSIGNED variables.")
- end
- end
- local function getINPUT(str, varName)
- local __str=str:sub(2, str:len()-1)
- write(__str)
- local inp=read()
- symbols[varName]=inp
- end
- local function parse(tokens)
- local i=1
- local __parseBelowCount=1
- while i <= #tokens and __THIS.TERM==false do
- if __parseBelowCount==1 then
- if tokens[i]=="END" then
- i=i+1
- 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
- if tokens[i+1]:sub(1, 6) == "STRING" then
- print(tokens[i+1]:sub(9, tokens[i+1]:len()-1))
- elseif tokens[i+1]:sub(1, 3) == "NUM" then
- print(tokens[i+1]:sub(5))
- elseif tokens[i+1]:sub(1, 4) == "EXPR" then
- print(loadstring("return "..tokens[i+1]:sub(6))())
- elseif tokens[i+1]:sub(1, 3) == "VAR" then
- print(getVARIABLE(tokens[i+1]))
- end
- i=i+2
- 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
- if tokens[i+2]:sub(1, 6) == "STRING" then
- doASSIGN(tokens[i], tokens[i+2]:sub(9, tokens[i+2]:len()-1))
- elseif tokens[i+2]:sub(1, 3) == "NUM" then
- doASSIGN(tokens[i], tokens[i+2]:sub(5))
- elseif tokens[i+2]:sub(1, 4) == "EXPR" then
- local __retStr=tokens[i+2]:sub(6)
- doASSIGN(tokens[i], loadstring("return "..__retStr)())
- elseif tokens[i+2]:sub(1, 3) == "VAR" then
- doASSIGN(tokens[i], getVARIABLE(tokens[i+2]))
- end
- i=i+3
- elseif tokens[i]..tokens[i+1]:sub(1, 6)..tokens[i+2]:sub(1, 3)=="INPUTSTRINGVAR" then
- getINPUT(tokens[i+1]:sub(8), tokens[i+2]:sub(5))
- i=i+3
- elseif tokens[i]..tokens[i+1]:sub(1, 3)..tokens[i+2]..tokens[i+3]:sub(1, 3)..tokens[i+4]=="IFNUMEQEQNUMTHEN" then
- if not (tokens[i+1]:sub(5) == tokens[i+3]:sub(5)) then
- __parseBelowCount=__parseBelowCount+1
- end
- i=i+5
- elseif tokens[i]..tokens[i+1]:sub(1, 3)..tokens[i+2]..tokens[i+3]:sub(1, 3)..tokens[i+4]=="IFVAREQEQNUMTHEN" then
- if not (getVARIABLE(tokens[i+1]) == tokens[i+3]:sub(5)) then
- __parseBelowCount=__parseBelowCount+1
- end
- i=i+5
- end
- elseif __parseBelowCount>1 then
- if tokens[i] == "IF" then
- __parseBelowCount=__parseBelowCount+1
- end
- if tokens[i] == "END" then
- i=i+1
- __parseBelowCount=__parseBelowCount-1
- else
- i=i+1
- end
- end
- end
- print(append(symbols))
- end
- local function lex(filecontents)
- local tok=""
- local state=0
- local varStarted=0
- local expr=""
- local var=""
- local __str=""
- local isexpr=0
- local tokens={}
- local n = ""
- for i=1, #filecontents do
- char = filecontents:sub(i, i)
- tok = tok..char
- if tok==" " then
- if state==0 then
- tok=""
- else
- tok=" "
- end
- elseif tok=="\n" or tok=="<EOF>" then
- if #expr>0 and isexpr==1 then
- tokens[#tokens+1]="EXPR:"..expr
- expr=""
- isexpr=0
- elseif #expr>0 and isexpr==0 then
- tokens[#tokens+1]="NUM:"..expr
- expr=""
- elseif #var>0 then
- tokens[#tokens+1]="VAR:"..var
- var = ""
- varStarted=0
- end
- tok=""
- elseif tok=="=" and state==0 then
- if #expr>0 and isexpr==0 then
- tokens[#tokens+1]="NUM:"..expr
- expr=""
- end
- if #var>0 then
- tokens[#tokens+1]="VAR:"..var
- var = ""
- varStarted=0
- end
- if tokens[#tokens] == "EQUALS" then
- tokens[#tokens]="EQEQ"
- else
- tokens[#tokens+1]="EQUALS"
- end
- tok=""
- elseif tok=="$" and state==0 then
- varStarted=1
- var = var..tok
- tok=""
- elseif varStarted==1 then
- if tok=="<" or tok==">" then
- if #var>0 then
- tokens[#tokens+1]="VAR:"..var
- var = ""
- varStarted=0
- end
- end
- var=var..tok
- tok=""
- elseif tok=="print" or tok=="PRINT" then
- tokens[#tokens+1]="PRINT"
- tok=""
- elseif tok=="INPUT" or tok=="input" then
- tokens[#tokens+1]="INPUT"
- tok=""
- elseif tok=="THEN" or tok=="then" then
- if #expr>0 and isexpr==0 then
- tokens[#tokens+1]="NUM:"..expr
- expr=""
- end
- if #var>0 then
- tokens[#tokens+1]="VAR:"..var
- var = ""
- varStarted=0
- end
- tokens[#tokens+1]="THEN"
- tok=""
- elseif tok=="STOPIF" or tok=="stopif" then
- tokens[#tokens+1]="END"
- tok=""
- elseif tok=="IF" or tok=="if" then
- tokens[#tokens+1]="IF"
- tok=""
- 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
- expr=expr..tok
- tok=""
- elseif tok=="+" or tok=="-" or tok=="/" or tok=="*" or tok=="(" or tok==")" or tok=="%" then
- isexpr=1
- expr=expr..tok
- tok=""
- elseif tok=="\t" then
- tok=""
- elseif tok=="\"" or tok==" \"" then
- if state==0 then
- state=1
- elseif state==1 then
- tokens[#tokens+1]="STRING:"..__str.."\""
- __str=""
- state=0
- tok=""
- end
- elseif state==1 then
- __str=__str..tok
- tok=""
- end
- end
- --print(append(tokens))
- return tokens
- --return ""
- end
- local function main()
- local content=openFile(tArgs[1])
- content=content.."<EOF>"
- local toks=lex(content)
- parse(toks)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement