Skip to content

Commit

Permalink
enable panic function
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Aug 11, 2024
1 parent 8c46a93 commit 6199fec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ test/example:
${CELL} -d -t riscv tests/examples/string.cell && ckb-debugger --bin string | grep "eq"
${CELL} -d -t riscv tests/examples/strings.cell && ckb-debugger --bin strings | grep "aa-bb"
${CELL} -d -t riscv tests/examples/make-slice.cell && ckb-debugger --bin make-slice | grep "0422"
${CELL} -d -t riscv tests/examples/panic.cell && ckb-debugger --bin panic | grep "runtime panic: hah"
${CELL} -d -t riscv tests/examples/func.cell && ckb-debugger --bin func | grep "999"
${CELL} -t riscv tests/examples/cell-data.cell && ckb-debugger --bin cell-data
${CELL} -t riscv tests/examples/inputs.cell && ckb-debugger --bin inputs
Expand Down
4 changes: 4 additions & 0 deletions compiler/compiler/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func (c *Compiler) compileCallNode(v *parser.CallNode) value.Value {
return c.appendFuncCall(v)
case "print":
return c.printFuncCall(v)
case "panic":
message, _ := v.Arguments[0].(*parser.ConstantNode)
c.panic(c.contextBlock, message.ValueStr)
return value.Value{}
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/lexer/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ var keywords = map[string]struct{}{
"interface": {},
"range": {},
"make": {},
"panic": {},
}
18 changes: 18 additions & 0 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,24 @@ func (p *parser) parseOneWithOptions(withAheadParse, withArithAhead, withIdentif
return outerConditionNode
}

if current.Val == "panic" {
p.i++
lParent := p.lookAhead(0)
p.expect(lParent, lexer.Item{Type: lexer.OPERATOR, Val: "("})
p.i++
args := p.parseUntil(lexer.Item{Type: lexer.OPERATOR, Val: ")"})
if len(args) != 1 {
panic("wrong number of arguments for panic(message)")
}
if _, ok := args[0].(*ConstantNode); !ok {
panic("wrong type of argument for panic(message)")
}
return &CallNode{
Function: &NameNode{Name: "panic"},
Arguments: args,
}
}

// "make" is a construtor command for composed types
if current.Val == "make" {
p.i++
Expand Down
6 changes: 6 additions & 0 deletions tests/examples/panic.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {
panic("hah")
return 0
}

0 comments on commit 6199fec

Please sign in to comment.