Skip to content

Commit

Permalink
Fix var blocks creating an empty type named var (#2173)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Jan 18, 2025
1 parent 0863030 commit 487a30c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions DMCompiler/Compiler/DM/AST/DMAST.ObjectStatements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace DMCompiler.Compiler.DM.AST;
/// </summary>
public abstract class DMASTStatement(Location location) : DMASTNode(location);

/// <summary>
/// Used when there was an error parsing a statement
/// </summary>
/// <remarks>Emit an error code before creating!</remarks>
public sealed class DMASTInvalidStatement(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
41 changes: 30 additions & 11 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,28 @@ public DMASTFile File() {
return new DMASTProcDefinition(loc, CurrentPath, parameters.ToArray(), procBlock, types);
}

//Object definition
if (Block() is { } block) {
Compiler.VerbosePrint($"Parsed object {CurrentPath}");
return new DMASTObjectDefinition(loc, CurrentPath, block);
}

//Var definition(s)
if (CurrentPath.FindElement("var") != -1) {
bool isIndented = false;
DreamPath varPath = CurrentPath;
List<DMASTObjectVarDefinition> varDefinitions = new();

var possibleNewline = Current();
if (Newline()) {
if (Check(TokenType.DM_Indent)) {
isIndented = true;
DMASTPath? newVarPath = Path();
if (newVarPath == null) {
Emit(WarningCode.InvalidVarDefinition, "Expected a var definition");
return new DMASTInvalidStatement(CurrentLoc);
}

varPath = CurrentPath.AddToPath(newVarPath.Path.PathString);
} else {
ReuseToken(possibleNewline);
}
}

while (true) {
Whitespace();

Expand All @@ -304,24 +315,26 @@ public DMASTFile File() {
var varDef = new DMASTObjectVarDefinition(loc, varPath, value, valType);

varDefinitions.Add(varDef);
if (Check(TokenType.DM_Comma)) {
if (Check(TokenType.DM_Comma) || (isIndented && Newline())) {
Whitespace();
DMASTPath? newVarPath = Path();

if (newVarPath == null) {
Emit(WarningCode.InvalidVarDefinition, "Expected a var definition");
break;
} else if (newVarPath.Path.Elements.Length > 1) { // TODO: This is valid DM
Emit(WarningCode.BadToken, newVarPath.Location, "Invalid var name");
break;
}

varPath = CurrentPath.AddToPath("../" + newVarPath.Path.PathString);
varPath = CurrentPath.AddToPath(
isIndented ? newVarPath.Path.PathString
: "../" + newVarPath.Path.PathString);
} else {
break;
}
}

if (isIndented)
Consume(TokenType.DM_Dedent, "Expected end of var block");

return (varDefinitions.Count == 1)
? varDefinitions[0]
: new DMASTMultipleObjectVarDefinitions(loc, varDefinitions.ToArray());
Expand All @@ -336,6 +349,12 @@ public DMASTFile File() {
return new DMASTObjectVarOverride(loc, CurrentPath, value);
}

//Object definition
if (Block() is { } block) {
Compiler.VerbosePrint($"Parsed object {CurrentPath}");
return new DMASTObjectDefinition(loc, CurrentPath, block);
}

//Empty object definition
Compiler.VerbosePrint($"Parsed object {CurrentPath} - empty");
return new DMASTObjectDefinition(loc, CurrentPath, null);
Expand Down
2 changes: 2 additions & 0 deletions DMCompiler/DM/Builders/DMCodeTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private void ProcessStatement(DMASTStatement statement, DreamPath currentType) {
}

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

0 comments on commit 487a30c

Please sign in to comment.