Skip to content

Commit

Permalink
feat: Introduce <REAL>_TRUNC_<INTEGER>
Browse files Browse the repository at this point in the history
This commit introduces the `<REAL>_TRUNC_<INTEGER>` library functions,
which under the hood use the compilers built-in implicit conversion feature.
  • Loading branch information
volsa committed Oct 15, 2024
1 parent 83c5b03 commit e069720
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 1 deletion.
39 changes: 39 additions & 0 deletions libs/stdlib/iec61131-st/real_trunc_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
f = open("real_trunc_integer.st", "w")

types_from = [
"REAL",
"LREAL",
]

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

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


f.write(template.format("LREAL", "REAL"))

for type_from in types_from:
for type_to in types_to:
f.write(template.format(type_from, type_to))
221 changes: 221 additions & 0 deletions libs/stdlib/iec61131-st/real_trunc_integer.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
(********************
*
* Converts a LREAL to REAL
*
*********************)
FUNCTION LREAL_TRUNC_REAL : REAL
VAR_INPUT
in : LREAL;
END_VAR

LREAL_TRUNC_REAL := in;
END_FUNCTION

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

REAL_TRUNC_SINT := in;
END_FUNCTION

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

REAL_TRUNC_USINT := in;
END_FUNCTION

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

REAL_TRUNC_INT := in;
END_FUNCTION

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

REAL_TRUNC_UINT := in;
END_FUNCTION

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

REAL_TRUNC_DINT := in;
END_FUNCTION

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

REAL_TRUNC_UDINT := in;
END_FUNCTION

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

REAL_TRUNC_LINT := in;
END_FUNCTION

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

REAL_TRUNC_ULINT := in;
END_FUNCTION

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

LREAL_TRUNC_SINT := in;
END_FUNCTION

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

LREAL_TRUNC_USINT := in;
END_FUNCTION

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

LREAL_TRUNC_INT := in;
END_FUNCTION

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

LREAL_TRUNC_UINT := in;
END_FUNCTION

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

LREAL_TRUNC_DINT := in;
END_FUNCTION

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

LREAL_TRUNC_UDINT := in;
END_FUNCTION

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

LREAL_TRUNC_LINT := in;
END_FUNCTION

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

LREAL_TRUNC_ULINT := in;
END_FUNCTION

2 changes: 1 addition & 1 deletion src/codegen/generators/expression_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
} else {
// TODO: using the global context we could get a slice here; currently not possible because the
// global context isn't passed into codegen
Err(Diagnostic::new(&format!("{element:?} not a direct access"))
Err(Diagnostic::new(format!("{element:?} not a direct access"))
.with_error_code("E055")
.with_location(element.get_location()))
}?;
Expand Down
27 changes: 27 additions & 0 deletions tests/lit/single/conversion/real_trunc_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
f = open("real_trunc_integer.st", "w")

types_from = [
"REAL",
"LREAL",
]

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

template = " printf('%d$N', {0}_TRUNC_{1}(4.9)); // CHECK: 4\n"

f.write("// RUN: (%COMPILE %s && %RUN) | %CHECK %s\n")
f.write("FUNCTION main\n")
f.write(template.format("LREAL", "REAL"))
for type_from in types_from:
for type_to in types_to:
f.write(template.format(type_from, type_to))
f.write("END_FUNCTION")
19 changes: 19 additions & 0 deletions tests/lit/single/conversion/real_trunc_integer.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
FUNCTION main
printf('%d$N', REAL_TRUNC_SINT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_USINT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_INT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_UINT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_DINT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_UDINT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_LINT(4.9)); // CHECK: 4
printf('%d$N', REAL_TRUNC_ULINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_SINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_USINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_INT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_UINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_DINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_UDINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_LINT(4.9)); // CHECK: 4
printf('%d$N', LREAL_TRUNC_ULINT(4.9)); // CHECK: 4
END_FUNCTION

0 comments on commit e069720

Please sign in to comment.