But When I make an if statement it goes like:
:strvar = "Hello, world."
if 1 == 1 then
print :strvar
stopif
And the if statement == true then run code inside it else dont.
:strvar = "Hello, world."
if 1 == 1 then
print :strvar
stopif
And the if statement == true then run code inside it else dont.
tokens[i]..tokens[i+1]:sub(1, 3)..tokens[i+2]..tokens[i+3]:sub(1, 3)..tokens[i+4]=="IFNUMEQEQNUMTHEN" then
if tokens[i+1]:sub(5) == tokens[i+3]:sub(5) then
print("IF SATEMENT EQUALS TRUE")
else
print("IF SATEMENT EQUALS FALSE")
end
[ 1 = VAR::test, 2 = EQUALS, 3 = STRING:"Cool dude.", 4 = IF, 5 = NUM:1, 6 = EQEQ, 7 = NUM:1, 8 = THEN, 9 = PRINT, 10 = VAR::test, 11 = END, 12 = PRINT, 13 = STRING:"Cool dude.", 14 = PRINT, 15 = EXPR:2*5+5, 16 = PRINT, 17 = NUM:15, ]
tArgs={ ... }
term.setCursorPos(1, 1)
term.clear()
print("BLML [ BasicLuaMadeLanguage ]\n")
symbols={}
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)
symbols[__varNm]=varValue
end
local function getVARIABLE(varName)
if symbols[varName:sub(5)] then
return symbols[varName:sub(5)]
else
return "Undefined variable: "..varName:sub(5)
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
while i <= #tokens do
local __secTok=tokens[i+1]
if tokens[i]=="END" then
i=i+1
elseif tokens[i]..__secTok:sub(1,6)=="PRINTSTRING" or tokens[i]..__secTok:sub(1,3)=="PRINTNUM" or tokens[i]..__secTok:sub(1,4)=="PRINTEXPR" or tokens[i]..__secTok: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 tokens[i+1]:sub(5) == tokens[i+3]:sub(5) then
print("IF SATEMENT EQUALS TRUE")
else
print("IF SATEMENT EQUALS FALSE")
end
i=i+5
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=="END" or tok=="end" 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
failas=fs.open("append", "w")
failas.write(append(tokens))
failas.close()
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()
Oh I got it guys, I just needed to make a boolean to check if parse the code below. Thanks for the comments and trying to help me fix it :)/>/>
local i=1
local __parseBelowCount=1
while i <= #tokens and __THIS.TERM==false do
local __secTok=tokens[i+1]
if __parseBelowCount==1 then
if tokens[i]=="END" then
i=i+1
elseif tokens[i]..__secTok:sub(1,6)=="PRINTSTRING" or tokens[i]..__secTok:sub(1,3)=="PRINTNUM" or tokens[i]..__secTok:sub(1,4)=="PRINTEXPR" or tokens[i]..__secTok: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 tokens[i+1]:sub(5) == tokens[i+3]:sub(5) then
--Do nothing...
else
__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 getVARIABLE(tokens[i+1]) == tokens[i+3]:sub(5) then
--Do nothing...
else
__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