Skip to content

Instantly share code, notes, and snippets.

@axeII
Last active September 7, 2017 18:08
Show Gist options
  • Save axeII/a5ac310c03fc0467aca95365cf31bba5 to your computer and use it in GitHub Desktop.
Save axeII/a5ac310c03fc0467aca95365cf31bba5 to your computer and use it in GitHub Desktop.
lexer.py
import re
from enum import Enum
class Lexer_state(Enum):
s = 1
word = 2
real = 3
number = 4
string = 5
class Lexer:
def __init__(self):
self.identify = re.compile("[a-zA-Z0-9_\.-]+")
def lexer(self,text):
state = Lexer_state.s
token = ""
while text != "":
if state == Lexer_state.s:
if text[0] in (' ', '\t', '\n'):
text = text[1:]
elif text[0] == '"':
text = text[1:]
state = Lexer_state.string
elif text[0].isdigit():
token += text[0]
text = text[1:]
state = Lexer_state.number
elif self.identify.match(text[0]):
state = Lexer_state.word
token += text[0]
text = text[1:]
else:
print("[Error]Not valid charatect")
break
elif state == Lexer_state.string:
if self.identify.match(text[0]):
token += text[0]
text = text[1:]
elif text[0] == '"':
text = text[1:]
yield token
token = ""
state = Lexer_state.s
else:
break
elif state == Lexer_state.number:
if text[0].isdigit():
print(text[0])
token += text[0]
text = text[1:]
elif text[0] == '.':
token += text[0]
text = text[1:]
state = Lexer_state.real
else:
state = Lexer_state.s
yield int(token)
token = ""
elif state == Lexer_state.real:
if text[0].isdigit():
token += text[0]
text = text[1:]
else:
yield float(token)
token = ""
elif state == Lexer_state.word:
if self.identify.match(text[0]):
token += text[0]
text = text[1:]
else:
state = Lexer_state.s
token = ""
yield token
def evaluate(self):
def operation(stack,op):
a = stack.pop()
b = stack.pop()
stack.append(op(a,b))
def performBinaryOp(op):
def perform(stack):
a = stack.pop()
b = stack.pop()
stack.append(op(a, b))
return perform
stack = []
wordList = {
"PRINT": lambda x: print(x),
"DUP": lambda x: x.append(x[-1]),
"+":performBinaryOp(lambda x,y: x + y),
"/": [operation, lambda x,y: x // y],
"-": [operation, lambda x,z: x - y],
#"slovo": []
}
while True:
for token in self.lexer(input('>')):
print(token)
try:
if isinstance(token, str):
wordList[token](stack)
elif isinstance(token, int):
stack.append(token)
except KeyError:
pass
if __name__ == "__main__":
l = Lexer()
l.evaluate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment