diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index a53716bbd..192abf6e3 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -223,9 +223,9 @@ impl KclvmServiceImpl { /// assert_eq!(result.type_errors.len(), 0); /// assert_eq!(result.symbols.len(), 12); /// assert_eq!(result.scopes.len(), 3); - /// assert_eq!(result.node_symbol_map.len(), 183); - /// assert_eq!(result.symbol_node_map.len(), 183); - /// assert_eq!(result.fully_qualified_name_map.len(), 193); + /// assert_eq!(result.node_symbol_map.len(), 187); + /// assert_eq!(result.symbol_node_map.len(), 187); + /// assert_eq!(result.fully_qualified_name_map.len(), 197); /// assert_eq!(result.pkg_scope_map.len(), 3); /// ``` #[inline] diff --git a/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_0.snap b/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_0.snap index 75f13b884..aa60d2f4d 100644 --- a/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_0.snap +++ b/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_0.snap @@ -1,6 +1,7 @@ --- source: loader/src/tests.rs expression: "format! (\"{:#?}\", p.symbols.values())" +snapshot_kind: text --- [ SymbolInfo { @@ -88,34 +89,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -298,6 +271,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, diff --git a/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_1.snap b/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_1.snap index 37124b641..2a13b2c65 100644 --- a/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_1.snap +++ b/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_1.snap @@ -1,6 +1,7 @@ --- source: loader/src/tests.rs expression: "format! (\"{:#?}\", p.symbols.values())" +snapshot_kind: text --- [ SymbolInfo { @@ -245,34 +246,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -455,6 +428,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, @@ -494,34 +495,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -704,6 +677,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, @@ -743,34 +744,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -953,6 +926,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, diff --git a/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_2.snap b/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_2.snap index ed383b2de..c4a2a17ae 100644 --- a/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_2.snap +++ b/kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_2.snap @@ -1,6 +1,7 @@ --- source: loader/src/tests.rs expression: "format! (\"{:#?}\", p.symbols.values())" +snapshot_kind: text --- [ SymbolInfo { @@ -577,34 +578,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -787,6 +760,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, @@ -826,34 +827,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -1036,6 +1009,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, @@ -1075,34 +1076,6 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, ), attrs: [ - SymbolRef { - id: Index { - index: 141, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 142, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 143, - generation: 0, - }, - kind: Function, - }, - SymbolRef { - id: Index { - index: 144, - generation: 0, - }, - kind: Function, - }, SymbolRef { id: Index { index: 145, @@ -1285,6 +1258,34 @@ expression: "format! (\"{:#?}\", p.symbols.values())" }, kind: Function, }, + SymbolRef { + id: Index { + index: 171, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 172, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 173, + generation: 0, + }, + kind: Function, + }, + SymbolRef { + id: Index { + index: 174, + generation: 0, + }, + kind: Function, + }, ], is_global: false, }, diff --git a/kclvm/loader/src/snapshots/kclvm_loader__tests__import_stmt_0.snap b/kclvm/loader/src/snapshots/kclvm_loader__tests__import_stmt_0.snap index 254a718db..5284ad512 100644 --- a/kclvm/loader/src/snapshots/kclvm_loader__tests__import_stmt_0.snap +++ b/kclvm/loader/src/snapshots/kclvm_loader__tests__import_stmt_0.snap @@ -1,6 +1,7 @@ --- source: loader/src/tests.rs -expression: "format!(\"{:#?}\", p.symbols.values())" +expression: "format! (\"{:#?}\", p.symbols.values())" +snapshot_kind: text --- [ SymbolInfo { @@ -47,112 +48,112 @@ expression: "format!(\"{:#?}\", p.symbols.values())" attrs: [ SymbolRef { id: Index { - index: 46, + index: 50, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 47, + index: 51, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 48, + index: 52, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 49, + index: 53, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 50, + index: 54, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 51, + index: 55, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 52, + index: 56, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 53, + index: 57, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 54, + index: 58, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 55, + index: 59, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 56, + index: 60, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 57, + index: 61, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 58, + index: 62, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 59, + index: 63, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 60, + index: 64, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 61, + index: 65, generation: 0, }, kind: Function, @@ -248,112 +249,112 @@ expression: "format!(\"{:#?}\", p.symbols.values())" attrs: [ SymbolRef { id: Index { - index: 46, + index: 50, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 47, + index: 51, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 48, + index: 52, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 49, + index: 53, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 50, + index: 54, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 51, + index: 55, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 52, + index: 56, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 53, + index: 57, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 54, + index: 58, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 55, + index: 59, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 56, + index: 60, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 57, + index: 61, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 58, + index: 62, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 59, + index: 63, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 60, + index: 64, generation: 0, }, kind: Function, }, SymbolRef { id: Index { - index: 61, + index: 65, generation: 0, }, kind: Function, @@ -459,7 +460,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())" def: Some( SymbolRef { id: Index { - index: 56, + index: 60, generation: 0, }, kind: Function, diff --git a/kclvm/runtime/src/net/mod.rs b/kclvm/runtime/src/net/mod.rs index 168fbec97..37e6ebe95 100644 --- a/kclvm/runtime/src/net/mod.rs +++ b/kclvm/runtime/src/net/mod.rs @@ -513,16 +513,25 @@ pub extern "C" fn kclvm_net_is_IP_in_CIDR( if let Some(cidr) = get_call_arg_str(args, kwargs, 1, Some("cidr")) { let parts: Vec<&str> = cidr.split('/').collect(); if parts.len() == 2 { - let ip = parts[0]; - let mask = parts[1]; - if let Ok(ip) = Ipv4Addr::from_str(ip) { - if let Ok(mask) = mask.parse::() { - let mask = u32::from_be_bytes(ip.octets()) & !((1 << (32 - mask)) - 1); - let ip = u32::from_be_bytes(ip.octets()); - let x = (ip & mask) == mask; - return kclvm_value_Bool(ctx, x as i8); - } - } + let cidr_ip = parts[0]; + let mask_bits = parts[1]; + let ip_addr = match Ipv4Addr::from_str(&ip) { + Ok(ip_addr) => ip_addr, + Err(_) => return kclvm_value_False(ctx), + }; + let cidr_ip_addr = match Ipv4Addr::from_str(cidr_ip) { + Ok(cidr_ip_addr) => cidr_ip_addr, + Err(_) => return kclvm_value_False(ctx), + }; + let mask_bits = match mask_bits.parse::() { + Ok(mask_bits) if mask_bits <= 32 => mask_bits, + _ => return kclvm_value_False(ctx), + }; + let mask = !((1 << (32 - mask_bits)) - 1); + let ip_u32 = u32::from_be_bytes(ip_addr.octets()); + let cidr_ip_u32 = u32::from_be_bytes(cidr_ip_addr.octets()); + let is_in_cidr = (ip_u32 & mask) == (cidr_ip_u32 & mask); + return kclvm_value_Bool(ctx, is_in_cidr as i8); } } return kclvm_value_False(ctx); diff --git a/kclvm/sema/src/builtin/system_module.rs b/kclvm/sema/src/builtin/system_module.rs index ceda2b74a..d5ca7420b 100644 --- a/kclvm/sema/src/builtin/system_module.rs +++ b/kclvm/sema/src/builtin/system_module.rs @@ -217,7 +217,7 @@ register_net_member! { false, None, ) - is_IP =>Type::function( + is_IP => Type::function( None, Type::bool_ref(), &[ @@ -233,7 +233,7 @@ register_net_member! { false, None, ) - is_loopback_IP =>Type::function( + is_loopback_IP => Type::function( None, Type::bool_ref(), &[ @@ -249,7 +249,7 @@ register_net_member! { false, None, ) - is_multicast_IP =>Type::function( + is_multicast_IP => Type::function( None, Type::bool_ref(), &[ @@ -281,7 +281,7 @@ register_net_member! { false, None, ) - is_link_local_multicast_IP =>Type::function( + is_link_local_multicast_IP => Type::function( None, Type::bool_ref(), &[ @@ -297,7 +297,7 @@ register_net_member! { false, None, ) - is_link_local_unicast_IP =>Type::function( + is_link_local_unicast_IP => Type::function( None, Type::bool_ref(), &[ @@ -313,7 +313,7 @@ register_net_member! { false, None, ) - is_global_unicast_IP =>Type::function( + is_global_unicast_IP => Type::function( None, Type::bool_ref(), &[ @@ -347,7 +347,7 @@ register_net_member! { ) parse_CIDR => Type::function( None, - Type::dict_ref(Type::str_ref(), Type::str_ref()), + Type::dict_ref(Type::str_ref(), Type::any_ref()), &[ Parameter { name: "cidr".to_string(), @@ -387,14 +387,7 @@ register_net_member! { has_default: false, default_value: None, range: dummy_range(), - }, - Parameter { - name: "new_prefix".to_string(), - ty: Type::int_ref(), - has_default: false, - default_value: None, - range: dummy_range(), - }, + } ], r#"Split a CIDR block into smaller subnets with a given prefix."#, false, diff --git a/test/grammar/builtins/net/cidr/main.k b/test/grammar/builtins/net/cidr/main.k new file mode 100644 index 000000000..a565992b0 --- /dev/null +++ b/test/grammar/builtins/net/cidr/main.k @@ -0,0 +1,7 @@ +import net + +cidr0 = net.parse_CIDR("192.168.1.0/24") +cidr1 = net.hosts_in_CIDR("192.168.1.0/24") +cidr2 = net.subnets_from_CIDR("192.168.1.0/24") +cidr3 = net.is_IP_in_CIDR("192.168.1.1", "192.168.1.0/24") +cidr4 = net.is_IP_in_CIDR("192.168.0.1", "192.168.1.0/24") diff --git a/test/grammar/builtins/net/cidr/stdout.golden b/test/grammar/builtins/net/cidr/stdout.golden new file mode 100644 index 000000000..eec869f8a --- /dev/null +++ b/test/grammar/builtins/net/cidr/stdout.golden @@ -0,0 +1,7 @@ +cidr0: + ip: '192.168.1.0' + mask: 24 +cidr1: [] +cidr2: [] +cidr3: true +cidr4: false diff --git a/test/grammar/builtins/net/is_ip_2/main.k b/test/grammar/builtins/net/is_ip_2/main.k index bed34c0c5..d215848d3 100644 --- a/test/grammar/builtins/net/is_ip_2/main.k +++ b/test/grammar/builtins/net/is_ip_2/main.k @@ -6,8 +6,3 @@ isip2 = net.is_link_local_multicast_IP("224.0.0.0") isip3 = net.is_link_local_unicast_IP("fe80::2012:1") isip4 = net.is_global_unicast_IP("220.181.108.89") isip5 = net.is_unspecified_IP("0.0.0.0") -isip6 = net.parse_CIDR("192.168.1.0/24") -isip7 = net.hosts_in_CIDR("192.168.1.0/24") -isip8 = net.subnets_from_CIDR("192.168.1.0/24") -isip9 = net.is_IP_in_CIDR("192.168.1.1", "192.168.1.0/24") -