From 9af7bf559f139937529ea7747cd275ed163fb1fa Mon Sep 17 00:00:00 2001 From: mlokr Date: Sun, 21 Jul 2024 10:29:58 +0200 Subject: [PATCH] fixed confused shift tokens (I still dont know which side is left) --- hblang/README.md | 26 +++++++++++++++++++ hblang/src/codegen.rs | 4 ++- hblang/src/lexer.rs | 10 +++---- .../codegen_tests_sort_something_viredly.txt | 3 +++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 hblang/tests/codegen_tests_sort_something_viredly.txt diff --git a/hblang/README.md b/hblang/README.md index d4381a7..ade4f69 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -517,3 +517,29 @@ foo := fn(): Foo { Foo := struct {x: int, y: u32, z: u32} ``` + +#### sort_something_viredly +```hb +main := fn(): int { + foo := sqrt + return 0 +} + +sqrt := fn(x: int): int { + temp := 0 + g := 0 + b := 32768 + bshift := 15 + loop if b == 0 break else { + bshift -= 1 + temp = b + (g << 1) + temp <<= bshift + if x >= temp { + g += b + x -= temp + } + b >>= 1 + } + return g +} +``` diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 325efb7..ed556b9 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -3247,7 +3247,8 @@ mod tests { last_module_name = module_name; last_start = i + m.len() + module_name.len() + 1; } - module_map.push((last_module_name, &input[last_start..])); + parser::test::format(ident, input[last_start..].trim()); + module_map.push((last_module_name, input[last_start..].trim())); let loader = |path: &str, _: &str| { module_map @@ -3338,5 +3339,6 @@ mod tests { arrays => README; struct_return_from_module_function => README; comptime_pointers => README; + sort_something_viredly => README; } } diff --git a/hblang/src/lexer.rs b/hblang/src/lexer.rs index 02c9617..b103860 100644 --- a/hblang/src/lexer.rs +++ b/hblang/src/lexer.rs @@ -98,9 +98,9 @@ pub enum TokenKind { Dot = b'.', Div = b'/', // Unused = 2-6 - Shr = b'<' - 5, + Shl = b'<' - 5, // Unused = 8 - Shl = b'>' - 5, + Shr = b'>' - 5, Colon = b':', Semi = b';', Lt = b'<', @@ -159,14 +159,14 @@ pub enum TokenKind { ModAss = b'%' + 128, XorAss = b'^' + 128, BandAss = b'&' + 128, - ShlAss = b'0' + 128, - ShrAss = b'1' + 128, + ShrAss = b'>' - 5 + 128, + ShlAss = b'<' - 5 + 128, } impl TokenKind { pub fn ass_op(self) -> Option { let id = (self as u8).saturating_sub(128); - if ascii_mask(b"|+-*/%^&01") & (1u128 << id) == 0 { + if ascii_mask(b"|+-*/%^&79") & (1u128 << id) == 0 { return None; } Some(unsafe { std::mem::transmute::(id) }) diff --git a/hblang/tests/codegen_tests_sort_something_viredly.txt b/hblang/tests/codegen_tests_sort_something_viredly.txt new file mode 100644 index 0000000..2f8b41a --- /dev/null +++ b/hblang/tests/codegen_tests_sort_something_viredly.txt @@ -0,0 +1,3 @@ +code size: 357 +ret: 0 +status: Ok(())