Skip to content

Commit

Permalink
feat: Add documentation for interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
FallenDeity committed Apr 30, 2023
1 parent 9512567 commit 1eb7168
Show file tree
Hide file tree
Showing 6 changed files with 619 additions and 17 deletions.
555 changes: 555 additions & 0 deletions docs/interpreter.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ def visit_while_stmt(self, stmt: "While") -> None:
try:
self._evaluate(stmt.body)
except PyLoxContinueError:
if isinstance(stmt.body, Block):
if stmt.for_transformed and isinstance(stmt.body, Block):
self._execute_block([stmt.body.statements[-1]], Environment(self._environment))
continue
raise PyLoxRuntimeError("Continue must be inside a loop.")
continue
except PyLoxRuntimeError:
return

Expand Down
11 changes: 1 addition & 10 deletions src/lexer/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ def _read_identifier(self) -> None:
self._cursor.advance()
value = self._cursor.source[self._cursor.start : self._cursor.current]
_ids = [i.lexeme for i in self._tokens if i.token_type == LiteralTokenType.IDENTIFIER]
if self._cursor.column - len(value) == 1 and value not in (
*KeywordTokenType.as_dict().values(),
*_ids,
):
self._logger.error(self._cursor.error_highlight(f"Invalid identifier '{value}'."))
raise PyLoxSyntaxError(self._cursor.error_highlight(f"Invalid identifier '{value}'."))
elif value in KeywordTokenType.as_dict().values():
if value in KeywordTokenType.as_dict().values():
self._add_token(KeywordTokenType(value))
else:
self._add_token(LiteralTokenType.IDENTIFIER, value)
Expand All @@ -73,9 +67,6 @@ def _read_number(self) -> None:
while self._cursor.peek().isdigit():
self._cursor.advance()
value = self._cursor.source[self._cursor.start : self._cursor.current]
if self._cursor.column - len(value) == 1 and self._cursor.peek().isalpha() or self._cursor.peek() == "_":
self._logger.error(self._cursor.error_highlight(f"Invalid number '{value}'."))
raise PyLoxSyntaxError(self._cursor.error_highlight(f"Invalid number '{value}'."))
self._add_token(LiteralTokenType.NUMBER, float(value) if is_float else int(value))

def _read_comment(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _for_statement(self) -> Stmt:
body = Block([body, Expression(increment)])
if condition is None:
condition = Literal(True)
body = While(condition, body)
body = While(condition, body, for_transformed=True)
if initializer is not None:
body = Block([initializer, body])
return body
Expand Down
1 change: 1 addition & 0 deletions src/utils/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ class While(Stmt):

condition: Expr
body: Stmt
for_transformed: bool = False

def accept(self, visitor: StmtProtocol, /) -> t.Any:
"""Accept a visitor."""
Expand Down
63 changes: 59 additions & 4 deletions test.lox
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
class Foo {
init () {
return 1;
class Node {
init (value) {
this.value = value;
this.next = nil;
}
}

var foo = Foo();

class List {
init() {
this.head = nil;
this.tail = nil;
}

add(value) {
var node = Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
}

remove(value) {
var node = this.head;
var prev = nil;
while (node) {
if (node.value == value) {
if (!prev) {
this.head = node.next;
} else {
prev.next = node.next;
}
return;
}
prev = node;
node = node.next;
}
}

write() {
var node = this.head;
while (node) {
print node.value;
node = node.next;
}
}
}


var list = List();
for (var i = 0; i < 10; i = i + 1) {
list.add(i);
}
for (var i = 0; i < 10; i = i + 1) {
if (i % 2 == 0) {
list.remove(i);
}
}
list.write();

0 comments on commit 1eb7168

Please sign in to comment.