1 function chartonumber(char)
2 local chartable = { ["0"] = 0, ["1"] = 1, ["2"] = 2, ["3"] = 3, ["4"] = 4, ["5"] = 5,
3 ["6"] = 6, ["7"] = 7, ["8"] = 8, ["9"] = 9 }
7 function iswhitespace(char)
8 local whitespace = { [" "] = true, ["\r"] = true, ["\n"] = true, ["\t"] = true, [""] = true}
9 return whitespace[char]
12 function isidentifierchar(char)
13 if chartonumber(char) then
16 if (char:byte(1) >= ("A"):byte(1) and char:byte(1) <= ("Z"):byte(1)) or
17 (char:byte(1) >= ("a"):byte(1) and char:byte(1) <= ("z"):byte(1)) or
24 function readToken(input)
26 local keywords = {"if", "int", "(", ")", "{", "}", ";", ",", "+", "-", "*", "/", "while", "<", ">", "==", "=", "return"}
28 -- strip off whitespace from the input
29 while iswhitespace(input:sub(1,1)) and input:len() > 0 do
33 if input:len() == 0 then
37 for i,keyword in pairs(keywords) do
38 if input:sub(1,keyword:len()) == keyword then
39 input = input:sub(keyword:len() + 1)
45 -- okay, let's try to tokenize a number
46 if chartonumber(input:sub(1,1)) then
49 while chartonumber(input:sub(1,1)) do
50 token.value = token.value*10 + chartonumber(input:sub(1,1))
53 if not iswhitespace(input:sub(1,1))
54 and input:sub(1,1) ~= ")"
55 and input:sub(1,1) ~= "}"
56 and input:sub(1,1) ~= ";"
57 and input:sub(1,1) ~= "+"
58 and input:sub(1,1) ~= ","
59 and input:sub(1,1) ~= "-" then
60 error("expected one of whitespace, ), }, ;, +, - after number; got "..input:sub(1,1))
65 -- ok, let's try to tokenize an identifier now.
66 if isidentifierchar(input:sub(1,1)) then
67 token.type = "identifier"
69 while isidentifierchar(input:sub(1,1)) do
70 token.value = token.value .. input:sub(1,1)
76 error("invalid character to start token: "..input:sub(1,1).." ("..input:byte(1)..")")
79 function tokenize(input)
81 while input:len() > 0 do
83 input,token = readToken(input)
84 table.insert(tokenlist, token)