Skip to content

Commit

Permalink
support number literal
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Sep 10, 2024
1 parent c204b8d commit 9248a12
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ test/example:
${CELL} -d -t riscv tests/examples/panic.cell && ckb-debugger --bin panic${exe} | grep "runtime panic: hah"
${CELL} -d -t riscv tests/examples/if-cond.cell && ckb-debugger --bin if-cond${exe} | grep "100:0:ss"
${CELL} -d -t riscv tests/examples/switch.cell && ckb-debugger --bin switch${exe} | grep "five"
${CELL} -d -t riscv tests/examples/number-literal.cell && ckb-debugger --bin number-literal${exe}
${CELL} -d -t riscv tests/examples/return.cell && ckb-debugger --bin return${exe}
${CELL} -d -t riscv tests/examples/named-ret-type.cell && ckb-debugger --bin named-ret-type${exe} | grep "0"
${CELL} -d -t riscv tests/examples/func.cell && ckb-debugger --bin func${exe} | grep "999"
Expand Down
29 changes: 27 additions & 2 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,35 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif
if err != nil {
panic(err)
}
next := p.lookAhead(1)
var ty TypeNode
if next.Type == lexer.IDENTIFIER {
switch next.Val {
case "u8":
ty = &SingleTypeNode{SourceName: "uint8", TypeName: "uint8"}
p.i++
case "u16":
ty = &SingleTypeNode{SourceName: "uint16", TypeName: "uint16"}
p.i++
case "u32":
ty = &SingleTypeNode{SourceName: "uint32", TypeName: "uint32"}
p.i++
case "u64":
ty = &SingleTypeNode{SourceName: "uint64", TypeName: "uint64"}
p.i++
case "u128":
ty = &SingleTypeNode{SourceName: "uint128", TypeName: "uint128"}
p.i++
case "u256":
ty = &SingleTypeNode{SourceName: "uint256", TypeName: "uint256"}
p.i++
}
}

res = &ConstantNode{
Type: NUMBER,
Value: val,
Type: NUMBER,
TargetType: ty,
Value: val,
}
if withAheadParse {
res = p.aheadParse(res)
Expand Down
31 changes: 31 additions & 0 deletions tests/examples/number-literal.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import (
"debug"
)

func main() {
a := 100u8
b := 200u16
c := 300u32
d := 400u64
e := 500u128
f := 600u256
if a != uint8(100) {
return 1
}
if b != uint16(200) {
return 2
}
if c != uint32(300) {
return 3
}
if d != uint64(400) {
return 4
}
if e != uint128(500) {
return 5
}
if f != uint256(600) {
return 6
}
return 0
}

0 comments on commit 9248a12

Please sign in to comment.