From cf0749072200b077336c5b7c8389d9fbb945ca71 Mon Sep 17 00:00:00 2001 From: subkey <2822448396@qq.com> Date: Wed, 17 Apr 2024 09:59:48 +0800 Subject: [PATCH] feat: 0x 0b --- samples/helloworld.ks | 21 +------------------ src/scan/literal.rs | 48 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/samples/helloworld.ks b/samples/helloworld.ks index faf9775..ed9cb91 100644 --- a/samples/helloworld.ks +++ b/samples/helloworld.ks @@ -3,23 +3,4 @@ let s = 99; -match s { - >=99, <90 { - log("我介于99(包含)和100(不含)之间") - } -} - -match s { - =99, =100 { - log("我就是99") - } -} - -match s { - // 第一个条件是等号的话, - // 该条件之后所有条件满足一个即可匹配成功 - =0, >80 { - log("我大于80"); - // 99匹配成功 - } -} +log(0b10101010) \ No newline at end of file diff --git a/src/scan/literal.rs b/src/scan/literal.rs index 8006eaa..fb19bf0 100644 --- a/src/scan/literal.rs +++ b/src/scan/literal.rs @@ -199,13 +199,57 @@ impl Scanner<'_> { // 结算 结算起点到末尾 vec.extend_from_slice(&self.src[start..i]); - + self.set_i(i+1); Expr::Literal(Litr::Buf(vec)) } // 解析数字字面量 b'0'..=b'9' => { + // 判断0x和0b + if self.cur()==b'0' { + match self.src[self.i()+1] { + // 解析16进制 + b'x'=> { + self.set_i(self.i()+2); + let mut i = self.i(); + loop { + match self.src[i] { + b'0'..=b'9'|b'a'..=b'f'|b'A'..=b'F'=> i += 1, + _=> break + } + } + + let n = usize::from_str_radix( + &String::from_utf8_lossy(&self.src[self.i()..i]), 16 + ).unwrap_or_else(|e|panic!("{e}")); + self.set_i(i); + + return Expr::Literal(Litr::Uint(n)); + } + // 解析2进制 + b'b'=> { + self.set_i(self.i()+2); + let mut i = self.i(); + loop { + match self.src[i] { + b'0'|b'1'=> i += 1, + _=> break + } + } + + let n = usize::from_str_radix( + &String::from_utf8_lossy(&self.src[self.i()..i]), 2 + ).unwrap_or_else(|e|panic!("{e}")); + self.set_i(i); + + return Expr::Literal(Litr::Uint(n)); + } + _=>() + } + } + + // 解析十进制并解析后缀 let mut is_float = false; while i < len { match self.src[i] { @@ -218,7 +262,7 @@ impl Scanner<'_> { } is_float = true }, - 0x30..=0x39 | b'e' | b'E' => {} + b'0'..=b'9' => (), _=> break } i += 1;