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

Fix var blocks creating an empty type named var #2173

Merged
merged 2 commits into from
Jan 18, 2025
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
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
Loading