diff --git a/lib/PHPCfg/Op/Stmt/Catch_.php b/lib/PHPCfg/Op/Stmt/Catch_.php new file mode 100644 index 0000000..296457e --- /dev/null +++ b/lib/PHPCfg/Op/Stmt/Catch_.php @@ -0,0 +1,45 @@ +var = $this->addWriteRef($var); + $this->types = $types; + $this->body = $body; + } + + public function getVariableNames(): array + { + return ['var']; + } + + public function getSubBlocks(): array + { + return ['body']; + } +} diff --git a/lib/PHPCfg/Op/Stmt/Try_.php b/lib/PHPCfg/Op/Stmt/Try_.php new file mode 100644 index 0000000..bcdee96 --- /dev/null +++ b/lib/PHPCfg/Op/Stmt/Try_.php @@ -0,0 +1,37 @@ +body = $body; + $this->catch = $catch; + $this->finally = $finally; + } + + public function getSubBlocks(): array + { + return ['body', 'catch', 'finally']; + } +} diff --git a/lib/PHPCfg/Parser.php b/lib/PHPCfg/Parser.php index cff8abb..8e55a2d 100755 --- a/lib/PHPCfg/Parser.php +++ b/lib/PHPCfg/Parser.php @@ -11,9 +11,11 @@ namespace PHPCfg; +use PHPCfg\Op\Stmt\Catch_; use PHPCfg\Op\Stmt\Jump; use PHPCfg\Op\Stmt\JumpIf; use PHPCfg\Op\Stmt\TraitUse; +use PHPCfg\Op\Stmt\Try_; use PHPCfg\Op\Terminal\Return_; use PHPCfg\Op\TraitUseAdaptation\Alias; use PHPCfg\Op\TraitUseAdaptation\Precedence; @@ -722,9 +724,35 @@ protected function parseStmt_TraitUse(Stmt\TraitUse $node) $this->block->children[] = new TraitUse($traits, $adaptations, $this->mapAttributes($node)); } + protected function parseStmt_Catch(Stmt\Catch_ $node) + { + $body = new Block($this->block); + $this->parseNodes($node->stmts, $body); + + $var = $this->writeVariable($this->parseExprNode($node->var)); + + $parsedTypes = []; + foreach ($node->types as $type) { + $parsedTypes[] = $this->parseTypeNode($type); + } + + $this->block->children[] = new Catch_($var, $parsedTypes, $body, $this->mapAttributes($node)); + } + protected function parseStmt_TryCatch(Stmt\TryCatch $node) { - // TODO: implement this!!! + $body = new Block($this->block); + $this->parseNodes($node->stmts, $body); + + $catch = new Block($this->block); + $this->parseNodes($node->catches, $catch); + + $finally = new Block($this->block); + if ($node->finally != null) { + $this->parseNodes($node->finally->stmts, $finally); + } + + $this->block->children[] = new Try_($body, $catch, $finally, $this->mapAttributes($node)); } protected function parseStmt_Unset(Stmt\Unset_ $node) diff --git a/lib/PHPCfg/Printer.php b/lib/PHPCfg/Printer.php index ddb51a5..32c0152 100755 --- a/lib/PHPCfg/Printer.php +++ b/lib/PHPCfg/Printer.php @@ -167,6 +167,13 @@ protected function renderOp(Op $op) } } } + + if ($op instanceof Op\Stmt\Catch_) { + foreach ($op->types as $index => $type) { + $result .= "\n type[$index]: " . $this->indent($this->renderType($type)); + } + } + if ($op instanceof Op\Stmt\ClassMethod) { $result .= $this->renderAttrGroups($op->attrGroups); $result .= "\n flags: " . $this->indent($this->renderFlags($op)); diff --git a/test/code/try.test b/test/code/try.test new file mode 100644 index 0000000..4b5f43f --- /dev/null +++ b/test/code/try.test @@ -0,0 +1,44 @@ + + body: Block#5 + +Block#4 + Parent: Block#1 + +Block#5 + Parent: Block#3 + Expr_FuncCall + name: LITERAL('get_class') + args[0]: Var#2<$e> + result: Var#3 + Expr_FuncCall + name: LITERAL('var_dump') + args[0]: Var#3 + result: Var#4 \ No newline at end of file diff --git a/test/code/try_finally.test b/test/code/try_finally.test new file mode 100644 index 0000000..c12c33c --- /dev/null +++ b/test/code/try_finally.test @@ -0,0 +1,42 @@ + + body: Block#5 + +Block#4 + Parent: Block#1 + Terminal_Return + expr: LITERAL('finally') + +Block#5 + Parent: Block#3 + Terminal_Return + expr: Var#2<$e> \ No newline at end of file