Skip to content

Commit

Permalink
is it done??
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Oct 20, 2022
1 parent 2eddd79 commit 593bb97
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@


class BuiltInFunction(BaseFunction):
def __init__(self, name):
def __init__(self, name: str):
super().__init__(name)

def execute(self, args):
def execute(self, args: list):
res = RTResult()
exec_ctx = self.generate_new_context()

Expand Down
8 changes: 4 additions & 4 deletions src/functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.position import Position
from src.runtime_result import RTResult
import src.runtime_result
import src.errors as errors
from src.values import Value, Number
from src.context import Context
Expand Down Expand Up @@ -48,7 +48,7 @@ def generate_new_context(self):
return new_context

def check_args(self, arg_names: list, args: list):
res = RTResult()
res = src.runtime_result.RTResult()

if len(args) > len(arg_names):
return res.failure(
Expand Down Expand Up @@ -80,7 +80,7 @@ def populate_args(self, arg_names: list, args: list, exec_ctx: Context):
exec_ctx.symbol_table.set(arg_name, arg_value)

def check_and_populate_args(self, arg_names: list, args: list, exec_ctx: Context):
res = RTResult()
res = src.runtime_result.RTResult()
res.register(self.check_args(arg_names, args))
if res.should_return():
return res
Expand All @@ -96,7 +96,7 @@ def __init__(self, name: str, body_node, arg_names: list, should_auto_return: bo
self.should_auto_return: bool = should_auto_return

def execute(self, args: list):
res = RTResult()
res = src.runtime_result.RTResult()
interpreter = Interpreter()
exec_ctx = self.generate_new_context()

Expand Down
32 changes: 16 additions & 16 deletions src/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
WhileNode,
)
from src.context import Context
from src.runtime_result import RTResult
import src.runtime_result
import src.errors as errors
from src.values import String, List, Number
import src.functions as funcs
Expand Down Expand Up @@ -48,21 +48,21 @@ def no_visit_method(self, node, context: Context):
###################################

def visit_NumberNode(self, node: NumberNode, context: Context):
return RTResult().success(
return src.runtime_result.RTResult().success(
Number(node.tok.value)
.set_context(context)
.set_pos(node.pos_start, node.pos_end)
)

def visit_StringNode(self, node: StringNode, context: Context):
return RTResult().success(
return src.runtime_result. RTResult().success(
String(node.tok.value)
.set_context(context)
.set_pos(node.pos_start, node.pos_end)
)

def visit_ListNode(self, node: ListNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
elements = []

for element_node in node.element_nodes:
Expand All @@ -75,7 +75,7 @@ def visit_ListNode(self, node: ListNode, context: Context):
)

def visit_VarAccessNode(self, node: VarAccessNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
var_name = node.var_name_tok.value
value = context.symbol_table.get(var_name)

Expand All @@ -93,7 +93,7 @@ def visit_VarAccessNode(self, node: VarAccessNode, context: Context):
return res.success(value)

def visit_VarAssignNode(self, node: VarAssignNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
var_name = node.var_name_tok.value
value = res.register(self.visit(node.value_node, context))
if res.should_return():
Expand All @@ -103,7 +103,7 @@ def visit_VarAssignNode(self, node: VarAssignNode, context: Context):
return res.success(value)

def visit_BinOpNode(self, node: BinOpNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
left = res.register(self.visit(node.left_node, context))
if res.should_return():
return res
Expand Down Expand Up @@ -144,7 +144,7 @@ def visit_BinOpNode(self, node: BinOpNode, context: Context):
return res.success(result.set_pos(node.pos_start, node.pos_end))

def visit_UnaryOpNode(self, node: UnaryOpNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
number = res.register(self.visit(node.node, context))
if res.should_return():
return res
Expand All @@ -162,7 +162,7 @@ def visit_UnaryOpNode(self, node: UnaryOpNode, context: Context):
return res.success(number.set_pos(node.pos_start, node.pos_end))

def visit_IfNode(self, node: IfNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()

for condition, expr, should_return_null in node.cases:
condition_value = res.register(self.visit(condition, context))
Expand All @@ -185,7 +185,7 @@ def visit_IfNode(self, node: IfNode, context: Context):
return res.success(Number.null)

def visit_ForNode(self, node: ForNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
elements = []

start_value = res.register(self.visit(node.start_value_node, context))
Expand Down Expand Up @@ -239,7 +239,7 @@ def visit_ForNode(self, node: ForNode, context: Context):
)

def visit_WhileNode(self, node: WhileNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
elements = []

while True:
Expand Down Expand Up @@ -275,7 +275,7 @@ def visit_WhileNode(self, node: WhileNode, context: Context):
)

def visit_FuncDefNode(self, node: FuncDefNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()

func_name = node.var_name_tok.value if node.var_name_tok else None
body_node = node.body_node
Expand All @@ -292,7 +292,7 @@ def visit_FuncDefNode(self, node: FuncDefNode, context: Context):
return res.success(func_value)

def visit_CallNode(self, node: CallNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()
args = []

value_to_call = res.register(self.visit(node.node_to_call, context))
Expand All @@ -316,7 +316,7 @@ def visit_CallNode(self, node: CallNode, context: Context):
return res.success(return_value)

def visit_ReturnNode(self, node: ReturnNode, context: Context):
res = RTResult()
res = src.runtime_result.RTResult()

if node.node_to_return:
value = res.register(self.visit(node.node_to_return, context))
Expand All @@ -328,7 +328,7 @@ def visit_ReturnNode(self, node: ReturnNode, context: Context):
return res.success_return(value)

def visit_ContinueNode(self, node: ContinueNode, context: Context):
return RTResult().success_continue()
return src.runtime_result.RTResult().success_continue()

def visit_BreakNode(self, node: BreakNode, context: Context):
return RTResult().success_break()
return src.runtime_result.RTResult().success_break()
26 changes: 14 additions & 12 deletions src/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from src.errors import InvalidSyntaxError
import typing
from src.runtime_result import RTResult
from src.errors import InvalidSyntaxError, RTError
from src.nodes import (
NumberNode,
StringNode,
Expand Down Expand Up @@ -48,24 +50,24 @@

class ParseResult:
def __init__(self):
self.error = None
self.error: RTError = None
self.node = None
self.last_registered_advance_count = 0
self.advance_count = 0
self.to_reverse_count = 0
self.last_registered_advance_count: int = 0
self.advance_count: int = 0
self.to_reverse_count: int = 0

def register_advancement(self):
self.last_registered_advance_count = 1
self.advance_count += 1

def register(self, res):
def register(self, res: RTResult):
self.last_registered_advance_count = res.advance_count
self.advance_count += res.advance_count
if res.error:
self.error = res.error
return res.node

def try_register(self, res):
def try_register(self, res: RTResult):
if res.error:
self.to_reverse_count = res.advance_count
return None
Expand All @@ -75,14 +77,14 @@ def success(self, node):
self.node = node
return self

def failure(self, error):
def failure(self, error: RTError):
if not self.error or self.last_registered_advance_count == 0:
self.error = error
return self


class Parser:
def __init__(self, tokens):
def __init__(self, tokens: typing.List[Token]):
self.tokens = tokens
self.tok_idx = -1
self.advance()
Expand Down Expand Up @@ -502,7 +504,7 @@ def list_expr(self):
ListNode(element_nodes, pos_start, self.current_tok.pos_end.copy())
)

def modify_list_expr(self, var_name_tok):
def modify_list_expr(self, var_name_tok: Token):
res = ParseResult()

return res.success(VarAccessNode(var_name_tok))
Expand Down Expand Up @@ -570,7 +572,7 @@ def if_expr_b_or_c(self):

return res.success((cases, else_case))

def if_expr_cases(self, case_keyword):
def if_expr_cases(self, case_keyword: str):
res = ParseResult()
cases = []
else_case = None
Expand Down Expand Up @@ -935,7 +937,7 @@ def func_def(self):

###################################

def bin_op(self, func_a, ops, func_b=None):
def bin_op(self, func_a: typing.Callable, ops: tuple, func_b: typing.Callable=None):
if func_b == None:
func_b = func_a

Expand Down
14 changes: 11 additions & 3 deletions src/runtime_result.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import src.errors


class RTResult:
def __init__(self):
self.value = None
self.error: src.errors.RTError = None
self.func_return_value = None
self.loop_should_continue: bool = False
self.loop_should_break: bool = False
self.reset()

def reset(self):
Expand All @@ -10,7 +18,7 @@ def reset(self):
self.loop_should_break = False

def register(self, res):
self.error = res.error
self.error: src.errors.RTError = res.error
self.func_return_value = res.func_return_value
self.loop_should_continue = res.loop_should_continue
self.loop_should_break = res.loop_should_break
Expand All @@ -36,12 +44,12 @@ def success_break(self):
self.loop_should_break = True
return self

def failure(self, error):
def failure(self, error: src.errors.RTError):
self.reset()
self.error = error
return self

def should_return(self):
def should_return(self) -> bool:
# Note: this will allow you to continue and break outside the current function
return (
self.error
Expand Down
4 changes: 2 additions & 2 deletions src/values.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from src.context import Context
from src.position import Position
from src.runtime_result import RTResult
import src.runtime_result
import src.errors as errors


Expand Down Expand Up @@ -61,7 +61,7 @@ def notted(self, other):
return None, self.illegal_operation(other)

def execute(self, args):
return RTResult().failure(self.illegal_operation())
return src.runtime_result.RTResult().failure(self.illegal_operation())

def copy(self):
raise Exception("No copy method defined")
Expand Down

0 comments on commit 593bb97

Please sign in to comment.