#!/usr/bin/perl use strict; use warnings; use Test::More no_plan => 1; use B::Simple; use ok 'B::Simple'; use ok 'B::Simple::Node'; =pod print "Hello World" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const[PV "Hello World"] s ->5 =cut print(('-' x 70), "\n"); print Seq( Apply( Sym('&print'), Args( Const("Hello World"), ) ) )->sexpr, "\n"; =pod my $x = 4; 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 4] s ->4 4 <0> padsv[$x:1,2] sRM*/LVINTRO ->5 =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), PadVar('$x'), Const(4), ) )->sexpr, "\n"; =pod my @x = (1, 2, 3); a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <2> aassign[t2] vKS ->a - <1> ex-list lKP ->7 3 <0> pushmark s ->4 4 <$> const[IV 1] s ->5 5 <$> const[IV 2] s ->6 6 <$> const[IV 3] s ->7 - <1> ex-list lK ->9 7 <0> pushmark s ->8 8 <0> padav[@x:1,2] lRM*/LVINTRO ->9 =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), PadVar('@x'), List(Const(1), Const(2), Const(3)), ) )->sexpr, "\n"; =pod our $x = 4; 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 4] s ->4 - <1> ex-rv2sv sKRM*/17 ->5 4 <#> gvsv[*x] s/OURINTR ->5 =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), Var('$x'), Const(4), ) )->sexpr, "\n"; =pod my ($x, $y) = (10, 20); a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <2> aassign[t3] vKS ->a - <1> ex-list lKP ->6 3 <0> pushmark s ->4 4 <$> const[IV 10] s ->5 5 <$> const[IV 20] s ->6 - <1> ex-list lKPRM*/128 ->9 6 <0> pushmark sRM*/128 ->7 7 <0> padsv[$x:1,2] lRM*/LVINTRO ->8 8 <0> padsv[$y:1,2] lRM*/LVINTRO ->9 =cut print(('-' x 70), "\n"); print Seq( Apply( # the = operator is polymorphic # it can operate on 2 lists and # just DWIM here. Op('&infix:<=>'), List(Var('$x'), Var('$y')), List(Const(10), Const(20)), ) )->sexpr, "\n"; =pod my $x = 4; print $x + 5 c <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 4] s ->4 4 <0> padsv[$x:1,2] sRM*/LVINTRO ->5 6 <;> nextstate(main 2 -e:1) v ->7 b <@> print vK ->c 7 <0> pushmark s ->8 a <2> add[t2] sK/2 ->b 8 <0> padsv[$x:1,2] s ->9 9 <$> const[IV 5] s ->a =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), PadVar('$x'), Const(4), ), Apply( Sym('&print'), Args( Apply( Op('&infix:<+>'), PadVar('$x'), Const(5) ) ) ) )->sexpr, "\n"; =pod my $x = 4; { print $x + 5; my $x = 3; print $x + 5; } p <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 4] s ->4 4 <0> padsv[$x:1,4] sRM*/LVINTRO ->5 6 <;> nextstate(main 4 -e:1) v ->7 o <2> leaveloop vK/2 ->p 7 <{> enterloop(next->o last->o redo->8) v ->8 - <@> lineseq vKP ->o 8 <;> nextstate(main 2 -e:1) v ->9 d <@> print vK ->e 9 <0> pushmark s ->a c <2> add[t2] sK/2 ->d a <0> padsv[$x:1,4] s ->b b <$> const[IV 5] s ->c e <;> nextstate(main 2 -e:1) v ->f h <2> sassign vKS/2 ->i f <$> const[IV 3] s ->g g <0> padsv[$x:2,3] sRM*/LVINTRO ->h i <;> nextstate(main 3 -e:1) v ->j n <@> print vK ->o j <0> pushmark s ->k m <2> add[t4] sK/2 ->n k <0> padsv[$x:2,3] s ->l l <$> const[IV 5] s ->m =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), PadVar('$x'), Const(4), ), # nested Seq creates a new # block scope to work in. Seq( Apply( Sym('&print'), Args( Apply( Op('&infix:<+>'), PadVar('$x'), Const(5) ) ) ), Apply( Op('&infix:<=>'), PadVar('$x'), Const(4), ), Apply( Sym('&print'), Args( Apply( Op('&infix:<+>'), PadVar('$x'), Const(5) ) ) ), ) )->sexpr, "\n"; =pod my $x = 4; if ($x == 4) { print "true" } e <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 4] s ->4 4 <0> padsv[$x:1,4] sRM*/LVINTRO ->5 6 <;> nextstate(main 4 -e:1) v ->7 - <1> null vK/1 ->e a <|> and(other->b) vK/1 ->e 9 <2> eq sK/2 ->a 7 <0> padsv[$x:1,4] s ->8 8 <$> const[IV 4] s ->9 - <@> scope vK ->- - <0> ex-nextstate v ->b d <@> print vK ->e b <0> pushmark s ->c c <$> const[PV "true"] s ->d =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), PadVar('$x'), Const(4), ), Apply( Op('&control_structure:'), Apply( Op('&infix<==>'), Const(4), PadVar('$x'), ), Seq( Apply( Sym('&print'), Args( Const("Hello World"), ) ) ), ) )->sexpr, "\n"; =pod my $x = 4; if ($x == 4 && $x != 5) { print "true" } i <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 4] s ->4 4 <0> padsv[$x:1,4] sRM*/LVINTRO ->5 6 <;> nextstate(main 4 -e:1) v ->7 - <1> null vK/1 ->i e <|> and(other->f) vK/1 ->i - <1> null sK/1 ->e a <|> and(other->b) sK/1 ->e 9 <2> eq sK/2 ->a 7 <0> padsv[$x:1,4] s ->8 8 <$> const[IV 4] s ->9 d <2> ne sK/2 ->e b <0> padsv[$x:1,4] s ->c c <$> const[IV 5] s ->d - <@> scope vK ->- - <0> ex-nextstate v ->f h <@> print vK ->i f <0> pushmark s ->g g <$> const[PV "true"] s ->h =cut print(('-' x 70), "\n"); print Seq( Apply( Op('&infix:<=>'), PadVar('$x'), Const(4), ), Apply( Op('&control_structure:'), Apply( Op('&infix:<&&>'), # the right and left side of the && # need to be Seq, so that short-circuting # will actually work correctly. If # they were Apply, they would be evaluated # when the && is called, with Seq they # need only be evaluated if needed. Seq( Apply( Op('&infix<==>'), Const(4), PadVar('$x'), ) ), Seq( Apply( Op('&infix'), Const(5), PadVar('$x'), ) ), ), Seq( Apply( Sym('&print'), Args( Const("Hello World"), ) ) ), ) )->sexpr, "\n"; =pod sub add_two { $_[0] + 2 } print add_two(2); main::add_two: 5 <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->5 1 <;> nextstate(main 1 -e:1) v ->2 4 <2> add[t2] sK/2 ->5 - <1> ex-aelem sK/2 ->3 - <1> ex-rv2av sKR/1 ->- 2 <#> aelemfast[*_] s ->3 - <0> ex-const s ->- 3 <$> const[IV 2] s ->4 main program: e <@> leave[1 ref] vKP/REFC ->(end) 6 <0> enter ->7 7 <;> nextstate(main 2 -e:1) v ->8 d <@> print vK ->e 8 <0> pushmark s ->9 c <1> entersub[t2] lKS/TARG,1 ->d - <1> ex-list lK ->c 9 <0> pushmark s ->a a <$> const[IV 2] sM ->b - <1> ex-rv2cv sK/1 ->- b <#> gv[*add_two] s ->c =cut print(('-' x 70), "\n"); print Seq( ## compile time ... Apply( # defining a function is # the same as any other # assignment, just the right # hand side is a Thunk Op('&infix:<=>'), Var('&add_two'), Thunk( Seq( Apply( Op('&infix:<+>'), Param(0), # find param by index ... Const(2) ) ) ) ), Seq( ## runtime ... Apply( Sym('&print'), Args( Apply( # user defined functions are # applied the same as builtins Sym('&add_two'), Args( Const(2) ) ), ) ) ) )->sexpr, "\n"; 1;