2019-12-28 00:14:53 +00:00
|
|
|
# A Simple Parser For Ekg
|
|
|
|
|
|
|
|
import TokenLib
|
|
|
|
from TokenLib import Token
|
|
|
|
|
|
|
|
|
|
|
|
class Parser():
|
|
|
|
def __init__(self, source: str):
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
def parse(self, optional_source: str = "") -> [TokenLib.Token]:
|
|
|
|
|
|
|
|
# Assumes that the source is already balanced
|
|
|
|
|
|
|
|
# Variables used for determining if a block is being parsed/
|
|
|
|
# if an escape sequence is present
|
|
|
|
|
|
|
|
escaped = False
|
|
|
|
block_level = 0
|
|
|
|
temp_block = ""
|
|
|
|
token_list = []
|
2020-01-04 08:18:18 +00:00
|
|
|
string_mode = False
|
|
|
|
temp_string = ""
|
|
|
|
integer_mode = False
|
|
|
|
temp_integer = ""
|
2019-12-28 00:14:53 +00:00
|
|
|
|
|
|
|
if optional_source:
|
|
|
|
code = optional_source
|
|
|
|
else:
|
|
|
|
code = self.source
|
|
|
|
|
|
|
|
for char in code:
|
2020-01-04 08:18:18 +00:00
|
|
|
#print(char, temp_integer, [str(x) for x in token_list])
|
|
|
|
if integer_mode:
|
|
|
|
if char not in "0123456789" and char != ".":
|
|
|
|
integer_mode = False
|
|
|
|
if block_level:
|
|
|
|
temp_block += temp_integer
|
|
|
|
else:
|
|
|
|
token_list.append(Token(TokenLib.NUMBER,
|
|
|
|
eval(temp_integer)))
|
|
|
|
|
|
|
|
|
|
|
|
temp_integer = ""
|
|
|
|
|
|
|
|
elif char == ".":
|
|
|
|
if "." in temp_integer:
|
|
|
|
raise SyntaxError("Misplaced '.'")
|
|
|
|
temp_integer += "."
|
|
|
|
|
|
|
|
else:
|
|
|
|
temp_integer += char
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif string_mode:
|
|
|
|
if escaped:
|
|
|
|
escaped = False
|
|
|
|
temp_string += char
|
|
|
|
else:
|
|
|
|
if char == '"':
|
|
|
|
string_mode = False
|
|
|
|
if block_level:
|
|
|
|
temp_block += '"' + temp_string + '"'
|
|
|
|
else:
|
|
|
|
token_list.append(Token(TokenLib.STRING,
|
|
|
|
temp_string))
|
|
|
|
|
|
|
|
temp_string = ""
|
|
|
|
|
|
|
|
elif char == "\\":
|
|
|
|
escaped = True
|
|
|
|
temp_string += char
|
|
|
|
|
|
|
|
else:
|
|
|
|
temp_string += char
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif escaped:
|
2019-12-28 00:14:53 +00:00
|
|
|
self.token_list.append(Token(TokenLib.ESCAPE, char))
|
|
|
|
escaped = False
|
2020-01-04 08:18:18 +00:00
|
|
|
continue
|
2019-12-28 00:14:53 +00:00
|
|
|
|
|
|
|
elif char == "\\":
|
|
|
|
escaped = True
|
2020-01-04 08:18:18 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
elif char in "0123456789":
|
|
|
|
temp_integer += char
|
|
|
|
integer_mode = True
|
|
|
|
continue
|
2019-12-28 00:14:53 +00:00
|
|
|
|
2020-01-04 08:18:18 +00:00
|
|
|
elif char == '"':
|
|
|
|
string_mode = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
if char == "[":
|
2019-12-28 00:14:53 +00:00
|
|
|
if block_level >= 1:
|
|
|
|
block_level += 1
|
|
|
|
temp_block += char
|
|
|
|
|
|
|
|
else:
|
|
|
|
block_level = 1
|
|
|
|
|
|
|
|
elif block_level:
|
|
|
|
if char == "]":
|
|
|
|
if block_level == 1:
|
|
|
|
token_list.append(Token(TokenLib.BLOCK,
|
|
|
|
self.parse(temp_block)))
|
|
|
|
block_level = 0
|
|
|
|
temp_block = ""
|
|
|
|
|
|
|
|
else:
|
|
|
|
temp_block += char
|
|
|
|
block_level -= 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
temp_block += char
|
|
|
|
|
|
|
|
else:
|
2020-01-04 08:18:18 +00:00
|
|
|
if char == " ":
|
|
|
|
continue
|
2019-12-28 00:14:53 +00:00
|
|
|
token_list.append(Token(TokenLib.INSTRUCTION,
|
|
|
|
char))
|
|
|
|
|
|
|
|
if temp_block:
|
|
|
|
token_list.append(Token(TokenLib.BLOCK,
|
|
|
|
temp_block))
|
|
|
|
|
2020-01-04 08:18:18 +00:00
|
|
|
elif temp_integer:
|
|
|
|
token_list.append(Token(TokenLib.NUMBER,
|
|
|
|
eval(temp_integer)))
|
|
|
|
|
|
|
|
elif temp_string:
|
|
|
|
token_list.append(Token(TokenLib.STRING,
|
|
|
|
temp_string))
|
|
|
|
|
|
|
|
|
2019-12-28 00:14:53 +00:00
|
|
|
return token_list
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-01-04 08:18:18 +00:00
|
|
|
source: str = "384+89"
|
2019-12-28 00:14:53 +00:00
|
|
|
parser: Parser = Parser(source)
|
|
|
|
tokens: [Token] = parser.parse()
|
|
|
|
|
|
|
|
for token in tokens:
|
|
|
|
print(token.get_data())
|