builtin_scope.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from sccd.syntax.expression import *
  2. from sccd.util.debug import *
  3. import termcolor
  4. def _in_state(ctx: EvalContext, state_list: List[str]) -> bool:
  5. from sccd.execution.statechart_state import StatechartState
  6. return StatechartState.in_state(ctx.current_state, state_list)
  7. def _log10(ctx: EvalContext, i: int) -> float:
  8. import math
  9. return math.log10(i)
  10. def _float_to_int(ctx: EvalContext, x: float) -> int:
  11. return int(x)
  12. def _log(ctx: EvalContext, s: str) -> None:
  13. print_debug(termcolor.colored("log: ",'blue')+s)
  14. def _int_to_str(ctx: EvalContext, i: int) -> str:
  15. return str(i)
  16. BuiltIn = Scope("builtin", None)
  17. BuiltIn.declare("INSTATE", SCCDFunction([SCCDArray(SCCDString)], SCCDBool), const=True)
  18. BuiltIn.declare("log10", SCCDFunction([SCCDInt], SCCDFloat), const=True)
  19. BuiltIn.declare("float_to_int", SCCDFunction([SCCDFloat], SCCDInt), const=True)
  20. BuiltIn.declare("log", SCCDFunction([SCCDString]), const=True)
  21. BuiltIn.declare("int_to_str", SCCDFunction([SCCDInt], SCCDString), const=True)
  22. def load_builtins(memory):
  23. memory.push_frame(BuiltIn)
  24. memory.current_frame().storage[0] = _in_state
  25. memory.current_frame().storage[1] = _log10
  26. memory.current_frame().storage[2] = _float_to_int
  27. memory.current_frame().storage[3] = _log
  28. memory.current_frame().storage[4] = _int_to_str