Skip to content

Commit

Permalink
Handle fexists() in the preprocessor (OpenDreamProject#1824)
Browse files Browse the repository at this point in the history
Co-authored-by: ike709 <[email protected]>
Co-authored-by: wixoa <[email protected]>
  • Loading branch information
3 people authored Jun 7, 2024
1 parent ab03e50 commit 6d7c578
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Content.Tests/DMProject/Tests/Preprocessor/fexists_fail.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if fexists("fake/path.abc")
#error "how could this exist"
#endif

/proc/RunTest()
return
8 changes: 8 additions & 0 deletions Content.Tests/DMProject/Tests/Preprocessor/fexists_pass.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if fexists("fexists_pass.dm")
#warn "yay!"
#else
#error "it doesn't exist"
#endif

/proc/RunTest()
return
2 changes: 1 addition & 1 deletion DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private List<Token> GetLineOfTokens()
case TokenType.DM_Preproc_LineSplice:
continue;
case TokenType.DM_Preproc_Identifier:
if(token.Text == "defined") // need to be careful here to prevent macros in defined() expressions from being clobbered
if(token.Text == "defined" || token.Text == "fexists") // need to be careful here to prevent macros in defined() or fexists() expressions from being clobbered
tryIdentifiersAsMacros = false;
else if (tryIdentifiersAsMacros && TryMacro(token)) // feeding any novel macro tokens back into the pipeline here
continue;
Expand Down
30 changes: 30 additions & 0 deletions DMCompiler/Compiler/DMPreprocessor/DMPreprocessorParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DMCompiler.Compiler.DM;
using System;
using System.Collections.Generic;
using System.IO;

namespace DMCompiler.Compiler.DMPreprocessor;

Expand Down Expand Up @@ -313,6 +314,35 @@ private static void Error(string msg) {
}

return _defines!.ContainsKey(definedInner.Text) ? 1.0f : 0.0f;
} else if (token.Text == "fexists") {
Advance();
if (!Check(TokenType.DM_LeftParenthesis)) {
Error("Expected '(' to begin fexists() expression");
return DegenerateValue;
}

Token fExistsInner = Current();

if (fExistsInner.Type != TokenType.DM_ConstantString) {
Error($"Unexpected token {fExistsInner.PrintableText} - file path expected");
return DegenerateValue;
}

Advance();
if (!Check(TokenType.DM_RightParenthesis)) {
DMCompiler.Emit(WarningCode.DefinedMissingParen, token.Location,
"Expected ')' to end fexists() expression");
}

var filePath = Path.GetRelativePath(".", fExistsInner.Value!.ToString()!.Replace('\\', '/'));

var outputDir = Path.Combine(Path.GetDirectoryName(DMCompiler.Settings.Files?[0]) ?? "/", Path.GetDirectoryName(fExistsInner.Location.SourceFile) ?? "/");
if (string.IsNullOrEmpty(outputDir))
outputDir = "./";

filePath = Path.Combine(outputDir, filePath);

return File.Exists(filePath) ? 1.0f : 0.0f;
}

Error($"Unexpected identifier {token.PrintableText} in preprocessor expression");
Expand Down

0 comments on commit 6d7c578

Please sign in to comment.