From 7785c220bcef000aedfbe2b9c95a32272786f370 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 21 Mar 2026 15:36:58 +0000 Subject: Serialize Patio operands to Protobuf --- .../compiler/patio/protobuf/PatioToProtobuf.java | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java b/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java index a08f42b6..f3142428 100644 --- a/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java +++ b/src/main/java/org/shedlang/compiler/patio/protobuf/PatioToProtobuf.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.patio.protobuf; import org.shedlang.compiler.patio.*; +import org.shedlang.compiler.patio.operands.*; import org.shedlang.compiler.patio.types.*; import org.shedlang.compiler.patio.values.*; import org.shedlang.compiler.sources.Source; @@ -300,6 +301,85 @@ public class PatioToProtobuf { }; } + // == Operands == + + public static PatioProtobuf.PatioOperand serializeOperand(PatioOperand operand) { + var builder = PatioProtobuf.PatioOperand.newBuilder(); + + switch (operand) { + case PatioBoolConst boolConst -> { + var boolConstBuilder = builder.getBoolConstBuilder(); + boolConstBuilder.setValue(boolConst.value()); + } + + case PatioByteConst byteConst -> { + var byteConstBuilder = builder.getByteConstBuilder(); + byteConstBuilder.setValue(byteConst.value()); + } + + case PatioDataPtrConst dataPtrConst -> { + var dataPtrConstBuilder = builder.getDataPtrConstBuilder(); + dataPtrConstBuilder.setValue(serializeDataPtrValue(dataPtrConst.value())); + } + + case PatioFunctionPtrConst functionPtrConst -> { + var functionPtrConstBuilder = builder.getFunctionPtrConstBuilder(); + functionPtrConstBuilder.setName(functionPtrConst.name()); + } + + case PatioIntConst intConst -> { + var intConstBuilder = builder.getIntConstBuilder(); + intConstBuilder.setValue(serializeIntValue(intConst.value())); + } + + case PatioInt32Const int32Const -> { + var int32ConstBuilder = builder.getInt32ConstBuilder(); + int32ConstBuilder.setValue(serializeIntValue(int32Const.value())); + } + + case PatioLocalRef localRef -> { + var localRefBuilder = builder.getLocalRefBuilder(); + localRefBuilder.setName(localRef.name()); + } + } + + return builder.build(); + } + + public static PatioOperand deserializeOperand(PatioProtobuf.PatioOperand operandProtobuf) { + return switch (operandProtobuf.getValueCase()) { + case BOOL_CONST -> + new PatioBoolConst(operandProtobuf.getBoolConst().getValue()); + + case BYTE_CONST -> + new PatioByteConst((byte) operandProtobuf.getByteConst().getValue()); + + case DATA_PTR_CONST -> + new PatioDataPtrConst( + deserializeDataPtrValue(operandProtobuf.getDataPtrConst().getValue()) + ); + + case FUNCTION_PTR_CONST -> + new PatioFunctionPtrConst(operandProtobuf.getFunctionPtrConst().getName()); + + case INT_CONST -> + new PatioIntConst( + deserializeIntValue(operandProtobuf.getIntConst().getValue()) + ); + + case INT32_CONST -> + new PatioInt32Const( + deserializeIntValue(operandProtobuf.getInt32Const().getValue()) + ); + + case LOCAL_REF -> + new PatioLocalRef(operandProtobuf.getLocalRef().getName()); + + case VALUE_NOT_SET -> + throw new UnsupportedOperationException("TODO"); + }; + } + // == Data ptr values == public static PatioProtobuf.PatioDataPtrValue serializeDataPtrValue(PatioDataPtrValue value) { -- cgit v1.2.3