Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "null statements" to the compiler #1670

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Content.Tests/DMProject/Tests/Statements/EmptyBlock.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// COMPILE ERROR
#pragma EmptyBlock error

/proc/RunTest()
if (TRUE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma EmptyBlock error

/proc/RunTest()
if (TRUE)
;
12 changes: 12 additions & 0 deletions DMCompiler/Compiler/DM/DMAST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public void VisitObjectVarOverride(DMASTObjectVarOverride objectVarOverride) {
throw new NotImplementedException();
}

public void VisitNullProcStatement(DMASTNullProcStatement nullProcStatement) {
throw new NotImplementedException();
}

public void VisitProcStatementExpression(DMASTProcStatementExpression statementExpression) {
throw new NotImplementedException();
}
Expand Down Expand Up @@ -772,6 +776,14 @@ public override void Visit(DMASTVisitor visitor) {
}
}

/// Lone semicolon, analogous to null statements in C.
/// Main purpose is to suppress EmptyBlock emissions.
public sealed class DMASTNullProcStatement(Location location) : DMASTProcStatement(location) {
public override void Visit(DMASTVisitor visitor) {
visitor.VisitNullProcStatement(this);
}
}

public sealed class DMASTProcStatementExpression : DMASTProcStatement {
public DMASTExpression Expression;

Expand Down
7 changes: 7 additions & 0 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public DMASTFile File() {

public DMASTStatement? Statement(bool requireDelimiter = true) {
var loc = Current().Location;

DMASTPath? path = Path();
if (path is null)
return null;
Expand Down Expand Up @@ -618,6 +619,12 @@ public DMASTFile File() {

public DMASTProcStatement? ProcStatement() {
var loc = Current().Location;

if (Current().Type == TokenType.DM_Semicolon) { // A lone semicolon creates a "null statement" (like C)
// Note that we do not consume the semicolon here
return new DMASTNullProcStatement(loc);
}

var leadingColon = Check(TokenType.DM_Colon);

DMASTExpression? expression = null;
Expand Down
2 changes: 2 additions & 0 deletions DMCompiler/DM/Visitors/DMASTSimplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void VisitProcBlockInner(DMASTProcBlockInner procBlockInner) {
}
}

public void VisitNullProcStatement(DMASTNullProcStatement nullProcStatement) { }

public void VisitProcStatementExpression(DMASTProcStatementExpression statementExpression) {
SimplifyExpression(ref statementExpression.Expression);
}
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DM/Visitors/DMProcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void ProcessBlockInner(DMASTProcBlockInner block, bool silenceEmptyBlock

public void ProcessStatement(DMASTProcStatement statement) {
switch (statement) {
case DMASTNullProcStatement: break;
case DMASTProcStatementExpression statementExpression: ProcessStatementExpression(statementExpression); break;
case DMASTProcStatementContinue statementContinue: ProcessStatementContinue(statementContinue); break;
case DMASTProcStatementGoto statementGoto: ProcessStatementGoto(statementGoto); break;
Expand Down
Loading