saqut-compiler/src/cli/args.hpp

129 lines
3.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ============================================================================
// saQut Compiler — CLI Argüman Ayrıştırıcı ve Kaynak Okuma
// ============================================================================
//
// DİZİN: src/cli/args.hpp
// BAĞIMLI: Yok (sadece standart kütüphane)
//
// AMAÇ:
// 1. POSIX tarzı argüman ayrıştırma
// 2. Kaynak dosya okuma (tüm komutlar tarafından paylaşılır)
//
// DESTEKLENEN FORMATLAR:
// saqut <komut> [dosya] [-o çıktı] [--format json] [--help]
// saqut run file:source.sqt (eski sözdizimi)
// saqut - (stdin — TODO)
//
// ============================================================================
#ifndef SAQUT_CLI_ARGS
#define SAQUT_CLI_ARGS
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct CliArgs {
std::string command;
std::vector<std::string> positional;
std::string outputFile;
std::string format;
bool showHelp = false;
bool stdinMode = false;
};
// ============================================================================
// parseArgs
// ============================================================================
inline CliArgs parseArgs(int argc, char* argv[]) {
CliArgs args;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-") {
args.stdinMode = true;
continue;
}
if (arg == "-h" || arg == "--help") {
args.showHelp = true;
continue;
}
if (arg.compare(0, 9, "--output=") == 0) {
args.outputFile = arg.substr(9);
continue;
}
if (arg.compare(0, 9, "--format=") == 0) {
args.format = arg.substr(9);
continue;
}
if (arg == "--output" || arg == "-o") {
if (i + 1 < argc) args.outputFile = argv[++i];
continue;
}
if (arg == "--format") {
if (i + 1 < argc) args.format = argv[++i];
continue;
}
if (arg.compare(0, 5, "file:") == 0) {
args.positional.push_back(arg.substr(5));
continue;
}
if (arg.compare(0, 7, "output:") == 0) {
args.outputFile = arg.substr(7);
continue;
}
if (arg.compare(0, 4, "ast:") == 0) {
args.outputFile = arg.substr(4);
continue;
}
// İlk argüman komut mu?
if (args.command.empty() && i == 1) {
if (arg == "run" || arg == "tokens" || arg == "ast" ||
arg == "symbols" || arg == "compile" || arg == "parse" ||
arg == "transpile" || arg == "interpret") {
args.command = arg;
continue;
}
args.command = "run";
args.positional.push_back(arg);
continue;
}
args.positional.push_back(arg);
}
if (args.command.empty()) args.command = "run";
if (args.positional.empty() && !args.stdinMode)
args.positional.push_back("source.sqt");
return args;
}
// ============================================================================
// readSource: Dosyadan veya stdin'den kaynak kod oku (TODO: stdin)
// ============================================================================
inline std::string readSource(const CliArgs& args) {
if (args.stdinMode) {
// TODO: std::cin'den EOF'a kadar oku
std::cerr << "TODO: stdin modu henüz desteklenmiyor\n";
return "";
}
if (args.positional.empty()) return "";
std::string path = args.positional[0];
std::ifstream file(path, std::ios::in | std::ios::binary);
if (!file.is_open()) {
std::cerr << "Hata: '" << path << "' dosyasıılamadı\n";
return "";
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
#endif // SAQUT_CLI_ARGS