Skip to content

Commit

Permalink
feat: Add TO_<BIT> conversion functions
Browse files Browse the repository at this point in the history
This commit adds the `TO_<BIT>` conversion functions which internally make use of the already existing and tested `<BIT>_TO_<BIT>` functions.
  • Loading branch information
volsa committed Nov 8, 2024
1 parent 8a07316 commit b3217e3
Show file tree
Hide file tree
Showing 2 changed files with 290 additions and 0 deletions.
246 changes: 246 additions & 0 deletions libs/stdlib/iec61131-st/to_bit.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
FUNCTION TO_BOOL<T: ANY_BIT> : BOOL
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_BOOL__BYTE : BOOL
VAR_INPUT
in : BYTE;
END_VAR

TO_BOOL__BYTE := BYTE_TO_BOOL(in);
END_FUNCTION

FUNCTION TO_BOOL__WORD : BOOL
VAR_INPUT
in : WORD;
END_VAR

TO_BOOL__WORD := WORD_TO_BOOL(in);
END_FUNCTION

FUNCTION TO_BOOL__DWORD : BOOL
VAR_INPUT
in : DWORD;
END_VAR

TO_BOOL__DWORD := DWORD_TO_BOOL(in);
END_FUNCTION

FUNCTION TO_BOOL__LWORD : BOOL
VAR_INPUT
in : LWORD;
END_VAR

