From 45a815c4c957b1f83c8e4d350d828c3dba7e284b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 2 Dec 2023 20:27:53 +0000 Subject: Rename InstructionSerializer to ShidSerializer --- .../backends/GenerateInstructionsBackend.java | 4 +- .../compiler/shid/InstructionSerializer.java | 284 --------------------- .../org/shedlang/compiler/shid/ShidSerializer.java | 284 +++++++++++++++++++++ 3 files changed, 286 insertions(+), 286 deletions(-) delete mode 100644 src/main/java/org/shedlang/compiler/shid/InstructionSerializer.java create mode 100644 src/main/java/org/shedlang/compiler/shid/ShidSerializer.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java b/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java index c2a6ea23..0a3dca5e 100644 --- a/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/GenerateInstructionsBackend.java @@ -1,7 +1,7 @@ package org.shedlang.compiler.backends; import org.shedlang.compiler.config.ProjectConfig; -import org.shedlang.compiler.shid.InstructionSerializer; +import org.shedlang.compiler.shid.ShidSerializer; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.typechecker.TypeInfos; @@ -22,7 +22,7 @@ public class GenerateInstructionsBackend implements Backend { System.out.println(namespaceName); var builder = new StringBuilder(); - InstructionSerializer.serializeInstructions(instructions, builder); + ShidSerializer.serializeInstructions(instructions, builder); var instructionsString = builder.toString(); System.out.println(instructionsString); diff --git a/src/main/java/org/shedlang/compiler/shid/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/shid/InstructionSerializer.java deleted file mode 100644 index 760a1095..00000000 --- a/src/main/java/org/shedlang/compiler/shid/InstructionSerializer.java +++ /dev/null @@ -1,284 +0,0 @@ -package org.shedlang.compiler.shid; - -import org.shedlang.compiler.shid.instructions.*; -import org.shedlang.compiler.treetext.Tree; -import org.shedlang.compiler.types.StructType; -import org.shedlang.compiler.types.UserDefinedEffect; - -import java.util.List; - -import static org.shedlang.compiler.treetext.TreeSerializer.serializeTree; -import static org.shedlang.compiler.treetext.Trees.*; - -public class InstructionSerializer { - private InstructionSerializer() { - } - - public static void serializeInstructions(List instructions, StringBuilder builder) { - var tree = instructionsToTree(instructions); - serializeTree(tree, builder, 0); - } - - public static void serializeInstruction(ShidInstruction instruction, StringBuilder builder) { - var tree = instructionToTree(instruction); - serializeTree(tree, builder, 0); - } - - private static Tree instructionsToTree(List instructions) { - return treeListIndented( - instructions.stream() - .map(instruction -> instructionToTree(instruction)) - .toList() - ); - } - - private static Tree instructionToTree(ShidInstruction instruction) { - return switch (instruction) { - case ShidCall callInstruction -> - treeList( - treeIdentifier("call"), - treeNamed( - "positional-arg-count", - treeInt(callInstruction.positionalArgCount()) - ), - treeNamed( - "named-arg-names", - treeList( - callInstruction.namedArgNames().stream() - .map(arg -> treeString(arg)) - .toList() - ) - ) - ); - - case ShidDefineEffect defineEffectInstruction -> - treeList( - treeIdentifier("define-effect"), - userDefinedEffectToTree(defineEffectInstruction.effect()) - ); - - case ShidDefineFunction defineFunctionInstruction -> - treeListIndented( - treeIdentifier("define-function"), - treeNamed("variant", switch (defineFunctionInstruction.variant()) { - case EFFECT_HANDLER_METHOD -> treeString("effect-handler-method"); - case FUNCTION -> treeString("function"); - }), - treeNamed("name", treeString(defineFunctionInstruction.name())), - treeNamed("positional-params", treeList( - defineFunctionInstruction.positionalParams().stream() - .map(param -> treeString(param.name())) - .toList() - )), - treeNamed("named-params", treeList( - defineFunctionInstruction.namedParams().stream() - .map(param -> treeString(param.name())) - .toList() - )), - treeNamed("free-variables", treeList( - defineFunctionInstruction.freeVariables().stream() - .map(freeVariable -> treeString(freeVariable.name())) - .toList() - )), - treeNamed("body", instructionsToTree(defineFunctionInstruction.body())) - ); - - case ShidDefineStruct defineStructInstruction -> - treeListIndented( - treeIdentifier("define-struct"), - treeNamed("type", structTypeToTree(defineStructInstruction.type())), - treeNamed("fields", treeList( - defineStructInstruction.fields().stream() - .map(field -> treeString(field.name())) - .toList() - )) - ); - - case ShidDiscard discardInstruction -> - treeList( - treeIdentifier("discard") - ); - - case ShidDuplicate duplicate -> - treeList( - treeIdentifier("duplicate") - ); - - case ShidFieldLoad fieldLoad -> - treeList( - treeIdentifier("field-load"), - switch (fieldLoad.receiver()) { - case ShidFieldLoad.Receiver.Effect effect -> - treeList( - treeIdentifier("effect"), - userDefinedEffectToTree(effect.effect()) - ); - - case ShidFieldLoad.Receiver.Namespace namespace -> - treeList( - treeIdentifier("namespace"), - treeString(namespace.name().toString()) - ); - - case ShidFieldLoad.Receiver.Struct struct -> - treeList( - treeIdentifier("struct"), - structTypeToTree(struct.type()) - ); - }, - treeString(fieldLoad.fieldName()) - ); - - case ShidIf if_ -> - treeListIndented( - treeIdentifier("if"), - treeNamed("result-count", treeInt(if_.resultCount())), - instructionsToTree(if_.trueBody()), - instructionsToTree(if_.falseBody()) - ); - - case ShidIntAdd intAdd -> - treeList(treeIdentifier("int-add")); - - case ShidIntEquals intEquals -> - treeList(treeIdentifier("int-eq")); - - case ShidIntNotEqual intNotEqual -> - treeList(treeIdentifier("int-ne")); - - case ShidIntSub intSub -> - treeList(treeIdentifier("int-sub")); - - case ShidLocalLoad localLoadInstruction -> - treeList( - treeIdentifier("local-load"), - treeString(localLoadInstruction.name()) - ); - - case ShidLocalStore localStoreInstruction -> - treeList( - treeIdentifier("local-store"), - treeString(localStoreInstruction.name()) - ); - - case ShidLoop loop -> - treeListIndented( - treeIdentifier("loop"), - instructionsToTree(loop.body()) - ); - - case ShidLoopBreak loopBreak -> - treeList(treeIdentifier("break")); - - case ShidLoopContinue loopContinue -> - treeList(treeIdentifier("continue")); - - case ShidNamespaceFieldStore namespaceFieldStore -> - treeList( - treeIdentifier("namespace-field-store"), - treeString(namespaceFieldStore.namespaceName().toString()), - treeString(namespaceFieldStore.fieldName()) - ); - - case ShidNamespaceImport namespaceImport -> - treeList( - treeIdentifier("namespace-import"), - treeString(namespaceImport.namespaceName().toString()) - ); - - case ShidPushBool pushBoolInstruction -> - treeList( - treeIdentifier("push-bool"), - treeIdentifier(Boolean.toString(pushBoolInstruction.value())) - ); - - case ShidPushInt pushIntInstruction -> - treeList( - treeIdentifier("push-int"), - treeIdentifier(pushIntInstruction.value().toString()) - ); - - case ShidPushString pushStringInstruction -> - treeList( - treeIdentifier("push-string"), - treeString(pushStringInstruction.value()) - ); - - case ShidPushUnit pushUnitInstruction -> - treeList( - treeIdentifier("push-unit") - ); - - case ShidResume resumeInstruction -> - treeList( - treeIdentifier("resume") - ); - - case ShidReturn returnInstruction -> - treeList( - treeIdentifier("return") - ); - - case ShidStringAdd stringAddInstruction -> - treeList(treeIdentifier("string-add")); - - case ShidSwitch switchInstruction -> - treeListIndented( - treeIdentifier("switch"), - treeListIndented( - switchInstruction.cases().stream() - .map(switchCase -> treeListIndented( - structTypeToTree(switchCase.caseType()), - instructionsToTree(switchCase.body()) - )) - .toList() - ) - ); - - case ShidTry tryInstruction -> - treeListIndented( - treeIdentifier("try"), - userDefinedEffectToTree(tryInstruction.handledEffect()), - treeListIndented( - tryInstruction.handlerMethodNames().stream() - .map(name -> treeString(name)) - .toList() - ), - instructionsToTree(tryInstruction.body()) - ); - - case ShidTupleCreate tupleCreateInstruction -> - treeList( - treeIdentifier("tuple-create"), - treeInt(tupleCreateInstruction.elementCount()) - ); - - case ShidTupleGet tupleGetInstruction -> - treeList( - treeIdentifier("tuple-get"), - treeInt(tupleGetInstruction.elementIndex()), - treeInt(tupleGetInstruction.elementCount()) - ); - - case ShidVarLoad varLoadInstruction -> - treeList( - treeIdentifier("var-load"), - treeString(varLoadInstruction.name()) - ); - - case ShidVarStore varStoreInstruction -> - treeList( - treeIdentifier("var-store"), - treeString(varStoreInstruction.name()) - ); - }; - } - - private static Tree userDefinedEffectToTree(UserDefinedEffect effect) { - return treeString(effect.describe()); - } - - private static Tree structTypeToTree(StructType type) { - return treeString(type.describe()); - } -} diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java new file mode 100644 index 00000000..791eb64c --- /dev/null +++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java @@ -0,0 +1,284 @@ +package org.shedlang.compiler.shid; + +import org.shedlang.compiler.shid.instructions.*; +import org.shedlang.compiler.treetext.Tree; +import org.shedlang.compiler.types.StructType; +import org.shedlang.compiler.types.UserDefinedEffect; + +import java.util.List; + +import static org.shedlang.compiler.treetext.TreeSerializer.serializeTree; +import static org.shedlang.compiler.treetext.Trees.*; + +public class ShidSerializer { + private ShidSerializer() { + } + + public static void serializeInstructions(List instructions, StringBuilder builder) { + var tree = instructionsToTree(instructions); + serializeTree(tree, builder, 0); + } + + public static void serializeInstruction(ShidInstruction instruction, StringBuilder builder) { + var tree = instructionToTree(instruction); + serializeTree(tree, builder, 0); + } + + private static Tree instructionsToTree(List instructions) { + return treeListIndented( + instructions.stream() + .map(instruction -> instructionToTree(instruction)) + .toList() + ); + } + + private static Tree instructionToTree(ShidInstruction instruction) { + return switch (instruction) { + case ShidCall callInstruction -> + treeList( + treeIdentifier("call"), + treeNamed( + "positional-arg-count", + treeInt(callInstruction.positionalArgCount()) + ), + treeNamed( + "named-arg-names", + treeList( + callInstruction.namedArgNames().stream() + .map(arg -> treeString(arg)) + .toList() + ) + ) + ); + + case ShidDefineEffect defineEffectInstruction -> + treeList( + treeIdentifier("define-effect"), + userDefinedEffectToTree(defineEffectInstruction.effect()) + ); + + case ShidDefineFunction defineFunctionInstruction -> + treeListIndented( + treeIdentifier("define-function"), + treeNamed("variant", switch (defineFunctionInstruction.variant()) { + case EFFECT_HANDLER_METHOD -> treeString("effect-handler-method"); + case FUNCTION -> treeString("function"); + }), + treeNamed("name", treeString(defineFunctionInstruction.name())), + treeNamed("positional-params", treeList( + defineFunctionInstruction.positionalParams().stream() + .map(param -> treeString(param.name())) + .toList() + )), + treeNamed("named-params", treeList( + defineFunctionInstruction.namedParams().stream() + .map(param -> treeString(param.name())) + .toList() + )), + treeNamed("free-variables", treeList( + defineFunctionInstruction.freeVariables().stream() + .map(freeVariable -> treeString(freeVariable.name())) + .toList() + )), + treeNamed("body", instructionsToTree(defineFunctionInstruction.body())) + ); + + case ShidDefineStruct defineStructInstruction -> + treeListIndented( + treeIdentifier("define-struct"), + treeNamed("type", structTypeToTree(defineStructInstruction.type())), + treeNamed("fields", treeList( + defineStructInstruction.fields().stream() + .map(field -> treeString(field.name())) + .toList() + )) + ); + + case ShidDiscard discardInstruction -> + treeList( + treeIdentifier("discard") + ); + + case ShidDuplicate duplicate -> + treeList( + treeIdentifier("duplicate") + ); + + case ShidFieldLoad fieldLoad -> + treeList( + treeIdentifier("field-load"), + switch (fieldLoad.receiver()) { + case ShidFieldLoad.Receiver.Effect effect -> + treeList( + treeIdentifier("effect"), + userDefinedEffectToTree(effect.effect()) + ); + + case ShidFieldLoad.Receiver.Namespace namespace -> + treeList( + treeIdentifier("namespace"), + treeString(namespace.name().toString()) + ); + + case ShidFieldLoad.Receiver.Struct struct -> + treeList( + treeIdentifier("struct"), + structTypeToTree(struct.type()) + ); + }, + treeString(fieldLoad.fieldName()) + ); + + case ShidIf if_ -> + treeListIndented( + treeIdentifier("if"), + treeNamed("result-count", treeInt(if_.resultCount())), + instructionsToTree(if_.trueBody()), + instructionsToTree(if_.falseBody()) + ); + + case ShidIntAdd intAdd -> + treeList(treeIdentifier("int-add")); + + case ShidIntEquals intEquals -> + treeList(treeIdentifier("int-eq")); + + case ShidIntNotEqual intNotEqual -> + treeList(treeIdentifier("int-ne")); + + case ShidIntSub intSub -> + treeList(treeIdentifier("int-sub")); + + case ShidLocalLoad localLoadInstruction -> + treeList( + treeIdentifier("local-load"), + treeString(localLoadInstruction.name()) + ); + + case ShidLocalStore localStoreInstruction -> + treeList( + treeIdentifier("local-store"), + treeString(localStoreInstruction.name()) + ); + + case ShidLoop loop -> + treeListIndented( + treeIdentifier("loop"), + instructionsToTree(loop.body()) + ); + + case ShidLoopBreak loopBreak -> + treeList(treeIdentifier("break")); + + case ShidLoopContinue loopContinue -> + treeList(treeIdentifier("continue")); + + case ShidNamespaceFieldStore namespaceFieldStore -> + treeList( + treeIdentifier("namespace-field-store"), + treeString(namespaceFieldStore.namespaceName().toString()), + treeString(namespaceFieldStore.fieldName()) + ); + + case ShidNamespaceImport namespaceImport -> + treeList( + treeIdentifier("namespace-import"), + treeString(namespaceImport.namespaceName().toString()) + ); + + case ShidPushBool pushBoolInstruction -> + treeList( + treeIdentifier("push-bool"), + treeIdentifier(Boolean.toString(pushBoolInstruction.value())) + ); + + case ShidPushInt pushIntInstruction -> + treeList( + treeIdentifier("push-int"), + treeIdentifier(pushIntInstruction.value().toString()) + ); + + case ShidPushString pushStringInstruction -> + treeList( + treeIdentifier("push-string"), + treeString(pushStringInstruction.value()) + ); + + case ShidPushUnit pushUnitInstruction -> + treeList( + treeIdentifier("push-unit") + ); + + case ShidResume resumeInstruction -> + treeList( + treeIdentifier("resume") + ); + + case ShidReturn returnInstruction -> + treeList( + treeIdentifier("return") + ); + + case ShidStringAdd stringAddInstruction -> + treeList(treeIdentifier("string-add")); + + case ShidSwitch switchInstruction -> + treeListIndented( + treeIdentifier("switch"), + treeListIndented( + switchInstruction.cases().stream() + .map(switchCase -> treeListIndented( + structTypeToTree(switchCase.caseType()), + instructionsToTree(switchCase.body()) + )) + .toList() + ) + ); + + case ShidTry tryInstruction -> + treeListIndented( + treeIdentifier("try"), + userDefinedEffectToTree(tryInstruction.handledEffect()), + treeListIndented( + tryInstruction.handlerMethodNames().stream() + .map(name -> treeString(name)) + .toList() + ), + instructionsToTree(tryInstruction.body()) + ); + + case ShidTupleCreate tupleCreateInstruction -> + treeList( + treeIdentifier("tuple-create"), + treeInt(tupleCreateInstruction.elementCount()) + ); + + case ShidTupleGet tupleGetInstruction -> + treeList( + treeIdentifier("tuple-get"), + treeInt(tupleGetInstruction.elementIndex()), + treeInt(tupleGetInstruction.elementCount()) + ); + + case ShidVarLoad varLoadInstruction -> + treeList( + treeIdentifier("var-load"), + treeString(varLoadInstruction.name()) + ); + + case ShidVarStore varStoreInstruction -> + treeList( + treeIdentifier("var-store"), + treeString(varStoreInstruction.name()) + ); + }; + } + + private static Tree userDefinedEffectToTree(UserDefinedEffect effect) { + return treeString(effect.describe()); + } + + private static Tree structTypeToTree(StructType type) { + return treeString(type.describe()); + } +} -- cgit v1.2.3