def visitTerminated(self, ctx: AhParser.TerminatedContext) -> BlockStatus: if ctx.return_statement(): return BlockStatus(returned=True, result=self.visit(ctx.return_statement())) if ctx.continue_statement(): return BlockStatus(continued=True) if ctx.done_statement(): return BlockStatus(done=True) return BlockStatus(result=self.visitChildren(ctx))
def visitWhile_statement( self, ctx: AhParser.While_statementContext) -> BlockStatus: while self.visit(ctx.condition()): block_status: BlockStatus = self.visit(ctx.block()) if block_status.returned: return block_status elif block_status.done: return BlockStatus() return BlockStatus()
def visitStatements(self, ctx: AhParser.StatementsContext) -> BlockStatus: i = 0 while ctx.statement(i): result: BlockStatus = self.visit(ctx.statement(i)) if result.returned or result.continued or result.done: return result if self.is_error(): return BlockStatus() i += 1 return BlockStatus()
def visitSwitch_statement( self, ctx: AhParser.Switch_statementContext) -> BlockStatus: i = 0 while ctx.case_statement(i): case_status: CaseStatus = self.visit(ctx.case_statement(i)) if case_status.executed and not case_status.continued: return BlockStatus(returned=case_status.returned, result=case_status.result) i += 1 if ctx.default_statement(): block_status: BlockStatus = self.visit(ctx.default_statement()) block_status.continued = False block_status.done = False return block_status return BlockStatus()
def visitIf_statement(self, ctx: AhParser.If_statementContext) -> BlockStatus: i = 0 while ctx.condition(i): if self.visit(ctx.condition(i)): return self.visit(ctx.block(i)) i += 1 if ctx.ELSE(): return self.visit(ctx.block(i)) return BlockStatus()
def visitFor_statement(self, ctx: AhParser.For_statementContext) -> BlockStatus: clause = self.visit(ctx.expr()) label = self.visit(ctx.labels()) if isinstance(clause, list): self.log('Looping through list with var ' + label) for item in clause: self.log('> Assigning ' + label + ' = ' + str(item)) self.symbol_table.enter_block_scope() self.set_variable(label=label, value=item) block_status: BlockStatus = self.visit(ctx.block()) self.symbol_table.exit_block_scope() if block_status.returned: return block_status elif block_status.done: return BlockStatus() elif isinstance(clause, float) or isinstance(clause, int): self.log('Looping through range with var ' + label) for i in range(0, int(clause)): self.log('> Assigning ' + label + ' = ' + str(i)) self.symbol_table.enter_block_scope() self.set_variable(label=label, value=i) block_status: BlockStatus = self.visit(ctx.block()) self.symbol_table.exit_block_scope() if block_status.returned: return block_status elif block_status.done: return BlockStatus() else: self.error('Invalid Loop Type: ' + str(type(clause))) return BlockStatus()
def execute_callback(self, callback: Callback = None, parameters: List[Parameter] = None) -> BlockStatus: """ Executes a callback """ self.symbol_table.execute(name=callback.name, parameters=parameters + callback.parameters, isolated_scope=True) if not callback.block: # TODO: This should throw exception self.symbol_table.complete_execution() return BlockStatus() result = self.visit(callback.block()) self.symbol_table.complete_execution() return result
def visitRunnable_statements( self, ctx: AhParser.Runnable_statementsContext) -> BlockStatus: # TODO: Must be changed to await threads where necessary # TODO: Spawn the thread here if saync is set result = BlockStatus() if ctx.method_call_statement(): result = self.visit(ctx.method_call_statement()) elif ctx.os_statement(): result = self.visit(ctx.os_statement()) elif ctx.commandtax_statement(): result = self.visit(ctx.commandtax_statement()) if ctx.atom_callback(): callback = self.visit(ctx.atom_callback()) self.execute_callback( callback=callback, parameters=[Parameter(name="result", value=result)]) return result