diff options
author | Piotr Fusik <piotr@fusion-lang.org> | 2024-02-27 21:23:55 +0100 |
---|---|---|
committer | Piotr Fusik <piotr@fusion-lang.org> | 2024-02-27 21:29:04 +0100 |
commit | 990a165c519f2c598f69f9e9c9e48a09e34e7c45 (patch) | |
tree | 8b513e9881d4161e3a583124e8930997bf0d4b61 | |
parent | ab20aa74118dbafe42ab8320eaf22859b556e259 (diff) |
[cpp] Environment.GetEnvironmentVariable(stringRef).
-rw-r--r-- | GenCpp.fu | 19 | ||||
-rw-r--r-- | libfut.cpp | 19 | ||||
-rw-r--r-- | libfut.cs | 19 | ||||
-rw-r--r-- | libfut.hpp | 1 | ||||
-rw-r--r-- | libfut.js | 19 | ||||
-rw-r--r-- | test/Environment.fu | 2 |
6 files changed, 41 insertions, 38 deletions
@@ -595,14 +595,6 @@ public class GenCpp : GenCCpp WriteChar(')'); } - void WriteCString!(FuExpr expr) - { - if (expr is FuLiteralString) - expr.Accept(this, FuPriority.Argument); - else - WritePostfix(expr, ".c_str()"); - } - void WriteRegex!(List<FuExpr#> args, int argIndex) { Include("regex"); @@ -1146,7 +1138,16 @@ public class GenCpp : GenCCpp case FuId.EnvironmentGetEnvironmentVariable: Include("cstdlib"); Write("std::getenv("); - WriteCString(args[0]); + if (args[0].Type.Id == FuId.StringStorageType) + WritePostfix(args[0], ".c_str()"); + else if (args[0] is FuLiteralString) + args[0].Accept(this, FuPriority.Argument); + else { + Include("string"); + Write("std::string("); + args[0].Accept(this, FuPriority.Argument); + Write(").c_str()"); + } WriteChar(')'); break; case FuId.RegexCompile: @@ -13530,14 +13530,6 @@ void GenCpp::writeCollectionMethod(const FuExpr * obj, std::string_view name, co writeChar(')'); } -void GenCpp::writeCString(const FuExpr * expr) -{ - if (dynamic_cast<const FuLiteralString *>(expr)) - expr->accept(this, FuPriority::argument); - else - writePostfix(expr, ".c_str()"); -} - void GenCpp::writeRegex(const std::vector<std::shared_ptr<FuExpr>> * args, int argIndex) { include("regex"); @@ -14082,7 +14074,16 @@ void GenCpp::writeCallExpr(const FuExpr * obj, const FuMethod * method, const st case FuId::environmentGetEnvironmentVariable: include("cstdlib"); write("std::getenv("); - writeCString((*args)[0].get()); + if ((*args)[0]->type->id == FuId::stringStorageType) + writePostfix((*args)[0].get(), ".c_str()"); + else if (dynamic_cast<const FuLiteralString *>((*args)[0].get())) + (*args)[0]->accept(this, FuPriority::argument); + else { + include("string"); + write("std::string("); + (*args)[0]->accept(this, FuPriority::argument); + write(").c_str()"); + } writeChar(')'); break; case FuId::regexCompile: @@ -13913,14 +13913,6 @@ namespace Fusion WriteChar(')'); } - void WriteCString(FuExpr expr) - { - if (expr is FuLiteralString) - expr.Accept(this, FuPriority.Argument); - else - WritePostfix(expr, ".c_str()"); - } - void WriteRegex(List<FuExpr> args, int argIndex) { Include("regex"); @@ -14458,7 +14450,16 @@ namespace Fusion case FuId.EnvironmentGetEnvironmentVariable: Include("cstdlib"); Write("std::getenv("); - WriteCString(args[0]); + if (args[0].Type.Id == FuId.StringStorageType) + WritePostfix(args[0], ".c_str()"); + else if (args[0] is FuLiteralString) + args[0].Accept(this, FuPriority.Argument); + else { + Include("string"); + Write("std::string("); + args[0].Accept(this, FuPriority.Argument); + Write(").c_str()"); + } WriteChar(')'); break; case FuId.RegexCompile: @@ -2325,7 +2325,6 @@ private: void writeStringMethod(const FuExpr * obj, std::string_view name, const FuMethod * method, const std::vector<std::shared_ptr<FuExpr>> * args); void writeAllAnyContains(std::string_view function, const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args); void writeCollectionMethod(const FuExpr * obj, std::string_view name, const std::vector<std::shared_ptr<FuExpr>> * args); - void writeCString(const FuExpr * expr); void writeRegex(const std::vector<std::shared_ptr<FuExpr>> * args, int argIndex); void writeWrite(const std::vector<std::shared_ptr<FuExpr>> * args, bool newLine); void writeRegexArgument(const FuExpr * expr); @@ -14335,14 +14335,6 @@ export class GenCpp extends GenCCpp this.writeChar(41); } - #writeCString(expr) - { - if (expr instanceof FuLiteralString) - expr.accept(this, FuPriority.ARGUMENT); - else - this.writePostfix(expr, ".c_str()"); - } - #writeRegex(args, argIndex) { this.include("regex"); @@ -14884,7 +14876,16 @@ export class GenCpp extends GenCCpp case FuId.ENVIRONMENT_GET_ENVIRONMENT_VARIABLE: this.include("cstdlib"); this.write("std::getenv("); - this.#writeCString(args[0]); + if (args[0].type.id == FuId.STRING_STORAGE_TYPE) + this.writePostfix(args[0], ".c_str()"); + else if (args[0] instanceof FuLiteralString) + args[0].accept(this, FuPriority.ARGUMENT); + else { + this.include("string"); + this.write("std::string("); + args[0].accept(this, FuPriority.ARGUMENT); + this.write(").c_str()"); + } this.writeChar(41); break; case FuId.REGEX_COMPILE: diff --git a/test/Environment.fu b/test/Environment.fu index f25519e..6cf2e3a 100644 --- a/test/Environment.fu +++ b/test/Environment.fu @@ -3,7 +3,7 @@ public static class Test public static bool Run() { string s = "HOME"; - string? homeDir = Environment.GetEnvironmentVariable(s); //FAIL: cpp TODO; cl + string? homeDir = Environment.GetEnvironmentVariable(s); //FAIL: cl return homeDir != null && Environment.GetEnvironmentVariable("NOT_EXISTING_ENV_VAR") == null && Environment.GetEnvironmentVariable("") == null |