Skip to content

Commit

Permalink
Initial standard library functions for reflection features.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Oct 31, 2024
1 parent 4b8eee5 commit d23737d
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
106 changes: 106 additions & 0 deletions lib/zhvlib/Reflect.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 "zhvlib/Reflect.hpp"

#include <zhivo/ast/TerminativeSignal.hpp>

ZHIVO_FUNC(reflect_get) {
if(args.size() != 1)
throw TerminativeThrowSignal(
std::move(address),
"Expecting 1 argument, got " +
std::to_string(args.size())
);

DynamicObject name = args.at(0);
const std::string symName = name.toString();

return symtab.getSymbol(
std::move(address),
symName
);
}

ZHIVO_FUNC(reflect_has) {
if(args.size() != 1)
throw TerminativeThrowSignal(
std::move(address),
"Expecting 1 argument, got " +
std::to_string(args.size())
);

DynamicObject name = args.at(0);
const std::string symName = name.toString();

return DynamicObject(symtab.hasSymbol(symName));
}

ZHIVO_FUNC(reflect_type) {
if(args.size() != 1)
throw TerminativeThrowSignal(
std::move(address),
"Expecting 1 argument, got " +
std::to_string(args.size())
);

DynamicObject name = args.at(0);
const std::string symName = name.toString();

return symtab.getSymbol(
std::move(address),
symName
).objectType();
}

ZHIVO_FUNC(reflect_declare) {
if(args.size() != 2)
throw TerminativeThrowSignal(
std::move(address),
"Expecting 2 argument, got " +
std::to_string(args.size())
);

DynamicObject name = args.at(0),
value = args.at(1);

std::string symName = name.toString();
if(Tokenizer::isValidIdentifier(symName))
throw TerminativeThrowSignal(
std::move(address),
"Invalid identifier string: " +
symName
);

symtab.setSymbol(symName, value);
return value;
}

ZHIVO_FUNC(reflect_delete) {
if(args.size() != 1)
throw TerminativeThrowSignal(
std::move(address),
"Expecting 1 argument, got " +
std::to_string(args.size())
);

DynamicObject name = args.at(0);
symtab.removeSymbol(name.toString());

return DynamicObject();
}
35 changes: 35 additions & 0 deletions lib/zhvlib/Reflect.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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_REFLECT_CC
#define ZHIVO_STDLIB_REFLECT_CC

#include "ZhivoLibrary.hpp"

ZHIVO_LIB_START

ZHIVO_FUNC(reflect_get);
ZHIVO_FUNC(reflect_has);
ZHIVO_FUNC(reflect_type);

ZHIVO_FUNC(reflect_declare);
ZHIVO_FUNC(reflect_delete);

ZHIVO_LIB_END

#endif

0 comments on commit d23737d

Please sign in to comment.