diff --git a/Cargo.lock b/Cargo.lock index 102790f81c..7cb8360856 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,9 +51,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" -version = "3.11.0" +version = "3.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" +checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "cc" @@ -90,9 +90,9 @@ dependencies = [ [[package]] name = "clap" -version = "3.2.22" +version = "3.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" +checksum = "1ed5341b2301a26ab80be5cbdced622e80ed808483c52e45e3310a877d3b37d7" dependencies = [ "atty", "bitflags", @@ -139,13 +139,13 @@ dependencies = [ [[package]] name = "console" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89eab4d20ce20cea182308bca13088fecea9c05f6776cf287205d41a0ed3c847" +checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c" dependencies = [ "encode_unicode", + "lazy_static", "libc", - "once_cell", "terminal_size", "winapi", ] @@ -158,10 +158,54 @@ checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "ctor" -version = "0.1.23" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "cxx" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b7d4e43b25d3c994662706a1d4fcfc32aaa6afd287502c111b237093bb23f3a" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84f8829ddc213e2c1368e51a2564c552b65a8cb6a28f31e576270ac81d5e5827" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e72537424b474af1460806647c41d4b6d35d09ef7fe031c5c2fa5766047cc56a" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdffe87e1d521a10f9696f833fe502293ea446d7f256c06128293a4119bdf4cb" +checksum = "309e4fb93eed90e1e14bea0da16b209f81813ba9fc7830c20ed151dd7bc0a4d7" dependencies = [ + "proc-macro2", "quote", "syn", ] @@ -255,17 +299,28 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.50" +version = "0.1.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd911b35d940d2bd0bea0f9100068e5b97b51a1cbe13d13382f132e0365257a0" +checksum = "f5a6ef98976b22b3b7f2f3a806f858cb862044cfa66805aa3ad84cb3d3b785ed" dependencies = [ "android_system_properties", "core-foundation-sys", + "iana-time-zone-haiku", "js-sys", "wasm-bindgen", "winapi", ] +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "indexmap" version = "1.9.1" @@ -279,7 +334,7 @@ dependencies = [ [[package]] name = "inkwell" version = "0.1.0" -source = "git+https://github.com/TheDan64/inkwell?branch=master#4b67a7f6b29c8ce9a5c53cfd12d4c0d0289a4389" +source = "git+https://github.com/TheDan64/inkwell?branch=master#27b8ab5623696709c8948fabfc62ce47d5a32edd" dependencies = [ "either", "inkwell_internals", @@ -292,7 +347,7 @@ dependencies = [ [[package]] name = "inkwell_internals" version = "0.5.0" -source = "git+https://github.com/TheDan64/inkwell?branch=master#4b67a7f6b29c8ce9a5c53cfd12d4c0d0289a4389" +source = "git+https://github.com/TheDan64/inkwell?branch=master#27b8ab5623696709c8948fabfc62ce47d5a32edd" dependencies = [ "proc-macro2", "quote", @@ -301,13 +356,13 @@ dependencies = [ [[package]] name = "insta" -version = "1.20.0" +version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58a931b01c76064c5be919faa2ef0dc570e9a889dcd1e5fef08a8ca6eb4d6c0b" +checksum = "581d4e3314cae4536e5d22ffd23189d4a374696c5ef733eadafae0ed273fd303" dependencies = [ "console", + "lazy_static", "linked-hash-map", - "once_cell", "similar", "yaml-rust", ] @@ -323,9 +378,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" [[package]] name = "js-sys" @@ -344,9 +399,18 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.133" +version = "0.2.135" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" + +[[package]] +name = "link-cplusplus" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966" +checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369" +dependencies = [ + "cc", +] [[package]] name = "linked-hash-map" @@ -536,9 +600,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.3" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" +checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" dependencies = [ "cfg-if 1.0.0", "libc", @@ -549,9 +613,9 @@ dependencies = [ [[package]] name = "pest" -version = "2.3.1" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb779fcf4bb850fbbb0edc96ff6cf34fd90c4b1a112ce042653280d9a7364048" +checksum = "dbc7bc69c062e492337d74d59b120c274fd3d261b6bf6d3207d499b4b379c41a" dependencies = [ "thiserror", "ucd-trie", @@ -595,9 +659,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.44" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bd7356a8122b6c4a24a82b278680c73357984ca2fc79a0f9fa6dea7dced7c58" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" dependencies = [ "unicode-ident", ] @@ -687,6 +751,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" + [[package]] name = "semver" version = "0.9.0" @@ -722,18 +792,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.145" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.145" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" dependencies = [ "proc-macro2", "quote", @@ -742,9 +812,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.85" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" dependencies = [ "itoa", "ryu", @@ -765,9 +835,9 @@ checksum = "62ac7f900db32bf3fd12e0117dd3dc4da74bc52ebaac97f39668446d89694803" [[package]] name = "smallvec" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "strsim" @@ -777,9 +847,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.101" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" dependencies = [ "proc-macro2", "quote", @@ -821,24 +891,24 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.15.1" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] name = "thiserror" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a99cb8c4b9a8ef0e7907cd3b617cc8dc04d571c4e73c8ae403d80ac160bb122" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a891860d3c8d66fec8e73ddb3765f90082374dbaaa833407b904a94f1a7eb43" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", @@ -873,9 +943,9 @@ checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" [[package]] name = "unicode-ident" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" [[package]] name = "unicode-width" @@ -993,46 +1063,60 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ + "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", + "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + [[package]] name = "windows_aarch64_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" [[package]] name = "windows_i686_gnu" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" [[package]] name = "windows_i686_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" [[package]] name = "windows_x86_64_gnu" -version = "0.36.1" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" [[package]] name = "windows_x86_64_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" [[package]] name = "yaml-rust" diff --git a/src/codegen/tests/code_gen_tests.rs b/src/codegen/tests/code_gen_tests.rs index 248d099ff5..053a94249b 100644 --- a/src/codegen/tests/code_gen_tests.rs +++ b/src/codegen/tests/code_gen_tests.rs @@ -3017,3 +3017,29 @@ fn date_and_time_global_constants_initialize() { // THEN the variables are initialized correctly insta::assert_snapshot!(result); } + +#[test] +fn contants_in_case_statements_resolved() { + let result = codegen( + " + PROGRAM main + VAR + DAYS_IN_MONTH : DINT; + END_VAR + VAR CONSTANT + SIXTY : DINT := 60; + END_VAR + CASE DAYS_IN_MONTH OF + 32..SIXTY : DAYS_IN_MONTH := 29; + (SIXTY + 2)..70 : DAYS_IN_MONTH := 30; + ELSE + DAYS_IN_MONTH := 31; + END_CASE; + END_PROGRAM + ", + ); + + // THEN the first case should be 32..60 + // AND the second case should be 62..70 + insta::assert_snapshot!(result); +} diff --git a/src/codegen/tests/snapshots/rusty__codegen__tests__code_gen_tests__contants_in_case_statements_resolved.snap b/src/codegen/tests/snapshots/rusty__codegen__tests__code_gen_tests__contants_in_case_statements_resolved.snap new file mode 100644 index 0000000000..0333c67a4a --- /dev/null +++ b/src/codegen/tests/snapshots/rusty__codegen__tests__code_gen_tests__contants_in_case_statements_resolved.snap @@ -0,0 +1,55 @@ +--- +source: src/codegen/tests/code_gen_tests.rs +expression: result +--- +; ModuleID = 'main' +source_filename = "main" + +%main = type { i32, i32 } + +@main_instance = global %main { i32 0, i32 60 } + +define void @main(%main* %0) { +entry: + %DAYS_IN_MONTH = getelementptr inbounds %main, %main* %0, i32 0, i32 0 + %SIXTY = getelementptr inbounds %main, %main* %0, i32 0, i32 1 + %load_DAYS_IN_MONTH = load i32, i32* %DAYS_IN_MONTH, align 4 + switch i32 %load_DAYS_IN_MONTH, label %else [ + ] + +case: ; preds = %range_then + store i32 29, i32* %DAYS_IN_MONTH, align 4 + br label %continue + +case4: ; preds = %range_then5 + store i32 30, i32* %DAYS_IN_MONTH, align 4 + br label %continue + +else: ; preds = %entry + %load_DAYS_IN_MONTH1 = load i32, i32* %DAYS_IN_MONTH, align 4 + %tmpVar = icmp sge i32 %load_DAYS_IN_MONTH1, 32 + br i1 %tmpVar, label %range_then, label %range_else + +range_then: ; preds = %else + %load_DAYS_IN_MONTH2 = load i32, i32* %DAYS_IN_MONTH, align 4 + %tmpVar3 = icmp sle i32 %load_DAYS_IN_MONTH2, 60 + br i1 %tmpVar3, label %case, label %range_else + +range_else: ; preds = %range_then, %else + %load_DAYS_IN_MONTH7 = load i32, i32* %DAYS_IN_MONTH, align 4 + %tmpVar8 = icmp sge i32 %load_DAYS_IN_MONTH7, 62 + br i1 %tmpVar8, label %range_then5, label %range_else6 + +range_then5: ; preds = %range_else + %load_DAYS_IN_MONTH9 = load i32, i32* %DAYS_IN_MONTH, align 4 + %tmpVar10 = icmp sle i32 %load_DAYS_IN_MONTH9, 70 + br i1 %tmpVar10, label %case4, label %range_else6 + +range_else6: ; preds = %range_then5, %range_else + store i32 31, i32* %DAYS_IN_MONTH, align 4 + br label %continue + +continue: ; preds = %range_else6, %case4, %case + ret void +} + diff --git a/src/resolver/const_evaluator.rs b/src/resolver/const_evaluator.rs index a845b6b4e8..c616ba9a81 100644 --- a/src/resolver/const_evaluator.rs +++ b/src/resolver/const_evaluator.rs @@ -162,6 +162,9 @@ fn needs_evaluation(expr: &AstStatement) -> bool { AstStatement::ExpressionList { expressions, .. } => { expressions.iter().any(needs_evaluation) } + AstStatement::RangeStatement { start, end, .. } => { + needs_evaluation(start) || needs_evaluation(end) + } _ => true, } } @@ -606,6 +609,16 @@ pub fn evaluate( Some(initial.clone()) } } + AstStatement::RangeStatement { start, end, id } => { + let start = + Box::new(evaluate(start, scope, index)?.unwrap_or_else(|| *start.to_owned())); + let end = Box::new(evaluate(end, scope, index)?.unwrap_or_else(|| *end.to_owned())); + Some(AstStatement::RangeStatement { + start, + end, + id: *id, + }) + } _ => return Err(format!("Cannot resolve constant: {:#?}", initial)), }; Ok(literal) diff --git a/src/resolver/tests/const_resolver_tests.rs b/src/resolver/tests/const_resolver_tests.rs index a0376d9f17..74afbb3d29 100644 --- a/src/resolver/tests/const_resolver_tests.rs +++ b/src/resolver/tests/const_resolver_tests.rs @@ -34,7 +34,7 @@ fn find_member_value<'a>(index: &'a Index, pou: &str, reference: &str) -> Option }) } -fn find_connstant_value<'a>(index: &'a Index, reference: &str) -> Option<&'a AstStatement> { +fn find_constant_value<'a>(index: &'a Index, reference: &str) -> Option<&'a AstStatement> { index.find_global_variable(reference).and_then(|it| { index .get_const_expressions() @@ -106,27 +106,27 @@ fn const_references_to_int_compile_time_evaluation() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_real_literal(4.2), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_real_literal(4.0), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); debug_assert_eq!( &create_real_literal(4.0), - find_connstant_value(&index, "f").unwrap() + find_constant_value(&index, "f").unwrap() ); } @@ -156,11 +156,11 @@ fn local_const_references_to_int_compile_time_evaluation() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_real_literal(4.2), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); } @@ -197,12 +197,12 @@ fn local_const_references_to_int_compile_time_evaluation_uses_correct_scopes() { // THEN g should resolve its inital value to global 'a' debug_assert_eq!( &create_int_literal(5), - find_connstant_value(&index, "g").unwrap() + find_constant_value(&index, "g").unwrap() ); // THEN h should resolve its inital value to 'prg.a' debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "h").unwrap() + find_constant_value(&index, "h").unwrap() ); // AND prg.v should resolve its initial value to 'prg.a' debug_assert_eq!( @@ -242,7 +242,7 @@ fn non_const_references_to_int_compile_time_evaluation() { // THEN a to f got their correct initial-literals debug_assert_eq!( &create_int_literal(2), - find_connstant_value(&index, "ok").unwrap() + find_constant_value(&index, "ok").unwrap() ); debug_assert_eq!( @@ -327,15 +327,15 @@ fn const_references_to_negative_reference() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(-4), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_real_literal(-4.2), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(-5), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); } @@ -370,27 +370,27 @@ fn const_references_to_int_additions_compile_time_evaluation() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(11), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_real_literal(4.2), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_real_literal(4.0), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); debug_assert_eq!( &create_real_literal(11.7), - find_connstant_value(&index, "f").unwrap() + find_constant_value(&index, "f").unwrap() ); } @@ -425,27 +425,27 @@ fn const_references_to_int_subtractions_compile_time_evaluation() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(-3), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_real_literal(4.2), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_real_literal(4.0), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); debug_assert_eq!( &create_real_literal(-3.7), - find_connstant_value(&index, "f").unwrap() + find_constant_value(&index, "f").unwrap() ); } @@ -480,27 +480,27 @@ fn const_references_to_int_multiplications_compile_time_evaluation() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(28), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_real_literal(4.2), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_real_literal(4.0), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); debug_assert_eq!( &create_real_literal(30.8), - find_connstant_value(&index, "f").unwrap() + find_constant_value(&index, "f").unwrap() ); } @@ -535,27 +535,27 @@ fn const_references_to_int_division_compile_time_evaluation() { debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( &create_int_literal(40), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(40), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(5), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_real_literal(40.2), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_real_literal(40.0), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); debug_assert_eq!( &create_real_literal(40_f64 / 7.7), - find_connstant_value(&index, "f").unwrap() + find_constant_value(&index, "f").unwrap() ); } @@ -671,127 +671,127 @@ fn const_references_int_float_type_behavior_evaluation() { // INT - INT debug_assert_eq!( &create_int_literal(4), - find_connstant_value(&index, "int_plus_int").unwrap() + find_constant_value(&index, "int_plus_int").unwrap() ); debug_assert_eq!( &create_int_literal(2), - find_connstant_value(&index, "int_minus_int").unwrap() + find_constant_value(&index, "int_minus_int").unwrap() ); debug_assert_eq!( &create_int_literal(6), - find_connstant_value(&index, "int_mul_int").unwrap() + find_constant_value(&index, "int_mul_int").unwrap() ); debug_assert_eq!( &create_int_literal(2), - find_connstant_value(&index, "int_div_int").unwrap() + find_constant_value(&index, "int_div_int").unwrap() ); debug_assert_eq!( &create_int_literal(5 % 2), - find_connstant_value(&index, "int_mod_int").unwrap() + find_constant_value(&index, "int_mod_int").unwrap() ); debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "int_eq_int").unwrap() + find_constant_value(&index, "int_eq_int").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "int_neq_int").unwrap() + find_constant_value(&index, "int_neq_int").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "int_g_int").unwrap() + find_constant_value(&index, "int_g_int").unwrap() ); debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "int_ge_int").unwrap() + find_constant_value(&index, "int_ge_int").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "int_l_int").unwrap() + find_constant_value(&index, "int_l_int").unwrap() ); debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "int_le_int").unwrap() + find_constant_value(&index, "int_le_int").unwrap() ); // INT - REAL debug_assert_eq!( &create_real_literal(4.1), - find_connstant_value(&index, "int_plus_real").unwrap() + find_constant_value(&index, "int_plus_real").unwrap() ); debug_assert_eq!( &create_real_literal(3.0 - 1.1), - find_connstant_value(&index, "int_minus_real").unwrap() + find_constant_value(&index, "int_minus_real").unwrap() ); debug_assert_eq!( &create_real_literal(3.0 * 1.1), - find_connstant_value(&index, "int_mul_real").unwrap() + find_constant_value(&index, "int_mul_real").unwrap() ); debug_assert_eq!( &create_real_literal(5.0 / 2.1), - find_connstant_value(&index, "int_div_real").unwrap() + find_constant_value(&index, "int_div_real").unwrap() ); debug_assert_eq!( &create_real_literal(5.0 % 2.1), - find_connstant_value(&index, "int_mod_real").unwrap() + find_constant_value(&index, "int_mod_real").unwrap() ); // REAL - INT debug_assert_eq!( &create_real_literal(4.3), - find_connstant_value(&index, "real_plus_int").unwrap() + find_constant_value(&index, "real_plus_int").unwrap() ); debug_assert_eq!( &create_real_literal(2.3), - find_connstant_value(&index, "real_minus_int").unwrap() + find_constant_value(&index, "real_minus_int").unwrap() ); debug_assert_eq!( &create_real_literal(6.6), - find_connstant_value(&index, "real_mul_int").unwrap() + find_constant_value(&index, "real_mul_int").unwrap() ); debug_assert_eq!( &create_real_literal(5.2 / 2.0), - find_connstant_value(&index, "real_div_int").unwrap() + find_constant_value(&index, "real_div_int").unwrap() ); debug_assert_eq!( &create_real_literal(5.2 % 2.0), - find_connstant_value(&index, "real_mod_int").unwrap() + find_constant_value(&index, "real_mod_int").unwrap() ); // REAL - REAL debug_assert_eq!( &create_real_literal(4.4), - find_connstant_value(&index, "real_plus_real").unwrap() + find_constant_value(&index, "real_plus_real").unwrap() ); debug_assert_eq!( &create_real_literal(3.3 - 1.1), - find_connstant_value(&index, "real_minus_real").unwrap() + find_constant_value(&index, "real_minus_real").unwrap() ); debug_assert_eq!( &create_real_literal(3.3 * 1.1), - find_connstant_value(&index, "real_mul_real").unwrap() + find_constant_value(&index, "real_mul_real").unwrap() ); debug_assert_eq!( &create_real_literal(5.3 / 2.1), - find_connstant_value(&index, "real_div_real").unwrap() + find_constant_value(&index, "real_div_real").unwrap() ); debug_assert_eq!( &create_real_literal(5.3 % 2.1), - find_connstant_value(&index, "real_mod_real").unwrap() + find_constant_value(&index, "real_mod_real").unwrap() ); // BOOL - BOOL debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "bool_and_bool").unwrap() + find_constant_value(&index, "bool_and_bool").unwrap() ); debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "bool_or_bool").unwrap() + find_constant_value(&index, "bool_or_bool").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "bool_xor_bool").unwrap() + find_constant_value(&index, "bool_xor_bool").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "not_bool").unwrap() + find_constant_value(&index, "not_bool").unwrap() ); } @@ -822,23 +822,23 @@ fn const_references_bool_bit_functions_behavior_evaluation() { // AND the index should have literal values debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_bool_literal(true), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_bool_literal(false), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); } @@ -868,23 +868,23 @@ fn const_references_int_bit_functions_behavior_evaluation() { // AND the index should have literal values debug_assert_eq!( &create_int_literal(0xFFAB), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(0x00AB), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(0xFFFF), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_int_literal(0xFF54), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_int_literal(0x0054), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); } #[test] @@ -953,20 +953,20 @@ fn division_by_0_should_fail() { // AND the real divisions are inf or nan debug_assert_eq!( &create_real_literal(f64::INFINITY), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_real_literal(f64::INFINITY), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); - if let AstStatement::LiteralReal { value, .. } = find_connstant_value(&index, "bb").unwrap() { + if let AstStatement::LiteralReal { value, .. } = find_constant_value(&index, "bb").unwrap() { assert!(value.parse::().unwrap().is_nan()); } else { unreachable!() } - if let AstStatement::LiteralReal { value, .. } = find_connstant_value(&index, "dd").unwrap() { + if let AstStatement::LiteralReal { value, .. } = find_constant_value(&index, "dd").unwrap() { assert!(value.parse::().unwrap().is_nan()); } else { unreachable!() @@ -1000,27 +1000,27 @@ fn const_references_not_function_with_signed_ints() { // AND the index should have literal values debug_assert_eq!( &create_int_literal(-85), - find_connstant_value(&index, "a").unwrap() + find_constant_value(&index, "a").unwrap() ); debug_assert_eq!( &create_int_literal(0x0000_ffab), - find_connstant_value(&index, "aa").unwrap() + find_constant_value(&index, "aa").unwrap() ); debug_assert_eq!( &create_int_literal(171), - find_connstant_value(&index, "b").unwrap() + find_constant_value(&index, "b").unwrap() ); debug_assert_eq!( &create_int_literal(-1), - find_connstant_value(&index, "c").unwrap() + find_constant_value(&index, "c").unwrap() ); debug_assert_eq!( &create_int_literal(-172), - find_connstant_value(&index, "d").unwrap() + find_constant_value(&index, "d").unwrap() ); debug_assert_eq!( &create_int_literal(84), - find_connstant_value(&index, "e").unwrap() + find_constant_value(&index, "e").unwrap() ); } @@ -1048,15 +1048,15 @@ fn const_references_to_bool_compile_time_evaluation() { // THEN a,b,and c got their correct initial-literals debug_assert_eq!(EMPTY, unresolvable); debug_assert_eq!( - find_connstant_value(&index, "a"), + find_constant_value(&index, "a"), Some(&create_bool_literal(true)) ); debug_assert_eq!( - find_connstant_value(&index, "b"), + find_constant_value(&index, "b"), Some(&create_bool_literal(true)) ); debug_assert_eq!( - find_connstant_value(&index, "c"), + find_constant_value(&index, "c"), Some(&create_bool_literal(false)) ); } @@ -1115,11 +1115,11 @@ fn evaluating_constants_can_handle_recursion() { ); // AND aa and bb where resolved correctly debug_assert_eq!( - find_connstant_value(&index, "aa"), + find_constant_value(&index, "aa"), Some(&create_int_literal(4)) ); debug_assert_eq!( - find_connstant_value(&index, "bb"), + find_constant_value(&index, "bb"), Some(&create_int_literal(4)) ); } @@ -1149,7 +1149,7 @@ fn const_string_initializers_should_be_converted() { // AND the globals should have gotten their values debug_assert_eq!( - find_connstant_value(&index, "aa"), + find_constant_value(&index, "aa"), Some(AstStatement::LiteralString { value: "World".into(), is_wide: false, @@ -1158,7 +1158,7 @@ fn const_string_initializers_should_be_converted() { }) ); debug_assert_eq!( - find_connstant_value(&index, "bb"), + find_constant_value(&index, "bb"), Some(AstStatement::LiteralString { value: "Hello".into(), is_wide: true, @@ -1193,7 +1193,7 @@ fn const_lreal_initializers_should_be_resolved_correctly() { // AND the globals should have gotten their values debug_assert_eq!( - find_connstant_value(&index, "tau"), + find_constant_value(&index, "tau"), Some(AstStatement::LiteralReal { value: "6.283".into(), id: 0, @@ -1526,3 +1526,37 @@ fn function_block_initializers_constant_resolved_in_assignment() { // THEN all should be resolved debug_assert_eq!(EMPTY, unresolvable); } + +#[test] +fn contants_in_case_statements_resolved() { + let (parse_result, mut index) = index( + " + PROGRAM main + VAR + DAYS_IN_MONTH : DINT; + END_VAR + VAR CONSTANT + SIXTY : DINT := 60; + END_VAR + CASE DAYS_IN_MONTH OF + 32..SIXTY : DAYS_IN_MONTH := 29; + (SIXTY + 2)..70 : DAYS_IN_MONTH := 30; + ELSE + DAYS_IN_MONTH := 31; + END_CASE; + END_PROGRAM + ", + ); + + // WHEN compile-time evaluation is applied + // AND types are resolved + annotate(&parse_result, &mut index); + let (_, unresolvable) = evaluate_constants(index); + + // THEN all should be resolved + debug_assert_eq!(EMPTY, unresolvable); + + // DONE in Codegen tests: + // AND the first case should be 32..60 + // AND the second case should be 62..70 +} diff --git a/src/validation/tests/statement_validation_tests.rs b/src/validation/tests/statement_validation_tests.rs index 6307b42caf..c165d3052c 100644 --- a/src/validation/tests/statement_validation_tests.rs +++ b/src/validation/tests/statement_validation_tests.rs @@ -394,6 +394,8 @@ fn switch_case() { res := 4; 2*2+1: res := 5; + (BASE*5)..(BASE*10): + res := 6; END_CASE END_PROGRAM "#,