Skip to content

Commit 1dc4a7f

Browse files
ike709ike709
andauthored
Bring parity to initial() (#2458)
Co-authored-by: ike709 <[email protected]>
1 parent 00b46c3 commit 1dc4a7f

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// COMPILE ERROR OD0011
2+
/proc/RunTest()
3+
initial(global)

Content.Tests/DMProject/Tests/SpecialProcs/initial/initial_proc_args.dm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
ASSERT(initial(b) == 5)
1111
ASSERT(initial(c) == "e")
12+
ASSERT(initial(args[2]) == 5)
1213

1314
/proc/RunTest()
1415
someargs("3", 5, c = "cc")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/proc/RunTest()
2+
. = "foo"
3+
ASSERT(initial(.) == "foo")

DMCompiler/DM/Expressions/LValue.cs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public void EmitPushValueNoConstant(ExpressionContext ctx) {
2525
ctx.Proc.AddLabel(endLabel);
2626
}
2727

28-
public virtual void EmitPushInitial(ExpressionContext ctx) {
29-
ctx.Compiler.Emit(WarningCode.BadExpression, Location, $"Can't get initial value of {this}");
30-
ctx.Proc.PushNullAndError();
31-
}
28+
public abstract void EmitPushInitial(ExpressionContext ctx);
3229
}
3330

3431
/// <summary>
@@ -42,6 +39,11 @@ public override void EmitPushValue(ExpressionContext ctx) {
4239
ctx.Proc.PushString("Encountered a bad LValue (compiler bug!)");
4340
ctx.Proc.Throw();
4441
}
42+
43+
public override void EmitPushInitial(ExpressionContext ctx) {
44+
ctx.Compiler.Emit(WarningCode.BadExpression, Location, "Can't get initial value of a bad LValue (compiler bug!)");
45+
ctx.Proc.Throw();
46+
}
4547
}
4648

4749
// global
@@ -51,6 +53,11 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
5153
ctx.Compiler.Emit(WarningCode.BadExpression, Location, "attempt to use `global` as a reference");
5254
return DMReference.Invalid;
5355
}
56+
57+
public override void EmitPushInitial(ExpressionContext ctx) {
58+
ctx.Compiler.Emit(WarningCode.BadExpression, Location, "Can't get initial value of `global`");
59+
ctx.Proc.PushNullAndError();
60+
}
5461
}
5562

5663
// src
@@ -62,6 +69,11 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
6269
return DMReference.Src;
6370
}
6471

72+
public override void EmitPushInitial(ExpressionContext ctx) {
73+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on `src` returns the current value");
74+
EmitPushValue(ctx);
75+
}
76+
6577
public override string GetNameof(ExpressionContext ctx) => "src";
6678
}
6779

@@ -75,6 +87,11 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
7587
return DMReference.Usr;
7688
}
7789

90+
public override void EmitPushInitial(ExpressionContext ctx) {
91+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on `usr` returns the current value");
92+
EmitPushValue(ctx);
93+
}
94+
7895
public override string GetNameof(ExpressionContext ctx) => "usr";
7996
}
8097

@@ -85,6 +102,11 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
85102
return DMReference.Args;
86103
}
87104

105+
public override void EmitPushInitial(ExpressionContext ctx) {
106+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on `args` returns the current value");
107+
EmitPushValue(ctx);
108+
}
109+
88110
public override string GetNameof(ExpressionContext ctx) => "args";
89111
}
90112

@@ -95,6 +117,12 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
95117
return DMReference.Callee;
96118
}
97119

120+
public override void EmitPushInitial(ExpressionContext ctx) {
121+
// This happens silently in BYOND
122+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on constant var `callee` is pointless");
123+
EmitPushValue(ctx);
124+
}
125+
98126
public override string GetNameof(ExpressionContext ctx) => "callee";
99127
}
100128

@@ -105,6 +133,12 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
105133
return DMReference.Caller;
106134
}
107135

136+
public override void EmitPushInitial(ExpressionContext ctx) {
137+
// This happens silently in BYOND
138+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on constant var `caller` is pointless");
139+
EmitPushValue(ctx);
140+
}
141+
108142
public override string GetNameof(ExpressionContext ctx) => "caller";
109143
}
110144

@@ -115,6 +149,12 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
115149
return DMReference.World;
116150
}
117151

152+
public override void EmitPushInitial(ExpressionContext ctx) {
153+
// This happens silently in BYOND
154+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on constant var `world` is pointless");
155+
EmitPushValue(ctx);
156+
}
157+
118158
public override string GetNameof(ExpressionContext ctx) => "world";
119159
}
120160

@@ -162,6 +202,11 @@ public override void EmitPushInitial(ExpressionContext ctx) {
162202
ctx.Proc.PushReferenceValue(DMReference.Src);
163203
ctx.Proc.PushString(variable.Name);
164204
ctx.Proc.Initial();
205+
206+
if (variable.IsConst) {
207+
// This happens silently in BYOND
208+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on a constant variable is pointless");
209+
}
165210
}
166211

167212
public void EmitPushIsSaved(DMProc proc) {
@@ -220,5 +265,11 @@ public override void EmitPushValue(ExpressionContext ctx) {
220265
ctx.Proc.PushGlobalVars();
221266
}
222267

268+
public override void EmitPushInitial(ExpressionContext ctx) {
269+
// This happens silently in BYOND
270+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on `global.vars` returns the current value");
271+
EmitPushValue(ctx);
272+
}
273+
223274
public override string GetNameof(ExpressionContext ctx) => "vars";
224275
}

DMCompiler/DM/Expressions/Procs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public override DMReference EmitReference(ExpressionContext ctx, string endLabel
6464
ShortCircuitMode shortCircuitMode = ShortCircuitMode.KeepNull) {
6565
return DMReference.Self;
6666
}
67+
68+
public override void EmitPushInitial(ExpressionContext ctx) {
69+
ctx.Compiler.Emit(WarningCode.PointlessBuiltinCall, Location, "calling initial() on `.` returns the current value");
70+
EmitPushValue(ctx);
71+
}
6772
}
6873

6974
// ..

0 commit comments

Comments
 (0)