Skip to content

Commit

Permalink
Merge branch 'master' into methods-ast
Browse files Browse the repository at this point in the history
  • Loading branch information
volsa authored Nov 14, 2024
2 parents 1b157fc + ac85aab commit 43186d4
Show file tree
Hide file tree
Showing 4 changed files with 399 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libs/stdlib/iec61131-st/bit_conversion.st
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,22 @@ END_VAR
CHAR_TO_BYTE := ptr^;
END_FUNCTION

(********************
*
* Converts WCHAR to BYTE
*
*********************)
FUNCTION WCHAR_TO_BYTE : BYTE
VAR_INPUT
in : WCHAR;
END_VAR
VAR
ptr : REF_TO BYTE;
END_VAR
ptr := REF(in);
WCHAR_TO_BYTE := ptr^;
END_FUNCTION

(********************
*
* Converts CHAR to WORD
Expand Down
302 changes: 302 additions & 0 deletions libs/stdlib/iec61131-st/to_bit.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
FUNCTION TO_BOOL<T: ANY> : BOOL
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_BOOL__BOOL : BOOL
VAR_INPUT
in : BOOL;
END_VAR

TO_BOOL__BOOL := in;
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> : BYTE
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_BYTE__BYTE : BYTE
VAR_INPUT
in : BYTE;
END_VAR

TO_BYTE__BYTE := in;
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__WCHAR : BYTE
VAR_INPUT
in : WCHAR;
END_VAR

TO_BYTE__WCHAR := WCHAR_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> : WORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_WORD__WORD : WORD
VAR_INPUT
in : WORD;
END_VAR

TO_WORD__WORD := in;
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__WCHAR : WORD
VAR_INPUT
in : WCHAR;
END_VAR

TO_WORD__WCHAR := WCHAR_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> : DWORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_DWORD__DWORD : DWORD
VAR_INPUT
in : DWORD;
END_VAR

TO_DWORD__DWORD := in;
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> : LWORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_LWORD__LWORD : LWORD
VAR_INPUT
in : LWORD;
END_VAR

TO_LWORD__LWORD := in;
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

29 changes: 29 additions & 0 deletions libs/stdlib/tests/bit_conversion_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,35 @@ fn char_to_byte() {
assert_eq!(maintype.c, 99u8);
}

#[test]
fn wchar_to_byte() {
#[derive(Default)]
struct Main {
a: u8,
b: u8,
c: u8,
}

let src = r#"
PROGRAM main
VAR
a : BYTE;
b : BYTE;
c : BYTE;
END_VAR
a := WCHAR_TO_BYTE(WCHAR#"a");
b := WCHAR_TO_BYTE(WCHAR#"b");
c := WCHAR_TO_BYTE(WCHAR#"c");
END_PROGRAM
"#;
let sources = add_std!(src, "bit_conversion.st");
let mut maintype = Main::default();
let _res: u8 = compile_and_run(sources, &mut maintype);
assert_eq!(maintype.a, 97u8);
assert_eq!(maintype.b, 98u8);
assert_eq!(maintype.c, 99u8);
}

#[test]
fn char_to_word() {
#[derive(Default)]
Expand Down
Loading

0 comments on commit 43186d4

Please sign in to comment.