Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


class BaseError(Exception):
def __init__(self, message):
super().__init__(message)

@property
def traceback(self):
return self._traceback


class SyntaxError(BaseError):
def __init__(self, message, tokens, current):
self.tokens = tokens
self.current = current
super().__init__(message)

def __str__(self):
string = ''
current_token = self.tokens[self.current]
current_line = current_token.line

start_of_line = self.current
while start_of_line > 0 and self.tokens[start_of_line].line == current_line:
start_of_line -= 1

end_of_line = self.current
while end_of_line < len(self.tokens) and self.tokens[end_of_line].line == current_line:
end_of_line += 1


for token, index in zip(self.tokens[start_of_line:end_of_line], range(start_of_line, end_of_line)):
if index == self.current:
string += '~~ '
string += token.lexeme + ' '

if index == self.current:
string += '~~'

return string
28 changes: 16 additions & 12 deletions src/pychart/runner.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from src.scanner import Scanner
from src.pyparser import Parser
from src import errors


def run(source: str):
scanner = Scanner(source)
tokens = scanner.get_tokens()
ast = Parser(tokens).parse()
try:
scanner = Scanner(source)
tokens = scanner.get_tokens()
ast = Parser(tokens).parse()

if ast is None:
return
if ast is None:
return

value = ast()
value = ast()

try:
value = int(value)
except:
pass
try:
value = int(value)
except:
pass

# print(f"AST: {ast}")
print(value)
# print(f"AST: {ast}")
print(value)
except errors.BaseError as e:
print(e)


def run_prompt():
Expand Down