Skip to content

Commit

Permalink
Address reviews, other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Dec 20, 2023
1 parent f4dc0de commit 6702666
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
38 changes: 19 additions & 19 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

namespace DMCompiler.Compiler.DM {
public partial class DMParser : Parser<Token> {

private DreamPath _currentPath = DreamPath.Root;

private bool _allowVarDeclExpression = false;

public DMParser(DMLexer lexer) : base(lexer) {
}

private static readonly TokenType[] AssignTypes = {
TokenType.DM_Equals,
TokenType.DM_PlusEquals,
Expand Down Expand Up @@ -149,6 +144,9 @@ public DMParser(DMLexer lexer) : base(lexer) {
TokenType.DM_XorEquals,
};

public DMParser(DMLexer lexer) : base(lexer) {
}

public DMASTFile File() {
var loc = Current().Location;
List<DMASTStatement> statements = new();
Expand Down Expand Up @@ -1352,7 +1350,7 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() {
List<DMASTProcStatementSwitch.SwitchCase> switchCases = new();
DMASTProcStatementSwitch.SwitchCase? switchCase = SwitchCase();

while(switchCase is not null) {
while (switchCase is not null) {
switchCases.Add(switchCase);
Newline();
Whitespace();
Expand All @@ -1374,19 +1372,20 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() {

DMASTExpression? expression = Expression();
if (expression == null) {
if (expressions.Count == 0) {
Error(WarningCode.BadExpression, "Expected an expression");
} else {
//Eat a trailing comma if there's at least 1 expression
break;
}
if (expressions.Count == 0)
DMCompiler.Emit(WarningCode.BadExpression, Current().Location, "Expected an expression");

break;
}

if (Check(TokenType.DM_To)) {
var loc = Current().Location;
Whitespace();
var loc = Current().Location;
DMASTExpression? rangeEnd = Expression();
if (rangeEnd == null) Error(WarningCode.BadExpression, "Expected an upper limit");
if (rangeEnd == null) {
DMCompiler.Emit(WarningCode.BadExpression, loc, "Expected an upper limit");
rangeEnd = new DMASTConstantNull(loc); // Fallback to null
}

expressions.Add(new DMASTSwitchCaseRange(loc, expression, rangeEnd));
} else {
Expand All @@ -1402,24 +1401,25 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() {

if (body == null) {
DMASTProcStatement? statement = ProcStatement();
var loc = Current().Location;

if (statement != null) {
body = new DMASTProcBlockInner(loc,statement);
body = new DMASTProcBlockInner(statement.Location, statement);
} else {
body = new DMASTProcBlockInner(loc);
body = new DMASTProcBlockInner(Current().Location);
}
}

return new DMASTProcStatementSwitch.SwitchCaseValues(expressions.ToArray(), body);
} else if (Check(TokenType.DM_Else)) {
var loc = Current().Location;
Whitespace();
var loc = Current().Location;
if (Current().Type == TokenType.DM_If) {
//From now on, all ifs/elseifs/elses are actually part of this if's chain, not the switch's.
//Ambiguous, but that is parity behaviour. Ergo, the following emission.
Error(WarningCode.SuspiciousSwitchCase, "Expected \"if\" or \"else\" - \"else if\" is ambiguous as a switch case and may cause unintended flow");
DMCompiler.Emit(WarningCode.SuspiciousSwitchCase, loc,
"Expected \"if\" or \"else\" - \"else if\" is ambiguous as a switch case and may cause unintended flow");
}

DMASTProcBlockInner? body = ProcBlock();

if (body == null) {
Expand Down
4 changes: 1 addition & 3 deletions DMCompiler/Compiler/DM/DMParserHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using OpenDreamShared.Compiler;
using DMCompiler.Compiler.DMPreprocessor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DMCompiler.Bytecode;

namespace DMCompiler.Compiler.DM {
public partial class DMParser : Parser<Token> {

public partial class DMParser {
/// <summary>
/// A special override of Error() since, for DMParser, we know we are in a compilation context and can make use of error codes.
/// </summary>
Expand Down

0 comments on commit 6702666

Please sign in to comment.