TO_BOOL__LWORD := LWORD_TO_BOOL(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_BYTE<T: ANY_BIT> : BYTE
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_BYTE__BOOL : BYTE
VAR_INPUT
in : BOOL;
END_VAR

TO_BYTE__BOOL := BOOL_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__CHAR : BYTE
VAR_INPUT
in : CHAR;
END_VAR

TO_BYTE__CHAR := CHAR_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__WORD : BYTE
VAR_INPUT
in : WORD;
END_VAR

TO_BYTE__WORD := WORD_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__DWORD : BYTE
VAR_INPUT
in : DWORD;
END_VAR

TO_BYTE__DWORD := DWORD_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__LWORD : BYTE
VAR_INPUT
in : LWORD;
END_VAR

TO_BYTE__LWORD := LWORD_TO_BYTE(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_WORD<T: ANY_BIT> : WORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_WORD__BOOL : WORD
VAR_INPUT
in : BOOL;
END_VAR

TO_WORD__BOOL := BOOL_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__CHAR : WORD
VAR_INPUT
in : CHAR;
END_VAR

TO_WORD__CHAR := CHAR_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__BYTE : WORD
VAR_INPUT
in : BYTE;
END_VAR

TO_WORD__BYTE := BYTE_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__DWORD : WORD
VAR_INPUT
in : DWORD;
END_VAR

TO_WORD__DWORD := DWORD_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__LWORD : WORD
VAR_INPUT
in : LWORD;
END_VAR

TO_WORD__LWORD := LWORD_TO_WORD(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_DWORD<T: ANY_BIT> : DWORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_DWORD__BOOL : DWORD
VAR_INPUT
in : BOOL;
END_VAR

TO_DWORD__BOOL := BOOL_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__CHAR : DWORD
VAR_INPUT
in : CHAR;
END_VAR

TO_DWORD__CHAR := CHAR_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__WCHAR : DWORD
VAR_INPUT
in : WCHAR;
END_VAR

TO_DWORD__WCHAR := WCHAR_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__BYTE : DWORD
VAR_INPUT
in : BYTE;
END_VAR

TO_DWORD__BYTE := BYTE_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__WORD : DWORD
VAR_INPUT
in : WORD;
END_VAR

TO_DWORD__WORD := WORD_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__LWORD : DWORD
VAR_INPUT
in : LWORD;
END_VAR

TO_DWORD__LWORD := LWORD_TO_DWORD(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_LWORD<T: ANY_BIT> : LWORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_LWORD__BOOL : LWORD
VAR_INPUT
in : BOOL;
END_VAR

TO_LWORD__BOOL := BOOL_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__CHAR : LWORD
VAR_INPUT
in : CHAR;
END_VAR

TO_LWORD__CHAR := CHAR_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__WCHAR : LWORD
VAR_INPUT
in : WCHAR;
END_VAR

TO_LWORD__WCHAR := WCHAR_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__BYTE : LWORD
VAR_INPUT
in : BYTE;
END_VAR

TO_LWORD__BYTE := BYTE_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__WORD : LWORD
VAR_INPUT
in : WORD;
END_VAR

TO_LWORD__WORD := WORD_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__DWORD : LWORD
VAR_INPUT
in : DWORD;
END_VAR

TO_LWORD__DWORD := DWORD_TO_LWORD(in);
END_FUNCTION

44 changes: 44 additions & 0 deletions tests/lit/single/conversion/to_bit.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Note: Tests are kept simple because there are already tests covering the underliny X_TO_Y functions, which
// the TO_Y(X) functions are based on.

// RUN: (%COMPILE %s && %RUN) | %CHECK %s
FUNCTION main
printf('%d$N', TO_BOOL(BYTE#0)); // CHECK: 0
printf('%d$N', TO_BOOL(BYTE#5)); // CHECK: 1
printf('%d$N', TO_BOOL(WORD#0)); // CHECK: 0
printf('%d$N', TO_BOOL(WORD#5)); // CHECK: 1
printf('%d$N', TO_BOOL(DWORD#0)); // CHECK: 0
printf('%d$N', TO_BOOL(DWORD#5)); // CHECK: 1
printf('%d$N', TO_BOOL(LWORD#0)); // CHECK: 0
printf('%d$N', TO_BOOL(LWORD#5)); // CHECK: 1

printf('%d$N', TO_BYTE(BOOL#FALSE)); // CHECK: 0
printf('%d$N', TO_BYTE(BOOL#TRUE)); // CHECK: 1
// printf('%d$N', TO_BYTE(CHAR#'a')); // CCHECK: 97
printf('%d$N', TO_BYTE(WORD#5)); // CHECK: 5
printf('%d$N', TO_BYTE(DWORD#5)); // CHECK: 5
printf('%d$N', TO_BYTE(LWORD#5)); // CHECK: 5

printf('%d$N', TO_WORD(BOOL#FALSE)); // CHECK: 0
printf('%d$N', TO_WORD(BOOL#TRUE)); // CHECK: 1
// printf('%d$N', TO_WORD(CHAR#5)); // CCHECK: 5
printf('%d$N', TO_WORD(BYTE#5)); // CHECK: 5
printf('%d$N', TO_WORD(DWORD#5)); // CHECK: 5
printf('%d$N', TO_WORD(LWORD#5)); // CHECK: 5

printf('%d$N', TO_DWORD(BOOL#FALSE)); // CHECK: 0
printf('%d$N', TO_DWORD(BOOL#TRUE)); // CHECK: 1
// printf('%d$N', TO_DWORD(CHAR#5)); // CCHECK: 5
// printf('%d$N', TO_DWORD(WCHAR#5)); // CCHECK: 5
printf('%d$N', TO_DWORD(BYTE#5)); // CHECK: 5
printf('%d$N', TO_DWORD(WORD#5)); // CHECK: 5
printf('%d$N', TO_DWORD(LWORD#5)); // CHECK: 5

printf('%d$N', TO_LWORD(BOOL#FALSE)); // CHECK: 0
printf('%d$N', TO_LWORD(BOOL#TRUE)); // CHECK: 1
// printf('%d$N', TO_LWORD(CHAR#5)); // CCHECK: 5
// printf('%d$N', TO_LWORD(WCHAR#5)); // CCHECK: 5
printf('%d$N', TO_LWORD(BYTE#5)); // CHECK: 5
printf('%d$N', TO_LWORD(WORD#5)); // CHECK: 5
printf('%d$N', TO_LWORD(DWORD#5)); // CHECK: 5
END_FUNCTION

0 comments on commit b3217e3

Please sign in to comment.