Skip to content

Commit 9248a12

Browse files
committed
support number literal
1 parent c204b8d commit 9248a12

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ test/example:
119119
${CELL} -d -t riscv tests/examples/panic.cell && ckb-debugger --bin panic${exe} | grep "runtime panic: hah"
120120
${CELL} -d -t riscv tests/examples/if-cond.cell && ckb-debugger --bin if-cond${exe} | grep "100:0:ss"
121121
${CELL} -d -t riscv tests/examples/switch.cell && ckb-debugger --bin switch${exe} | grep "five"
122+
${CELL} -d -t riscv tests/examples/number-literal.cell && ckb-debugger --bin number-literal${exe}
122123
${CELL} -d -t riscv tests/examples/return.cell && ckb-debugger --bin return${exe}
123124
${CELL} -d -t riscv tests/examples/named-ret-type.cell && ckb-debugger --bin named-ret-type${exe} | grep "0"
124125
${CELL} -d -t riscv tests/examples/func.cell && ckb-debugger --bin func${exe} | grep "999"

compiler/parser/parser.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,35 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif
9898
if err != nil {
9999
panic(err)
100100
}
101+
next := p.lookAhead(1)
102+
var ty TypeNode
103+
if next.Type == lexer.IDENTIFIER {
104+
switch next.Val {
105+
case "u8":
106+
ty = &SingleTypeNode{SourceName: "uint8", TypeName: "uint8"}
107+
p.i++
108+
case "u16":
109+
ty = &SingleTypeNode{SourceName: "uint16", TypeName: "uint16"}
110+
p.i++
111+
case "u32":
112+
ty = &SingleTypeNode{SourceName: "uint32", TypeName: "uint32"}
113+
p.i++
114+
case "u64":
115+
ty = &SingleTypeNode{SourceName: "uint64", TypeName: "uint64"}
116+
p.i++
117+
case "u128":
118+
ty = &SingleTypeNode{SourceName: "uint128", TypeName: "uint128"}
119+
p.i++
120+
case "u256":
121+
ty = &SingleTypeNode{SourceName: "uint256", TypeName: "uint256"}
122+
p.i++
123+
}
124+
}
101125

102126
res = &ConstantNode{
103-
Type: NUMBER,
104-
Value: val,
127+
Type: NUMBER,
128+
TargetType: ty,
129+
Value: val,
105130
}
106131
if withAheadParse {
107132
res = p.aheadParse(res)

tests/examples/number-literal.cell

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import (
2+
"debug"
3+
)
4+
5+
func main() {
6+
a := 100u8
7+
b := 200u16
8+
c := 300u32
9+
d := 400u64
10+
e := 500u128
11+
f := 600u256
12+
if a != uint8(100) {
13+
return 1
14+
}
15+
if b != uint16(200) {
16+
return 2
17+
}
18+
if c != uint32(300) {
19+
return 3
20+
}
21+
if d != uint64(400) {
22+
return 4
23+
}
24+
if e != uint128(500) {
25+
return 5
26+
}
27+
if f != uint256(600) {
28+
return 6
29+
}
30+
return 0
31+
}

0 commit comments

Comments
 (0)