From cf0865661f6a6f5f66e3bc76730f6cd9a4a9023c Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 1 Jun 2024 14:34:19 -0700 Subject: [PATCH 1/6] Pointer Parsing --- .../Compiler/DM/AST/DMAST.ExpressionUnary.cs | 2 ++ DMCompiler/Compiler/DM/DMParser.cs | 6 +++- DMCompiler/DM/Builders/DMExpressionBuilder.cs | 4 +++ DMCompiler/DM/Expressions/Unary.cs | 28 +++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/DMCompiler/Compiler/DM/AST/DMAST.ExpressionUnary.cs b/DMCompiler/Compiler/DM/AST/DMAST.ExpressionUnary.cs index 8cbb8183b5..8060c9e657 100644 --- a/DMCompiler/Compiler/DM/AST/DMAST.ExpressionUnary.cs +++ b/DMCompiler/Compiler/DM/AST/DMAST.ExpressionUnary.cs @@ -17,6 +17,8 @@ public sealed class DMASTPreIncrement(Location location, DMASTExpression express public sealed class DMASTPreDecrement(Location location, DMASTExpression expression) : DMASTUnary(location, expression); public sealed class DMASTPostIncrement(Location location, DMASTExpression expression) : DMASTUnary(location, expression); public sealed class DMASTPostDecrement(Location location, DMASTExpression expression) : DMASTUnary(location, expression); +public sealed class DMASTPointerRef(Location location, DMASTExpression expression) : DMASTUnary(location, expression); +public sealed class DMASTPointerDeref(Location location, DMASTExpression expression) : DMASTUnary(location, expression); public sealed class DMASTSin(Location location, DMASTExpression expression) : DMASTUnary(location, expression); public sealed class DMASTCos(Location location, DMASTExpression expression) : DMASTUnary(location, expression); public sealed class DMASTTan(Location location, DMASTExpression expression) : DMASTUnary(location, expression); diff --git a/DMCompiler/Compiler/DM/DMParser.cs b/DMCompiler/Compiler/DM/DMParser.cs index ba472ee5fe..f31e2a4397 100644 --- a/DMCompiler/Compiler/DM/DMParser.cs +++ b/DMCompiler/Compiler/DM/DMParser.cs @@ -2060,7 +2060,9 @@ private void ExpressionTo(out DMASTExpression endRange, out DMASTExpression? ste TokenType.DM_Exclamation, TokenType.DM_Tilde, TokenType.DM_PlusPlus, - TokenType.DM_MinusMinus + TokenType.DM_MinusMinus, + TokenType.DM_And, + TokenType.DM_Star }, out var unaryToken)) { Whitespace(); DMASTExpression? expression = ExpressionUnary(isTernaryB); @@ -2071,6 +2073,8 @@ private void ExpressionTo(out DMASTExpression endRange, out DMASTExpression? ste case TokenType.DM_Tilde: return new DMASTBinaryNot(loc, expression); case TokenType.DM_PlusPlus: return new DMASTPreIncrement(loc, expression); case TokenType.DM_MinusMinus: return new DMASTPreDecrement(loc, expression); + case TokenType.DM_And: return new DMASTPointerRef(loc, expression); + case TokenType.DM_Star: return new DMASTPointerDeref(loc, expression); } Emit(WarningCode.BadToken, loc, $"Problem while handling unary '{unaryToken.PrintableText}'"); diff --git a/DMCompiler/DM/Builders/DMExpressionBuilder.cs b/DMCompiler/DM/Builders/DMExpressionBuilder.cs index 502f1a3c7e..95a3a8e5ea 100644 --- a/DMCompiler/DM/Builders/DMExpressionBuilder.cs +++ b/DMCompiler/DM/Builders/DMExpressionBuilder.cs @@ -227,6 +227,10 @@ public static DMExpression BuildExpression(DMASTExpression expression, DMObject return new PreDecrement(preDecrement.Location, BuildExpression(preDecrement.Value, dmObject, proc, inferredPath)); case DMASTPostDecrement postDecrement: return new PostDecrement(postDecrement.Location, BuildExpression(postDecrement.Value, dmObject, proc, inferredPath)); + case DMASTPointerRef pointerRef: + return new PointerRef(pointerRef.Location, BuildExpression(pointerRef.Value, dmObject, proc, inferredPath)); + case DMASTPointerDeref pointerDeref: + return new PointerDeref(pointerDeref.Location, BuildExpression(pointerDeref.Value, dmObject, proc, inferredPath)); case DMASTGradient gradient: return new Gradient(gradient.Location, new ArgumentList(gradient.Location, dmObject, proc, gradient.Parameters)); diff --git a/DMCompiler/DM/Expressions/Unary.cs b/DMCompiler/DM/Expressions/Unary.cs index 98e691a7f3..526ee77aa8 100644 --- a/DMCompiler/DM/Expressions/Unary.cs +++ b/DMCompiler/DM/Expressions/Unary.cs @@ -123,4 +123,32 @@ protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference refer proc.Decrement(reference); } } + + // &x + internal sealed class PointerRef(Location location, DMExpression expr) : AssignmentUnaryOp(location, expr) { + protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference reference, string endLabel) { } + + public override void EmitPushValue(DMObject dmObject, DMProc proc) { + Expr.EmitPushValue(dmObject, proc); + DMCompiler.UnimplementedWarning(location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); + } + + public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode) { + return Expr.EmitReference(dmObject, proc, endLabel, shortCircuitMode); + } + } + + // *x + internal sealed class PointerDeref(Location location, DMExpression expr) : AssignmentUnaryOp(location, expr) { + protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference reference, string endLabel) { } + + public override void EmitPushValue(DMObject dmObject, DMProc proc) { + Expr.EmitPushValue(dmObject, proc); + DMCompiler.UnimplementedWarning(location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); + } + + public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode) { + return Expr.EmitReference(dmObject, proc, endLabel, shortCircuitMode); + } + } } From 01c5e601004068a5557b96e43be0c5c0b49a7d8c Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 1 Jun 2024 23:38:02 -0700 Subject: [PATCH 2/6] Update DMCompiler/DM/Expressions/Unary.cs Co-authored-by: wixoa --- DMCompiler/DM/Expressions/Unary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/DM/Expressions/Unary.cs b/DMCompiler/DM/Expressions/Unary.cs index 526ee77aa8..8cdfa6dad5 100644 --- a/DMCompiler/DM/Expressions/Unary.cs +++ b/DMCompiler/DM/Expressions/Unary.cs @@ -130,7 +130,7 @@ protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference refer public override void EmitPushValue(DMObject dmObject, DMProc proc) { Expr.EmitPushValue(dmObject, proc); - DMCompiler.UnimplementedWarning(location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); + DMCompiler.UnimplementedWarning(Location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); } public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode) { From a9bb7a91ed22cd35b16ac82471a9f1f8fa329d42 Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 1 Jun 2024 23:38:07 -0700 Subject: [PATCH 3/6] Update DMCompiler/DM/Expressions/Unary.cs Co-authored-by: wixoa --- DMCompiler/DM/Expressions/Unary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/DM/Expressions/Unary.cs b/DMCompiler/DM/Expressions/Unary.cs index 8cdfa6dad5..15697ba203 100644 --- a/DMCompiler/DM/Expressions/Unary.cs +++ b/DMCompiler/DM/Expressions/Unary.cs @@ -133,7 +133,7 @@ public override void EmitPushValue(DMObject dmObject, DMProc proc) { DMCompiler.UnimplementedWarning(Location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); } - public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode) { + public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode = ShortCircuitMode.KeepNull) { return Expr.EmitReference(dmObject, proc, endLabel, shortCircuitMode); } } From 8eca74235e5e8ec614575da918c015fe15561fca Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 1 Jun 2024 23:38:14 -0700 Subject: [PATCH 4/6] Update DMCompiler/DM/Expressions/Unary.cs Co-authored-by: wixoa --- DMCompiler/DM/Expressions/Unary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/DM/Expressions/Unary.cs b/DMCompiler/DM/Expressions/Unary.cs index 15697ba203..e0b9d38ef7 100644 --- a/DMCompiler/DM/Expressions/Unary.cs +++ b/DMCompiler/DM/Expressions/Unary.cs @@ -147,7 +147,7 @@ public override void EmitPushValue(DMObject dmObject, DMProc proc) { DMCompiler.UnimplementedWarning(location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); } - public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode) { + public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode = ShortCircuitMode.KeepNull) { return Expr.EmitReference(dmObject, proc, endLabel, shortCircuitMode); } } From facc90386326b2edb9db8ce6794f28ad02a07891 Mon Sep 17 00:00:00 2001 From: ike709 Date: Sat, 1 Jun 2024 23:38:23 -0700 Subject: [PATCH 5/6] Update DMCompiler/DM/Expressions/Unary.cs Co-authored-by: wixoa --- DMCompiler/DM/Expressions/Unary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DMCompiler/DM/Expressions/Unary.cs b/DMCompiler/DM/Expressions/Unary.cs index e0b9d38ef7..9d7291021e 100644 --- a/DMCompiler/DM/Expressions/Unary.cs +++ b/DMCompiler/DM/Expressions/Unary.cs @@ -144,7 +144,7 @@ protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference refer public override void EmitPushValue(DMObject dmObject, DMProc proc) { Expr.EmitPushValue(dmObject, proc); - DMCompiler.UnimplementedWarning(location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); + DMCompiler.UnimplementedWarning(Location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); } public override DMReference EmitReference(DMObject dmObject, DMProc proc, string endLabel, ShortCircuitMode shortCircuitMode = ShortCircuitMode.KeepNull) { From 9de7134a64753adefa2e53432ac8d55fc655edb9 Mon Sep 17 00:00:00 2001 From: ike709 Date: Sun, 2 Jun 2024 11:15:35 -0700 Subject: [PATCH 6/6] review --- DMCompiler/DM/Expressions/Unary.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/DMCompiler/DM/Expressions/Unary.cs b/DMCompiler/DM/Expressions/Unary.cs index 9d7291021e..ebc347e3b7 100644 --- a/DMCompiler/DM/Expressions/Unary.cs +++ b/DMCompiler/DM/Expressions/Unary.cs @@ -125,9 +125,7 @@ protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference refer } // &x - internal sealed class PointerRef(Location location, DMExpression expr) : AssignmentUnaryOp(location, expr) { - protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference reference, string endLabel) { } - + internal sealed class PointerRef(Location location, DMExpression expr) : UnaryOp(location, expr) { public override void EmitPushValue(DMObject dmObject, DMProc proc) { Expr.EmitPushValue(dmObject, proc); DMCompiler.UnimplementedWarning(Location, "Pointers are currently unimplemented and identifiers will be treated as normal variables."); @@ -139,9 +137,7 @@ public override DMReference EmitReference(DMObject dmObject, DMProc proc, string } // *x - internal sealed class PointerDeref(Location location, DMExpression expr) : AssignmentUnaryOp(location, expr) { - protected override void EmitOp(DMObject dmObject, DMProc proc, DMReference reference, string endLabel) { } - + internal sealed class PointerDeref(Location location, DMExpression expr) : UnaryOp(location, expr) { public override void EmitPushValue(DMObject dmObject, DMProc proc) { Expr.EmitPushValue(dmObject, proc); DMCompiler.UnimplementedWarning(Location, "Pointers are currently unimplemented and identifiers will be treated as normal variables.");