mirror of https://github.com/japl-lang/japl.git
Refactored directories and fixed visibility of lex() procedure in lexer.nim
This commit is contained in:
parent
076e8a6911
commit
586cd9418d
|
@ -186,7 +186,7 @@ proc scanToken(self: var Lexer) =
|
|||
raise newException(ParseError, &"Unexpected character '{single}' at {self.line}")
|
||||
|
||||
|
||||
proc lex(self: var Lexer): seq[Token] =
|
||||
proc lex*(self: var Lexer): seq[Token] =
|
||||
while not self.done():
|
||||
self.start = self.current
|
||||
self.scanToken()
|
|
@ -38,7 +38,7 @@ class Environment(object):
|
|||
"""Finds the scope specified by distance"""
|
||||
|
||||
env = self
|
||||
for i in range(0, distance):
|
||||
for _ in range(distance):
|
||||
env = env.enclosing
|
||||
return env
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
from .tokentype import TokenType
|
||||
|
||||
|
||||
@dataclass
|
||||
class Token(object):
|
||||
"""The representation of a JAPL token"""
|
|
@ -1,6 +1,5 @@
|
|||
from enum import Enum, auto
|
||||
|
||||
|
||||
class TokenType(Enum):
|
||||
"""
|
||||
An enumeration for all JAPL types
|
||||
|
@ -44,7 +43,6 @@ class TokenType(Enum):
|
|||
IF = auto()
|
||||
NIL = auto()
|
||||
OR = auto()
|
||||
PRINT = auto()
|
||||
RETURN = auto()
|
||||
SUPER = auto()
|
||||
THIS = auto()
|
|
@ -64,7 +64,7 @@ class Resolver(Expression.Visitor, Statement.Visitor):
|
|||
Resolves an expression
|
||||
"""
|
||||
|
||||
expression.accept(self)
|
||||
return expression.accept(self)
|
||||
|
||||
@resolve.register
|
||||
def resolve_statements(self, stmt: list):
|
||||
|
@ -190,11 +190,6 @@ class Resolver(Expression.Visitor, Statement.Visitor):
|
|||
if stmt.else_branch:
|
||||
self.resolve(stmt.else_branch)
|
||||
|
||||
def visit_print(self, stmt):
|
||||
"""Visits a print statement node"""
|
||||
|
||||
self.resolve(stmt.expression)
|
||||
|
||||
def visit_return(self, stmt):
|
||||
"""Visits a return statement node"""
|
||||
|
|
@ -47,31 +47,23 @@ class JAPL(object):
|
|||
except (EOFError, KeyboardInterrupt):
|
||||
print()
|
||||
return
|
||||
else:
|
||||
if not source:
|
||||
continue
|
||||
lexer = Lexer(source)
|
||||
try:
|
||||
tokens = lexer.lex()
|
||||
except ParseError as err:
|
||||
print(f"\nAn exception occurred, details below\n\nParseError: {err.args[0]}")
|
||||
if not source:
|
||||
continue
|
||||
lexer = Lexer(source)
|
||||
try:
|
||||
tokens = lexer.lex()
|
||||
ast = Parser(tokens).parse()
|
||||
self.resolver.resolve(ast)
|
||||
result = self.interpreter.interpret(ast)
|
||||
except ParseError as err:
|
||||
if len(err.args) == 2:
|
||||
token, message = err.args
|
||||
print(f"An exception occurred at line {token.line} at '{token.lexeme}': {message}")
|
||||
else:
|
||||
try:
|
||||
ast = Parser(tokens).parse()
|
||||
except ParseError as err:
|
||||
token, message = err.args
|
||||
print(f"An exception occurred at line {token.line} at '{token.lexeme}': {message}")
|
||||
else:
|
||||
try:
|
||||
self.resolver.resolve(ast)
|
||||
result = self.interpreter.interpret(ast)
|
||||
except JAPLError as error:
|
||||
if len(error.args) == 2:
|
||||
token, message = error.args
|
||||
print(
|
||||
f"An exception occurred at line {token.line}, file 'stdin' at '{token.lexeme}': {message}")
|
||||
else:
|
||||
print(f"An exception occurred, details below\n\n{type(error).__name__}: {error}")
|
||||
else:
|
||||
if result is not None:
|
||||
print(repr(result))
|
||||
print(f"\nAn exception occurred, details below\n\nParseError: {err.args[0]}")
|
||||
except JAPLError as error:
|
||||
if len(error.args) == 2:
|
||||
token, message = error.args
|
||||
print(f"An exception occurred at line {token.line}, file 'stdin' at '{token.lexeme}': {message}")
|
||||
else:
|
||||
print(f"An exception occurred, details below\n\n{type(error).__name__}: {error}")
|
Loading…
Reference in New Issue