Skip to content

Commit 0457129

Browse files
committed
#202 - improve print codegen
1 parent dfbf61c commit 0457129

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/Application/HydraScript.Application.CodeGeneration/Visitors/InstructionProvider.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,25 @@ public AddressedInstructions Visit(IfStatement visitable)
232232

233233
public AddressedInstructions Visit(PrintStatement visitable)
234234
{
235-
AddressedInstructions result = [];
236-
237235
if (visitable.Expression is PrimaryExpression prim)
238-
result.Add(new AsString(_valueFactory.Create(prim.ToValueDto())));
239-
else
240236
{
241-
result.AddRange(visitable.Expression.Accept(_expressionVisitor));
242-
var name = result.OfType<Simple>().Last().Left!;
243-
result.Add(new AsString(name));
237+
var valueDto = prim.ToValueDto();
238+
var printedValue = _valueFactory.Create(valueDto);
239+
IExecutableInstruction instruction = valueDto is { Type: ValueDtoType.Env } or { Type: ValueDtoType.Constant, Value: string }
240+
? new Print(printedValue)
241+
: new AsString(printedValue);
242+
AddressedInstructions shortResult = [instruction];
243+
if (instruction is AsString asString)
244+
shortResult.Add(new Print(asString.Left!));
245+
return shortResult;
244246
}
245247

248+
AddressedInstructions result = [];
249+
250+
result.AddRange(visitable.Expression.Accept(_expressionVisitor));
251+
var name = result.OfType<Simple>().Last().Left!;
252+
result.Add(new AsString(name));
253+
246254
result.Add(new Print((result[result.End] as AsString)!.Left!));
247255

248256
return result;

0 commit comments

Comments
 (0)