From 7933e73c1cc4d61833d8bf5b2daee80473511dd8 Mon Sep 17 00:00:00 2001 From: Nathanne Isip Date: Thu, 17 Oct 2024 12:36:14 +0800 Subject: [PATCH] Initial Zhivo standard library source files. --- lib/zhivo_std.hpp | 34 ++++++++++++++++++++++++++++++++ lib/zhivo_stdlib_io.cc | 43 +++++++++++++++++++++++++++++++++++++++++ lib/zhivo_stdlib_io.hpp | 32 ++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 lib/zhivo_std.hpp create mode 100644 lib/zhivo_stdlib_io.cc create mode 100644 lib/zhivo_stdlib_io.hpp diff --git a/lib/zhivo_std.hpp b/lib/zhivo_std.hpp new file mode 100644 index 0000000..51eaa58 --- /dev/null +++ b/lib/zhivo_std.hpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 - Nathanne Isip + * This file is part of Zhivo. + * + * Zhivo 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, either version 3 of the License, + * or (at your option) any later version. + * + * Zhivo 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 Zhivo. If not, see . + */ + +#ifndef ZHIVO_STD_LIB_CC +#define ZHIVO_STD_LIB_CC + +#include +#include + +#define ZHIVO_LIB_START extern "C" { +#define ZHIVO_LIB_END } + +#define ZHIVO_FUNC(funcName) \ + DynamicObject funcName( \ + const SymbolTable& symtab, \ + const std::vector& args \ + ) + +#endif diff --git a/lib/zhivo_stdlib_io.cc b/lib/zhivo_stdlib_io.cc new file mode 100644 index 0000000..33f55b9 --- /dev/null +++ b/lib/zhivo_stdlib_io.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 - Nathanne Isip + * This file is part of Zhivo. + * + * Zhivo 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, either version 3 of the License, + * or (at your option) any later version. + * + * Zhivo 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 Zhivo. If not, see . + */ + +#include "zhivo_stdlib_io.hpp" + +ZHIVO_FUNC(io_print) { + if(args.size() == 0) + return {}; + + for(size_t i = 0; i < args.size(); i++) { + DynamicObject arg = args.at(i); + std::cout << arg.toString(); + } + + return {}; +} + +ZHIVO_FUNC(io_exit) { + if(args.size() == 0) + exit(0); + + DynamicObject exitCode = args.at(0); + if(!exitCode.isNumber()) + throw std::runtime_error("Exit code is not a number."); + + exit(static_cast(exitCode.getNumber())); + return {}; +} diff --git a/lib/zhivo_stdlib_io.hpp b/lib/zhivo_stdlib_io.hpp new file mode 100644 index 0000000..2e135de --- /dev/null +++ b/lib/zhivo_stdlib_io.hpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 - Nathanne Isip + * This file is part of Zhivo. + * + * Zhivo 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, either version 3 of the License, + * or (at your option) any later version. + * + * Zhivo 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 Zhivo. If not, see . + */ + +#ifndef ZHIVO_STDLIB_IO_CC +#define ZHIVO_STDLIB_IO_CC + +#include "zhivo_std.hpp" + +#include +#include + +ZHIVO_LIB_START +ZHIVO_FUNC(io_print); +ZHIVO_FUNC(io_exit); +ZHIVO_LIB_END + +#endif