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

Handle fexists() in the preprocessor #1824

Merged
merged 10 commits into from
Jun 7, 2024
7 changes: 7 additions & 0 deletions Content.Tests/DMProject/Tests/Preprocessor/fexists_fail.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#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
34 changes: 34 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 @@ -314,6 +315,39 @@

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_Resource && fexistsInner.Type != TokenType.DM_ConstantString) {
Error($"Unexpected token {fexistsInner.PrintableText} - resource path expected");
return DegenerateValue;
}

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

Console.WriteLine(Directory.GetCurrentDirectory());

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

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

filePath = Path.Combine(outputDir, filePath);

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

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