diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:29:08 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:29:08 +0100 |
| commit | 6820962c44cc7a27fe45d0a2ebe8140cd11f5811 (patch) | |
| tree | 19d8b4208605761f53eb8602a8d5b682e45902ef /src/main/java | |
| parent | bbd0ad7fad36761f55555347b01a32887fc4a5f9 (diff) | |
Add typed int add node
Diffstat (limited to 'src/main/java')
4 files changed, 44 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java new file mode 100644 index 00000000..383bd575 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java @@ -0,0 +1,16 @@ +package org.shedlang.compiler.ast.typed; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.Type; + +public record TypedBinaryOperationNode( + TypedBinaryOperator operator, + TypedExpressionNode left, + TypedExpressionNode right, + Source source +) implements TypedExpressionNode { + @Override + public Type type() { + return operator.resultType(); + } +} diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java new file mode 100644 index 00000000..5f6498f6 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java @@ -0,0 +1,18 @@ +package org.shedlang.compiler.ast.typed; + +import org.shedlang.compiler.types.Type; +import org.shedlang.compiler.types.Types; + +public enum TypedBinaryOperator { + INT_ADD(Types.INT); + + private final Type type; + + TypedBinaryOperator(Type type) { + this.type = type; + } + + public Type resultType() { + return type; + } +} diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java index 4bb9cb8e..963cf8a3 100644 --- a/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java @@ -4,6 +4,6 @@ import org.shedlang.compiler.types.Type; public sealed interface TypedExpressionNode extends TypedNode - permits TypedBoolLiteralNode, TypedCallNode, TypedIntLiteralNode, TypedReferenceNode, TypedStringLiteralNode { + permits TypedBinaryOperationNode, TypedBoolLiteralNode, TypedCallNode, TypedIntLiteralNode, TypedReferenceNode, TypedStringLiteralNode { Type type(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 9cd01151..c96b8cfd 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -12,6 +12,12 @@ public class StackCompiler { private StackCompiler() { } + private static PVector<Instruction> compileBinaryOperation(TypedBinaryOperationNode node) { + return compileExpression(node.left()) + .plusAll(compileExpression(node.right())) + .plus(new IntAddInstruction(node.source())); + } + private static PVector<Instruction> compileBoolLiteral(TypedBoolLiteralNode node) { return P.vector(new PushBoolInstruction(node.value(), node.source())); } @@ -42,6 +48,9 @@ public class StackCompiler { public static PVector<Instruction> compileExpression(TypedExpressionNode node) { return switch (node) { + case TypedBinaryOperationNode typedBinaryOperationNode -> + compileBinaryOperation(typedBinaryOperationNode); + case TypedBoolLiteralNode typedBoolLiteralNode -> compileBoolLiteral(typedBoolLiteralNode); |
