Skip to content

Commit

Permalink
feat: Introduce TO_<NUM> library functions
Browse files Browse the repository at this point in the history
This commit introduces the `TO_<NUM>` library functions, which under the
hood use the already exisiting and tested `<NUM>_TO_<NUM>` library functions.
  • Loading branch information
volsa committed Oct 17, 2024
1 parent 83c5b03 commit 56f7a1f
Show file tree
Hide file tree
Showing 3 changed files with 1,742 additions and 0 deletions.
87 changes: 87 additions & 0 deletions libs/stdlib/iec61131-st/to_num.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
f = open("to_num.st", "w")

types = [
"SINT",
"USINT",
"INT",
"UINT",
"DINT",
"UDINT",
"LINT",
"ULINT",

"REAL",
"LREAL",

# "BIT",
# "BOOL",
# "BYTE",
# "WORD",
# "DWORD",
# "LWORD",

# "STRING",
# "WSTRING",

# "TIME",
# "LTIME",
# "DATE",
# "LDATE",
# "DT",
# "LDT",
# "TOD",
# "LTOD",
]

generic = """(********************
*
* Converts any other numerical value to {0}
*
*********************)
FUNCTION TO_{0}<T: ANY_NUM> : {0}
VAR_INPUT
in : T;
END_VAR
END_FUNCTION
"""

generic_impl = """(********************
*
* Converts {0} to {1}
*
*********************)
FUNCTION TO_{1}__{0} : {1}
VAR_INPUT
in : {0};
END_VAR
TO_{1}__{0} := {0}_TO_{1}(in);
END_FUNCTION
"""

generic_impl_same = """(********************
*
* Converts {0} to {1}
*
*********************)
FUNCTION TO_{1}__{0} : {1}
VAR_INPUT
in : {0};
END_VAR
TO_{1}__{0} := in;
END_FUNCTION
"""

for type_from in types:
f.write(generic.format(type_from))
f.write("\n")

for type_to in types:
if type_from == type_to:
f.write(generic_impl_same.format(type_from, type_to))
else:
f.write(generic_impl.format(type_from, type_to))

f.write("\n")

Loading

0 comments on commit 56f7a1f

Please sign in to comment.