Skip to content

Commit

Permalink
feat: Introduce <REAL>_TO_<INT> functions
Browse files Browse the repository at this point in the history
This commit introduces the `<REAL>_TO_<INT>` library functions, which make use of our implicit truncation
feature.
  • Loading branch information
volsa committed Oct 17, 2024
1 parent 441618f commit d244cf5
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 0 deletions.
24 changes: 24 additions & 0 deletions libs/stdlib/iec61131-st/real_trunc_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
f = open("real_trunc_int.st", "w")

types_real = ["REAL", "LREAL"]
types_int = ["SINT", "USINT", "INT", "UINT", "DINT", "UDINT", "LINT", "ULINT"]

template = """
(********************
*
* Converts {0} to {1}
*
********************)
FUNCTION {0}_TRUNC_{1} : {1}
VAR_INPUT
in : {0};
END_VAR
{0}_TRUNC_{1} := in;
END_FUNCTION
"""

for real in types_real:
for int in types_int:
f.write(template.format(real, int))
f.write("\n")
224 changes: 224 additions & 0 deletions libs/stdlib/iec61131-st/real_trunc_int.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@

(********************
*
* Converts REAL to SINT
*
********************)
FUNCTION REAL_TRUNC_SINT : SINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_SINT := in;
END_FUNCTION


(********************
*
* Converts REAL to USINT
*
********************)
FUNCTION REAL_TRUNC_USINT : USINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_USINT := in;
END_FUNCTION


(********************
*
* Converts REAL to INT
*
********************)
FUNCTION REAL_TRUNC_INT : INT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_INT := in;
END_FUNCTION


(********************
*
* Converts REAL to UINT
*
********************)
FUNCTION REAL_TRUNC_UINT : UINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_UINT := in;
END_FUNCTION


(********************
*
* Converts REAL to DINT
*
********************)
FUNCTION REAL_TRUNC_DINT : DINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_DINT := in;
END_FUNCTION


(********************
*
* Converts REAL to UDINT
*
********************)
FUNCTION REAL_TRUNC_UDINT : UDINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_UDINT := in;
END_FUNCTION


(********************
*
* Converts REAL to LINT
*
********************)
FUNCTION REAL_TRUNC_LINT : LINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_LINT := in;
END_FUNCTION


(********************
*
* Converts REAL to ULINT
*
********************)
FUNCTION REAL_TRUNC_ULINT : ULINT
VAR_INPUT
in : REAL;
END_VAR

REAL_TRUNC_ULINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to SINT
*
********************)
FUNCTION LREAL_TRUNC_SINT : SINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_SINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to USINT
*
********************)
FUNCTION LREAL_TRUNC_USINT : USINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_USINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to INT
*
********************)
FUNCTION LREAL_TRUNC_INT : INT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_INT := in;
END_FUNCTION


(********************
*
* Converts LREAL to UINT
*
********************)
FUNCTION LREAL_TRUNC_UINT : UINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_UINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to DINT
*
********************)
FUNCTION LREAL_TRUNC_DINT : DINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_DINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to UDINT
*
********************)
FUNCTION LREAL_TRUNC_UDINT : UDINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_UDINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to LINT
*
********************)
FUNCTION LREAL_TRUNC_LINT : LINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_LINT := in;
END_FUNCTION


(********************
*
* Converts LREAL to ULINT
*
********************)
FUNCTION LREAL_TRUNC_ULINT : ULINT
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_ULINT := in;
END_FUNCTION

18 changes: 18 additions & 0 deletions tests/lit/single/conversion/real_trunc_int.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
FUNCTION main
printf('%d$N', REAL_TRUNC_SINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_USINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_INT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_UINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_INT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_UINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_DINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_UDINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_LINT(5.6)); // CHECK: 5
printf('%d$N', REAL_TRUNC_ULINT(5.6)); // CHECK: 5

printf('%d$N', REAL_TRUNC_SINT(-5.6)); // CHECK: -5
printf('%d$N', REAL_TRUNC_INT(-5.6)); // CHECK: -5
printf('%d$N', REAL_TRUNC_DINT(-5.6)); // CHECK: -5
printf('%d$N', REAL_TRUNC_LINT(-5.6)); // CHECK: -5
END_FUNCTION

0 comments on commit d244cf5

Please sign in to comment.