Skip to content

Commit

Permalink
Add "null statements" to the compiler
Browse files Browse the repository at this point in the history
Just a lone semicolon that represents a statement with nothing in it. Allows for suppressing EmptyBlock emissions.
  • Loading branch information
wixoaGit committed Feb 12, 2024
1 parent 011bace commit 92156f4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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

0 comments on commit 92156f4

Please sign in to comment.