diff options
Diffstat (limited to 'src/main/java')
6 files changed, 94 insertions, 5 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 47e7c496..120a78af 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.patio; import org.shedlang.compiler.patio.ast.PatioCompilationUnit; +import org.shedlang.compiler.patio.ast.instructions.PatioDataPtrToInt; import org.shedlang.compiler.patio.ast.instructions.PatioIntAdd; import org.shedlang.compiler.patio.ast.instructions.PatioLocalStore; import org.shedlang.compiler.patio.ast.instructions.PatioReturn; @@ -257,6 +258,34 @@ public class PatioCodeGen { case PushIntInstruction pushInt -> pushValue(pushInt.value().intValueExact(), pushInt.source(), context); + case PushStringInstruction pushString -> { + var stringPtrOperandResult = PatioCodeGenStrings.define( + pushString.value(), + pushString.source(), + context + ); + var stringPtrOperand = stringPtrOperandResult.value(); + context = stringPtrOperandResult.context(); + + var stringPtrResult = context.allocateLocal( + PatioCodeGenLocalSpace.STRING, + PatioCodeGenTypes.VALUE_TYPE + ); + var stringPtr = stringPtrResult.value(); + context = stringPtrResult.context(); + + context = context.addInstruction(new PatioDataPtrToInt( + stringPtr, + stringPtrOperand, + pushString.source() + )); + + context = context.temporaryPush(stringPtr); + + yield context; + } + + case PushUnitInstruction pushUnit -> pushValue(0, pushUnit.source(), context); diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenCompilationUnitElementSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenCompilationUnitElementSpace.java index 0db4e4de..4d18223e 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenCompilationUnitElementSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenCompilationUnitElementSpace.java @@ -1,7 +1,8 @@ package org.shedlang.compiler.patio; public enum PatioCodeGenCompilationUnitElementSpace { - FUNCTION("function"); + FUNCTION("function"), + STRING("string"); private final String name; diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java index 4fe625f3..f19f92f2 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java @@ -231,7 +231,18 @@ public record PatioCodeGenContext( ); } - public PatioCodeGenContext addDataElement(String name, PatioDataLayout layout, Source source) { + public PatioCodeGenResult<String> allocateCompilationUnitElementName( + PatioCodeGenCompilationUnitElementSpace space + ) { + return withCompilationUnitContextResult( + this.compilationUnitContext.allocateCompilationUnitName( + space, + Optional.empty() + ) + ); + } + + public PatioCodeGenContext addDataElementMutable(String name, PatioDataLayout layout, Source source) { var dataElement = new PatioDataElementMutable(name, layout, source); return addCompilationUnitElement(dataElement); } @@ -355,7 +366,7 @@ public record PatioCodeGenContext( return new PatioCodeGenContext(this.compilationUnitContext, result.context).toResult(result.value); } - private <T> PatioCodeGenResult<T> toResult(T value) { + public <T> PatioCodeGenResult<T> toResult(T value) { return new PatioCodeGenResult<>(value, this); } diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java index 0a8cac3e..1735c2a0 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java @@ -12,6 +12,7 @@ public enum PatioCodeGenLocalSpace { NAMESPACE_VALUE("namespaceValue"), LOCAL("local"), RECEIVER("receiver"), + STRING("string"), TEMPORARY("tmp"); private final String name; diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java index fbbd5c79..123bbf4a 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java @@ -37,13 +37,13 @@ public class PatioCodeGenNamespaces { var namespaceTypeInfo = context.namespaceTypeInfo(namespaceName); var layout = namespaceDataLayout(namespaceTypeInfo); - context = context.addDataElement( + context = context.addDataElementMutable( globalNameNamespaceValue(namespaceName), layout, source ); - context = context.addDataElement( + context = context.addDataElementMutable( globalNameNamespaceIsInited(namespaceName), PatioDataLayouts.scalar(PatioTypes.BOOL), source diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java new file mode 100644 index 00000000..a9b7c753 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenStrings.java @@ -0,0 +1,47 @@ +package org.shedlang.compiler.patio; + +import org.shedlang.compiler.patio.ast.PatioDataElementImmutable; +import org.shedlang.compiler.patio.ast.operands.PatioDataPtr; +import org.shedlang.compiler.patio.ast.types.PatioDataValue; +import org.shedlang.compiler.patio.ast.types.PatioDataValues; +import org.shedlang.compiler.sources.Source; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +public class PatioCodeGenStrings { + private PatioCodeGenStrings() {} + + public static PatioCodeGenResult<PatioDataPtr> define( + String value, + Source source, + PatioCodeGenContext context + ) { + var codeUnits = value.getBytes(StandardCharsets.UTF_8); + + var codeUnitsDataValues = new ArrayList<PatioDataValue>(); + for (var codeUnit : codeUnits) { + codeUnitsDataValues.add(PatioDataValues.byte_(codeUnit)); + } + + var dataValue = PatioDataValues.struct(List.of( + PatioDataValues.int_(codeUnits.length), + PatioDataValues.array(codeUnitsDataValues) + )); + + var elementNameResult = context.allocateCompilationUnitElementName( + PatioCodeGenCompilationUnitElementSpace.STRING + ); + var elementName = elementNameResult.value(); + context = elementNameResult.context(); + + context = context.addCompilationUnitElement(new PatioDataElementImmutable( + elementName, + dataValue, + source + )); + + return context.toResult(new PatioDataPtr(elementName)); + } +} |
