Skip to content

Commit

Permalink
Initial Zhivo standard library source files.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Oct 17, 2024
1 parent bdccffd commit 7933e73
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/zhivo_std.hpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#ifndef ZHIVO_STD_LIB_CC
#define ZHIVO_STD_LIB_CC

#include <zhivo/core/DynamicObject.hpp>
#include <zhivo/core/SymbolTable.hpp>

#define ZHIVO_LIB_START extern "C" {
#define ZHIVO_LIB_END }

#define ZHIVO_FUNC(funcName) \
DynamicObject funcName( \
const SymbolTable& symtab, \
const std::vector<DynamicObject>& args \
)

#endif
43 changes: 43 additions & 0 deletions lib/zhivo_stdlib_io.cc
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#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<int>(exitCode.getNumber()));
return {};
}
32 changes: 32 additions & 0 deletions lib/zhivo_stdlib_io.hpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#ifndef ZHIVO_STDLIB_IO_CC
#define ZHIVO_STDLIB_IO_CC

#include "zhivo_std.hpp"

#include <exception>
#include <iostream>

ZHIVO_LIB_START
ZHIVO_FUNC(io_print);
ZHIVO_FUNC(io_exit);
ZHIVO_LIB_END

#endif

0 comments on commit 7933e73

Please sign in to comment.