Add the skeleton of th subcommand system
ci/woodpecker/push/build Pipeline was successful
Details
ci/woodpecker/push/build Pipeline was successful
Details
This commit is contained in:
parent
990de49c23
commit
6919c02ada
|
@ -0,0 +1,27 @@
|
||||||
|
/* -*- C++ -*-
|
||||||
|
* Serene Programming Language
|
||||||
|
*
|
||||||
|
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, version 2.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SERENE_COMMANDS_H
|
||||||
|
#define SERENE_COMMANDS_H
|
||||||
|
|
||||||
|
namespace serene::commands {
|
||||||
|
int cc(int argc, char **argv);
|
||||||
|
int run();
|
||||||
|
} // namespace serene::commands
|
||||||
|
|
||||||
|
#endif
|
|
@ -14,4 +14,8 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
target_sources(serene PRIVATE serene.cpp)
|
target_sources(serene PRIVATE
|
||||||
|
serene.cpp
|
||||||
|
|
||||||
|
commands/commands.cpp
|
||||||
|
)
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
/* -*- C++ -*-
|
||||||
|
* Serene Programming Language
|
||||||
|
*
|
||||||
|
* Copyright (c) 2019-2023 Sameer Rahmani <lxsameer@gnu.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, version 2.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "serene/commands.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
namespace serene::commands {
|
||||||
|
int cc(int argc, char **argv) {
|
||||||
|
printf(">> %d, %s\n", argc, argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int run() { return 0; }
|
||||||
|
} // namespace serene::commands
|
|
@ -16,32 +16,55 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iomanip>
|
#include "serene/commands.h"
|
||||||
#include <iostream>
|
#include "serene/config.h"
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
struct Vec3 {
|
#include <llvm/Support/CommandLine.h>
|
||||||
int x, y, z;
|
#include <llvm/Support/FormatVariadic.h>
|
||||||
|
|
||||||
// following constructor is no longer needed since C++20
|
#include <cstring>
|
||||||
Vec3(int x = 0, int y = 0, int z = 0) noexcept : x(x), y(y), z(z) {}
|
#include <string>
|
||||||
|
|
||||||
friend std::ostream &operator<<(std::ostream &os, const Vec3 &v) {
|
namespace cl = llvm::cl;
|
||||||
return os << "{ x=" << v.x << ", y=" << v.y << ", z=" << v.z << " }";
|
|
||||||
|
static std::string banner =
|
||||||
|
llvm::formatv("\n\nSerene Compiler Version {0}"
|
||||||
|
"\nCopyright (C) 2019-2023 "
|
||||||
|
"Sameer Rahmani <lxsameer@gnu.org>\n"
|
||||||
|
"Serene comes with ABSOLUTELY NO WARRANTY;\n"
|
||||||
|
"This is free software, and you are welcome\n"
|
||||||
|
"to redistribute it under certain conditions; \n"
|
||||||
|
"for details take a look at the LICENSE file.\n",
|
||||||
|
SERENE_VERSION);
|
||||||
|
|
||||||
|
namespace serene::opts {
|
||||||
|
// Global options ===========================================================
|
||||||
|
static cl::opt<bool> verbose("v", cl::desc("Use verbose output"),
|
||||||
|
cl::cat(cl::getGeneralCategory()),
|
||||||
|
cl::sub(cl::SubCommand::getAll()));
|
||||||
|
|
||||||
|
// Subcommands ==============================================================
|
||||||
|
// We don't use this subcommand directly but we need it for the CLI interface
|
||||||
|
static cl::SubCommand CC("cc", "Serene's C compiler interface");
|
||||||
|
|
||||||
|
static cl::SubCommand Run("run", "Run a Serene file");
|
||||||
|
} // namespace serene::opts
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
// We don't use llvm::cl here cause we want to let
|
||||||
|
// the clang take care of the argument parsing.
|
||||||
|
if ((argc >= 2) && (strcmp(argv[1], "cc") == 0)) {
|
||||||
|
return serene::commands::cc(--argc, ++argv);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
// We start using llvm::cl from here onward which
|
||||||
// Use the default constructor.
|
// enforces our rules even for the subcommands.
|
||||||
std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>();
|
cl::ParseCommandLineOptions(argc, argv, banner);
|
||||||
// Use the constructor that matches these arguments
|
|
||||||
std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2);
|
|
||||||
// Create a unique_ptr to an array of 5 elements
|
|
||||||
std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5);
|
|
||||||
|
|
||||||
std::cout << "make_unique<Vec3>(): " << *v1 << '\n'
|
if (serene::opts::Run) {
|
||||||
<< "make_unique<Vec3>(0,1,2): " << *v2 << '\n'
|
serene::commands::run();
|
||||||
<< "make_unique<Vec3[]>(5): ";
|
}
|
||||||
for (int i = 0; i < 5; i++)
|
|
||||||
std::cout << std::setw(i ? 30 : 0) << v3[static_cast<size_t>(i)] << '\n';
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue