Skip to content

Commit

Permalink
Fix empty braced object blocks and add null object statements (#2175)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Jan 18, 2025
1 parent 487a30c commit 75b5cf2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions DMCompiler/Compiler/DM/AST/DMAST.ObjectStatements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public abstract class DMASTStatement(Location location) : DMASTNode(location);
/// <remarks>Emit an error code before creating!</remarks>
public sealed class DMASTInvalidStatement(Location location) : DMASTStatement(location);

/// Lone semicolon, statement containing nothing
public sealed class DMASTNullStatement(Location location) : DMASTStatement(location);

public sealed class DMASTObjectDefinition(Location location, DreamPath path, DMASTBlockInner? innerBlock)
: DMASTStatement(location) {
/// <summary> Unlike other Path variables stored by AST nodes, this path is guaranteed to be the real, absolute path of this object definition block. <br/>
Expand Down
7 changes: 6 additions & 1 deletion DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ public DMASTFile File() {
protected DMASTStatement? Statement() {
var loc = CurrentLoc;

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 DMASTNullStatement(loc);
}

DMASTPath? path = Path();
if (path is null)
return null;
Expand Down Expand Up @@ -536,7 +541,7 @@ public DMASTFile File() {
Newline();
Consume(TokenType.DM_RightCurlyBracket, "Expected '}'");

return new DMASTBlockInner(loc, blockInner.ToArray());
return new DMASTBlockInner(loc, blockInner?.ToArray() ?? []);
}

return null;
Expand Down
5 changes: 3 additions & 2 deletions DMCompiler/DM/Builders/DMCodeTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ private void ProcessStatement(DMASTStatement statement, DreamPath currentType) {
}

switch (statement) {
case DMASTInvalidStatement:
break; // An error should have been emitted by whatever made this
case DMASTInvalidStatement: // An error should have been emitted by whatever made this
case DMASTNullStatement:
break;
case DMASTObjectDefinition objectDefinition:
CodeTree.AddType(objectDefinition.Path);
if (objectDefinition.InnerBlock != null)
Expand Down

0 comments on commit 75b5cf2

Please sign in to comment.