Closed MEMESCOEP closed 3 years ago
I am trying to get input as a string, but when I use input(), the return statement never works, and doesn't assign the string to p.NAME.
Code:
``from sly import Lexer from sly import Parser import sys import os import ctypes
class BasicLexer(Lexer): tokens = { NAME, NUMBER, STRING, PROMPT_FLIP, PRINT } ignore = '\t ' literals = { '=', '+', '-', '/', '*', '(', ')', ',', ';', '[', ']', '\'', '\"'}
# Define tokens as regular expressions # (stored as raw strings) NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' STRING = r'\".*?\"' NAME['prompt'] = PROMPT_FLIP NAME['print'] = PRINT # Number token @_(r'\d+') def NUMBER(self, t): # convert it into a python integer t.value = int(t.value) return t # Comment token @_(r'\\.*') def COMMENT(self, t): pass # Newline token(used only for showing # errors in new line) @_(r'\n+') def newline(self, t): self.lineno = t.value.count('\n')
class BasicParser(Parser): def GetReturn(self, p): return('var', p.NAME)
tokens = BasicLexer.tokens precedence = ( ('left', '+', '-'), ('left', '*', '/'), ('right', 'UMINUS'), ) def __init__(self, variables: dict = None): self.env = { } self.prompt = True @_('PROMPT_FLIP STRING') def statement(self, p): text0 = p.STRING text0 = text0.strip('\"') if parser.prompt: text = input(text0) print(text) return text @_('PRINT STRING') def statement(self, p): text = p.STRING text = text.strip('\"') print(text) @_('PRINT NUMBER') def statement(self, p): text = p.NUMBER #print(text) return('var', p.NUMBER) @_('PRINT NAME') def statement(self, p): #text = #print('var %s' % p.NAME) print(var) #print(text) @_('') def statement(self, p): pass @_('var_assign') def statement(self, p): return p.var_assign @_('NAME "=" expr') def var_assign(self, p): return ('var_assign', p.NAME, p.expr) @_('NAME "=" PROMPT_FLIP') def var_assign(self, p): #text0 = p.STRING #text0 = text0.strip('"') #A=parser.prompt #text = input() #print(text) #if parser.prompt: #print(text) #print("You Typed: ", text) #a = parser.prompt a = input("> ") return ('var_assign', p.NAME, a) @_('NAME "=" STRING') def var_assign(self, p): text0 = p.STRING text = text0.strip('\"') return ('var_assign', p.NAME, p.STRING) #return ('var_assign', p.NAME, text) @_('expr') def statement(self, p): return (p.expr) @_('expr "+" expr') def expr(self, p): return ('add', p.expr0, p.expr1) @_('expr "-" expr') def expr(self, p): return ('sub', p.expr0, p.expr1) @_('expr "*" expr') def expr(self, p): return ('mul', p.expr0, p.expr1) @_('expr "/" expr') def expr(self, p): return ('div', p.expr0, p.expr1) @_('"-" expr %prec UMINUS') def expr(self, p): return p.expr @_('NAME') def expr(self, p): return ('var', p.NAME) @_('NUMBER') def expr(self, p): return ('num', p.NUMBER)
class BasicExecute:
def __init__(self, tree, env): self.env = env result = self.walkTree(tree) if result is not None and isinstance(result, int): print(result) if isinstance(result, str) and result[0] == '"': print(result) def walkTree(self, node): if isinstance(node, int): return node if isinstance(node, str): return node if node is None: return None if node[0] == 'program': if node[1] == None: self.walkTree(node[2]) else: self.walkTree(node[1]) self.walkTree(node[2]) if node[0] == 'num': return node[1] if node[0] == 'str': return node[1] if node[0] == 'add': return self.walkTree(node[1]) + self.walkTree(node[2]) elif node[0] == 'sub': return self.walkTree(node[1]) - self.walkTree(node[2]) elif node[0] == 'mul': return self.walkTree(node[1]) * self.walkTree(node[2]) elif node[0] == 'div': return self.walkTree(node[1]) / self.walkTree(node[2]) if node[0] == 'var_assign': self.env[node[1]] = self.walkTree(node[2]) return node[1] if node[0] == 'var': try: return self.env[node[1]] except LookupError: print("Undefined variable '"+node[1]) return 0
lexer = BasicLexer() parser = BasicParser() env = {}
def CompileToEXE(fileDEETA):
os.system("copy Compile.py Compiled.py") f = open("Compiled.py", "w") f.truncate(0) f.write("def CompileToEXE(fileDEETA):") f.write(" " + fileDEETA) f.close()
if (sys.argv[2] == "-cr"): print("Compiling and Running...") f = open(sys.argv[1], "r")
Lines = f.readlines() count = 0 linelmao=0 # Strips the newline character for line in Lines: count += 1 linelmao = line.strip() text = linelmao #print(text) tree = parser.parse(lexer.tokenize(text)) #print(tree) BasicExecute(tree, env) #print("Line{}: {}".format(count, linelmao.strip())) #if parser.prompt: # text = input('calc > ') #else: # text = input('')
else: text = "" print("Compiling...") f = open(sys.argv[1], "r") inf = f.read()
CompileToEXE(inf)
if name == 'main':
"""lexer = BasicLexer() parser = BasicParser() print('AUSL Command Line') env = {} #print(text) while True: #print(text) #text = linelmao #print(text) #text = input('AUSL > ') try: text = inf text = input('AUSL > ') except EOFError: break if text: tree = parser.parse(lexer.tokenize(text)) BasicExecute(tree, env)"""
This is a lot of stuff to go through. Please create a simplified example. Also ensure you are using 3 ` marks with a python modifier to ensure proper syntax highlighting.
`
python
e.g.
```python print("hello")
I am trying to get input as a string, but when I use input(), the return statement never works, and doesn't assign the string to p.NAME.
Code:
``from sly import Lexer from sly import Parser import sys import os import ctypes
class BasicLexer(Lexer): tokens = { NAME, NUMBER, STRING, PROMPT_FLIP, PRINT } ignore = '\t ' literals = { '=', '+', '-', '/', '*', '(', ')', ',', ';', '[', ']', '\'', '\"'}
class BasicParser(Parser): def GetReturn(self, p): return('var', p.NAME)
tokens are passed from lexer to parser
class BasicExecute:
lexer = BasicLexer() parser = BasicParser() env = {}
def CompileToEXE(fileDEETA):
print("Compiling...")
if (sys.argv[2] == "-cr"): print("Compiling and Running...") f = open(sys.argv[1], "r")
inf = f.read()
else: text = "" print("Compiling...") f = open(sys.argv[1], "r") inf = f.read()
if name == 'main':