def da_if_transform(insts: dal.InstGrp, body: dal.InstGrp, elseBody: dal.InstGrp) -> None: test_result = insts.getFlag(insts.TEST_EXPR) assert (isinstance(test_result, core.DBool) or isinstance(test_result, dal.Var)) # Create Jmp instruction else_exists = len(elseBody) > 0 body_exists = len(body) > 0 if else_exists: next_inst_idx = len(elseBody) + 1 + len(insts) jmp_inst = dal.JmpTrue(test_result, core.DInt(next_inst_idx)) else: if body_exists: next_inst_idx = len(body) + 1 + len(insts) jmp_inst = dal.JmpFalse(test_result, core.DInt(next_inst_idx)) else: return insts.addInst(jmp_inst) insts.addInsts(elseBody.insts()) insts.addInsts(body.insts())
def is_open(self) -> core.DBool: return self.operate( core.Operation( "core", "Box", core.opTuple("is_open", []) ) )
def query(self, arg: core.DStr) -> core.DStr: return self.operate( core.Operation( "core", "Box", core.opTuple("query", [arg]) ) )
def op(self, arg: core.DStr) -> None: return self.operate( core.Operation( "core", "Box", core.opTuple("op", [arg]) ) )
def IfStmt_Case_1() -> bool: box = BoxMachinePlus() if box.query(DStr("ident")) == "Box": v = core.DInt(1) else: v = core.DInt(2) return True
def da_binOp_Eq_transform(insts: dal.InstGrp, loperand: object, roperand: object) -> Snippet: s = Snippet() if not da_binOp_need_transformed(loperand, roperand): s.value = loperand == roperand return s else: l = insts.getFlag(insts.COMPARETOR_LEFT) if l is None: l = da_to_python_type(loperand) r = insts.getFlag(insts.COMPARETOR_RIGHT) if r is None: r = da_to_python_type(roperand) insts.setFlagWith(insts.COMPARETOR_LEFT, None) insts.setFlagWith(insts.COMPARETOR_RIGHT, None) ident = insts.new_da_var_ident() var = dal.Var(ident) eq_inst = dal.Equal(l, r, var) insts.addInst(eq_inst) s.addInst(var) ret = core.DBool() ret.compileInfo = 1 ret.transInfo = TransformInfos() ret.transInfo.set_op_ret = var s.value = ret return s
def DictProperty() -> core.Property: return core.Property("DP", {"0/1": "on", "0/2": "off", "0/3": "on"})
def ListProperty() -> core.Property: return core.Property("LP", ["0/1", "0/2", "0/3"])
def trivialOP() -> core.Operation: return core.Operation("S", "D", dtyp.opTuple("OP", "ARG"))
def spec3() -> core.OpSpec: return core.OpSpec("SP3", [("A3", core.DInt), ("A4", core.DStr)], ("R", core.DStr))
def startup(self) -> core.Operation: return core.Operation("core", "ND", core.opTuple("startup", None))
def __eq__(self, o: 'OInst') -> core.DBool: return core.DBool(self._inst_code == o._inst_code) and \ core.DBool(self._args == o.args()) and \ self._ret == o.ret()
def get(self) -> core.DStr: return self.operate( core.Operation("core", "Box", core.opTuple("get", [])))
def put(self, candy: str) -> core.DNone: arg = core.DStr(candy) return self.operate( core.Operation("core", "Box", core.opTuple("put", [arg])))
def close(self) -> core.DNone: return self.operate( core.Operation("core", "Box", core.opTuple("close", [])))
def open(self) -> core.DNone: return self.operate( core.Operation("core", "Box", core.opTuple("open", [])))
def test_SetItem(self, ListProperty, DictProperty) -> None: alterTo = ["off", "on", "off"] for idx in range(2): port = ListProperty[idx] DictProperty[port] = alterTo[idx] for idx in range(2): port = ListProperty[idx] assert alterTo[idx] == DictProperty[port] ############################################################################### # Machine TestCases # ############################################################################### # Box Properties boxProperties = [core.Property("contain", ["10"])] boxOpSpec = [ # Open core.OpSpec("open", [], ("N/A", core.DType)), # Close core.OpSpec("close", [], ("N/A", core.DNone)), # Put core.OpSpec("put", [("Candy", core.DStr)], ("N/A", core.DNone)), # Get core.OpSpec("get", [], ("Candy", core.DStr)) ] class BoxMachine(core.Machine): """
def send(self, package: bytes, port: int) -> core.Operation: return core.Operation("core", "ND", core.opTuple("send", [package, port]))
def Trivial() -> core.Message: return core.Message("S", "D", "MSG")
def __eq__(self, o: 'Var') -> core.DBool: return core.DBool(self.ident == o.ident)
def shutdown(self) -> core.Operation: return core.Operation("core", "ND", core.opTuple("shutdown", None))
import pytest import astpretty import typing as typ import DevAuto as DA import DevAuto.Core as core from DevAuto.Core import DStr import DevAuto.Translator.translator as trans from DevAuto import DFunc from DevAuto.lang_imp import InstGrp from TestCases.CoreTestCases.devCore_Check import BoxMachine boxOpSpec = [ # is_open core.OpSpec("is_open", [], ("successed", core.DBool)), # op core.OpSpec("op", [("arg", core.DStr)], ("ret", core.DNone)), # query core.OpSpec("query", [("arg", core.DStr)], ("ret", core.DStr)) ] class BoxMachinePlus(BoxMachine, core.Dut): def __init__(self) -> None: BoxMachine.__init__(self) self._operations = self._operations + boxOpSpec self.value = 0 @core.Machine.operation("is_open", boxOpSpec) def is_open(self) -> core.DBool:
def spec2() -> core.OpSpec: return core.OpSpec("SP2", [("A1", core.DStr), ("A2", core.DStr)], ("R", core.DStr))
def test_BoxMachineOperation(self, boxMachine) -> None: assert boxMachine.open() == core.DNone() assert boxMachine.close() == core.DNone() assert boxMachine.put("123") == core.DNone() assert boxMachine.get() == core.DStr()
def routeTbl(self) -> da_typ.DA_DICT: query = core.Query("core", "ND", ("route", None, da_typ.DA_DICT)) return self.operate(query)
def spec1() -> core.OpSpec: return core.OpSpec("SP1", [("A1", core.DInt), ("A2", core.DStr)], ("R", core.DStr))
############################################################################### # TrivialMachine # ############################################################################### class TrivialMachine(core.Machine): """ A machine without properties, operations, it's trivial. """ def __init__(self) -> None: core.Machine.__init__(self, "Trivial", [], []) ############################################################################### # BoxMachine, a machine that simulate a box # ############################################################################### # Box Properties boxProperties = [core.Property("contain", 10)] boxOpSpec = [ # Open core.OpSpec("open", (), ("N/A", da_typ.DNone)), # Close core.OpSpec("close", (), ("N/A", da_typ.DNone)), # Put core.OpSpec("put", (("Candy", da_typ.DSTR)), ("N/A", da_typ.DNone)), # Get core.OpSpec("get", (), ("Candy", da_typ.DSTR)) ] class BoxMachine(core.Machine): """