Skip to content

Commit

Permalink
feat: 0x 0b
Browse files Browse the repository at this point in the history
  • Loading branch information
Bylx666 committed Apr 17, 2024
1 parent beda469 commit cf07490
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
21 changes: 1 addition & 20 deletions samples/helloworld.ks
Original file line number Diff line number Diff line change
Expand Up @@ -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)
48 changes: 46 additions & 2 deletions src/scan/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -218,7 +262,7 @@ impl Scanner<'_> {
}
is_float = true
},
0x30..=0x39 | b'e' | b'E' => {}
b'0'..=b'9' => (),
_=> break
}
i += 1;
Expand Down

0 comments on commit cf07490

Please sign in to comment.