Skip to content

Commit

Permalink
nodes type hinting!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Oct 20, 2022
1 parent 254ce02 commit 91ba59b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def execute_run(self, exec_ctx: Context):
global_symbol_table.set("RUN", BuiltInFunction.run)


def run(fn, text):
def run(fn: str, text: str):
# Generate tokens
lexer = Lexer(fn, text)
tokens, error = lexer.make_tokens()
Expand Down
8 changes: 4 additions & 4 deletions src/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@


class Lexer:
def __init__(self, fn, text):
self.fn = fn
self.text = text
self.pos = Position(-1, 0, -1, fn, text)
def __init__(self, fn: str, text: str):
self.fn: str = fn
self.text: str = text
self.pos: Position = Position(-1, 0, -1, fn, text)
self.current_char = None
self.advance()

Expand Down
42 changes: 23 additions & 19 deletions src/nodes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from src.position import Position
from src.token import Token


class NumberNode:
def __init__(self, tok):
def __init__(self, tok: Token):
self.tok = tok

self.pos_start = self.tok.pos_start
Expand All @@ -10,7 +14,7 @@ def __repr__(self):


class StringNode:
def __init__(self, tok):
def __init__(self, tok: Token):
self.tok = tok

self.pos_start = self.tok.pos_start
Expand All @@ -21,23 +25,23 @@ def __repr__(self):


class ListNode:
def __init__(self, element_nodes, pos_start, pos_end):
def __init__(self, element_nodes: list, pos_start: Position, pos_end: Position):
self.element_nodes = element_nodes

self.pos_start = pos_start
self.pos_end = pos_end


class VarAccessNode:
def __init__(self, var_name_tok):
def __init__(self, var_name_tok: Token):
self.var_name_tok = var_name_tok

self.pos_start = self.var_name_tok.pos_start
self.pos_end = self.var_name_tok.pos_end


class VarAssignNode:
def __init__(self, var_name_tok, value_node):
def __init__(self, var_name_tok: Token, value_node):
self.var_name_tok = var_name_tok
self.value_node = value_node

Expand All @@ -46,7 +50,7 @@ def __init__(self, var_name_tok, value_node):


class BinOpNode:
def __init__(self, left_node, op_tok, right_node):
def __init__(self, left_node, op_tok: Token, right_node):
self.left_node = left_node
self.op_tok = op_tok
self.right_node = right_node
Expand All @@ -59,7 +63,7 @@ def __repr__(self):


class UnaryOpNode:
def __init__(self, op_tok, node):
def __init__(self, op_tok: Token, node):
self.op_tok = op_tok
self.node = node

Expand All @@ -71,7 +75,7 @@ def __repr__(self):


class IfNode:
def __init__(self, cases, else_case):
def __init__(self, cases: list, else_case: tuple):
self.cases = cases
self.else_case = else_case

Expand All @@ -82,12 +86,12 @@ def __init__(self, cases, else_case):
class ForNode:
def __init__(
self,
var_name_tok,
var_name_tok: Token,
start_value_node,
end_value_node,
step_value_node,
body_node,
should_return_null,
should_return_null: bool,
):
self.var_name_tok = var_name_tok
self.start_value_node = start_value_node
Expand All @@ -101,7 +105,7 @@ def __init__(


class WhileNode:
def __init__(self, condition_node, body_node, should_return_null):
def __init__(self, condition_node, body_node, should_return_null: bool):
self.condition_node = condition_node
self.body_node = body_node
self.should_return_null = should_return_null
Expand All @@ -111,7 +115,7 @@ def __init__(self, condition_node, body_node, should_return_null):


class FuncDefNode:
def __init__(self, var_name_tok, arg_name_toks, body_node, should_auto_return):
def __init__(self, var_name_tok: Token, arg_name_toks: list, body_node, should_auto_return: bool):
self.var_name_tok = var_name_tok
self.arg_name_toks = arg_name_toks
self.body_node = body_node
Expand All @@ -128,33 +132,33 @@ def __init__(self, var_name_tok, arg_name_toks, body_node, should_auto_return):


class CallNode:
def __init__(self, node_to_call, arg_nodes):
def __init__(self, node_to_call, arg_nodes: list):
self.node_to_call = node_to_call
self.arg_nodes = arg_nodes

self.pos_start = self.node_to_call.pos_start
self.pos_start: Position = self.node_to_call.pos_start

if len(self.arg_nodes) > 0:
self.pos_end = self.arg_nodes[len(self.arg_nodes) - 1].pos_end
self.pos_end: Position = self.arg_nodes[len(self.arg_nodes) - 1].pos_end
else:
self.pos_end = self.node_to_call.pos_end
self.pos_end: Position = self.node_to_call.pos_end


class ReturnNode:
def __init__(self, node_to_return, pos_start, pos_end):
def __init__(self, node_to_return, pos_start: Position, pos_end: Position):
self.node_to_return = node_to_return

self.pos_start = pos_start
self.pos_end = pos_end


class ContinueNode:
def __init__(self, pos_start, pos_end):
def __init__(self, pos_start: Position, pos_end: Position):
self.pos_start = pos_start
self.pos_end = pos_end


class BreakNode:
def __init__(self, pos_start, pos_end):
def __init__(self, pos_start: Position, pos_end: Position):
self.pos_start = pos_start
self.pos_end = pos_end

0 comments on commit 91ba59b

Please sign in to comment.