1515use PHPCfg \Func ;
1616use PHPCfg \Script ;
1717use PHPCfg \Operand ;
18- use PHPCfg \Printer \Protobuf \PrimitiveType as PBPrimitiveType ;
18+ use PHPCfg \Printer \Protobuf \Scalar as PBScalar ;
1919use PHPCfg \Printer \Protobuf \Script as PBScript ;
2020use PHPCfg \Printer \Protobuf \Script \PBFunction ;
2121use PHPCfg \Printer \Protobuf \Script \PBFunction \Block as PBBlock ;
3131
3232use PHPCfg \Printer \Protobuf \Script \PBFunction \Block \Op \MapLabel as PBMapLabel ;
3333use PHPCfg \Printer \Protobuf \Script \PBFunction \Block \Op \OneLabelType as PBOneLabelType ;
34- use PHPCfg \Printer \Protobuf \Script \PBFunction \Block \Op \ChildBlock as PBChildBlock ;
3534
3635class Protobuf extends Printer
3736{
@@ -44,7 +43,7 @@ public function printScript(Script $script): PBScript
4443 foreach ($ script ->functions as $ func ) {
4544 $ functions [] = $ this ->printFunc ($ func );
4645 }
47-
46+
4847 $ protoscript ->setFunctions ($ functions );
4948 return $ protoscript ;
5049 }
@@ -53,7 +52,7 @@ public function printFunc(Func $func): PBFunction
5352 {
5453 $ function = new PBFunction ();
5554 $ function ->setName ($ func ->name );
56- if ($ func ->class ) {
55+ if ($ func ->class ) {
5756 $ function ->setClass ($ func ->class ->name );
5857 }
5958
@@ -67,7 +66,7 @@ public function printFunc(Func $func): PBFunction
6766
6867 $ pbblockParents = [];
6968 foreach ($ block ->parents as $ prev ) {
70- if ($ rendered ['blockIds ' ]->contains ($ prev )) {
69+ if ($ rendered ['blockIds ' ]->offsetExists ($ prev )) {
7170 $ pbblockParents [] = $ rendered ['blockIds ' ][$ prev ];
7271 }
7372 }
@@ -83,12 +82,12 @@ public function printFunc(Func $func): PBFunction
8382 $ pbcatchTargets [] = $ pbcatchTarget ;
8483 }
8584
86- if ($ rendered ['blockIds ' ]->contains ($ block ->catchTarget ->finally )) {
85+ if ($ rendered ['blockIds ' ]->offsetExists ($ block ->catchTarget ->finally )) {
8786 $ pbfinallyTarget = new PBFinallyTarget ();
8887 $ pbfinallyTarget ->setBlockId ($ rendered ['blockIds ' ][$ block ->catchTarget ->finally ]);
8988 $ pbblock ->setFinallyTarget ($ pbfinallyTarget );
9089 }
91-
90+
9291 $ pbblock ->setCatchTargets ($ pbcatchTargets );
9392 }
9493
@@ -97,7 +96,8 @@ public function printFunc(Func $func): PBFunction
9796 foreach ($ ops as $ op ) {
9897 $ pbop = new PBOp ();
9998 $ pbop ->setLabel ($ op ['label ' ]);
100-
99+ $ pbop ->setKind ($ op ['kind ' ]);
100+
101101 $ childpbblocks = [];
102102 foreach ($ op ['childBlocks ' ] as $ child ) {
103103 $ childpbblocks [$ child ['name ' ]] = $ rendered ['blockIds ' ][$ child ['block ' ]];
@@ -110,7 +110,7 @@ public function printFunc(Func $func): PBFunction
110110 $ pbblock ->setOps ($ pbops );
111111 $ pbblocks [] = $ pbblock ;
112112 }
113-
113+
114114 $ function ->setBlocks ($ pbblocks );
115115
116116 return $ function ;
@@ -124,37 +124,42 @@ public function renderOperand(Operand $var): PBOneOperand
124124 $ kind = $ result ['kind ' ];
125125 $ type = $ result ['type ' ];
126126
127- if ($ kind == "NULL " ) {
127+ if ($ kind == "NULL " ) {
128128 $ pboneOperand = new PBOneOperand ();
129129 $ pboneOperand ->setNull (new PBNull ());
130130 return $ pboneOperand ;
131- } else if ($ kind == "LITERAL " ) {
131+ } else if ($ kind == "LITERAL " ) {
132132 $ pboneOperand = new PBOneOperand ();
133133 $ literaloperand = new PBLiteral ();
134- $ literaloperand ->setType ($ type );
135- $ primitive = $ this ->renderPrimitiveType ($ result ["value " ]);
136- if ($ primitive ) {
137- $ literaloperand ->setValue ($ primitive );
134+
135+ $ value = $ result ["value " ];
136+ $ type = gettype ($ value );
137+ if (is_bool ($ value )) {
138+ $ value = $ value ? "true " : "false " ;
139+ } else {
140+ $ value = strval ($ value );
138141 }
142+
143+ $ literaloperand ->setType ($ type );
144+ $ literaloperand ->setValue ($ value );
139145 $ pboneOperand ->setLiteral ($ literaloperand );
140146 return $ pboneOperand ;
141- } else if ($ kind == "TEMP " ) {
147+ } else if ($ kind == "TEMP " ) {
142148 $ pboneOperand = new PBOneOperand ();
143149 $ tempoperand = new PBTemporary ();
144150 $ tempoperand ->setType ($ type );
145151 $ tempoperand ->setId ($ result ["id " ]);
146- if ($ result ["original " ] && $ result ["original " ]->hasVariable ()) {
152+ if ($ result ["original " ] && $ result ["original " ]->hasVariable ()) {
147153 $ tempoperand ->setOriginal ($ result ["original " ]->getVariable ());
148154 }
149155 $ pboneOperand ->setTemporary ($ tempoperand );
150156 return $ pboneOperand ;
151-
152- } else if ($ kind == "VARIABLE " ) {
157+ } else if ($ kind == "VARIABLE " ) {
153158 $ pboneOperand = new PBOneOperand ();
154159 $ varoperand = new PBVariable ();
155160 $ varoperand ->setType ($ type );
156161 $ varoperand ->setName ("$ " . $ result ["name " ]);
157- if (!empty ($ result ["scope " ])) {
162+ if (!empty ($ result ["scope " ])) {
158163 $ varoperand ->setScope ($ result ["scope " ]);
159164 }
160165 $ varoperand ->setReference ($ result ["reference " ]);
@@ -167,27 +172,25 @@ public function renderOperand(Operand $var): PBOneOperand
167172 throw new LogicException ("Unknown operand rendering: " . get_class ($ var ));
168173 }
169174
170- public function renderPrimitiveType (string | float | int | bool $ value ): ? PBPrimitiveType
175+ public function renderScalar (string | float | int | bool $ value ): PBScalar
171176 {
172- if (is_string ($ value )) {
173- $ primitive = new PBPrimitiveType ();
174- $ primitive ->setString ($ value );
175- return $ primitive ;
176- } else if (is_bool ($ value )) {
177- $ primitive = new PBPrimitiveType ();
178- $ primitive ->setBool ($ value );
179- return $ primitive ;
180- } else if (is_float ($ value )) {
181- $ primitive = new PBPrimitiveType ();
182- $ primitive ->setFloat ($ value );
183- return $ primitive ;
184- } else if (is_int ($ value )) {
185- $ primitive = new PBPrimitiveType ();
186- $ primitive ->setInt ($ value );
187- return $ primitive ;
177+ if (is_bool ($ value )) {
178+ $ scalar = new PBScalar ();
179+ $ scalar ->setBool ($ value );
180+ return $ scalar ;
181+ } else if (is_float ($ value )) {
182+ $ scalar = new PBScalar ();
183+ $ scalar ->setFloat ($ value );
184+ return $ scalar ;
185+ } else if (is_int ($ value )) {
186+ $ scalar = new PBScalar ();
187+ $ scalar ->setInt ($ value );
188+ return $ scalar ;
188189 }
189190
190- return null ;
191+ $ scalar = new PBScalar ();
192+ $ scalar ->setString (strval ($ value ));
193+ return $ scalar ;
191194 }
192195
193196 public function renderOpLabelValue (mixed $ value ): PBOneLabelType
@@ -202,14 +205,11 @@ public function renderOpLabelValue(mixed $value): PBOneLabelType
202205 }
203206
204207 $ maplabel ->setValue ($ map );
205- $ result ->setMap ($ maplabel );
206- } else if ($ value instanceof PBOneOperand) {
208+ $ result ->setMap ($ maplabel );
209+ } else if ($ value instanceof PBOneOperand) {
207210 $ result ->setOperand ($ value );
208211 } else {
209- $ primitive = $ this ->renderPrimitiveType ($ value );
210- if ($ primitive ) {
211- $ result ->setPrimitive ($ primitive );
212- }
212+ $ result ->setScalar ($ this ->renderScalar ($ value ));
213213 }
214214
215215 return $ result ;
@@ -218,18 +218,18 @@ public function renderOpLabelValue(mixed $value): PBOneLabelType
218218 public function renderOpLabel (array $ desc ): PBMapLabel
219219 {
220220 unset($ desc ['childblocks ' ]);
221+ unset($ desc ['kind ' ]);
221222
223+ $ map = [];
222224 foreach ($ desc as $ name => $ val ) {
223225 if (is_array ($ val )) {
224226 foreach ($ val as $ k => $ v ) {
225227 $ map [$ k ] = $ this ->renderOpLabelValue ($ v );
226228 }
227229 } else {
228- $ stringlabel = new PBOneLabelType ();
229- $ primitive = new PBPrimitiveType ();
230- $ primitive ->setString ($ val );
231- $ stringlabel ->setPrimitive ($ primitive );
232- $ map [$ name ] = $ stringlabel ;
230+ $ label = new PBOneLabelType ();
231+ $ label ->setScalar ($ this ->renderScalar ($ val ));
232+ $ map [$ name ] = $ label ;
233233 }
234234 }
235235
0 commit comments