Skip to content

Commit

Permalink
AST implementation for the 'size' expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Nov 21, 2024
1 parent 4fb4a53 commit ac415fb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/n8/ast/expression/SizeExpression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 - Nathanne Isip
* This file is part of N8.
*
* N8 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.
*
* N8 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 N8. If not, see <https://www.gnu.org/licenses/>.
*/

#include <n8/ast/ASTNodeException.hpp>
#include <n8/ast/expression/SizeExpression.hpp>

#include <n8/parser/Token.hpp>

DynamicObject SizeExpression::visit(SymbolTable& symbols) {
DynamicObject value = this->expression->visit(symbols);
if(value.isArray())
return DynamicObject((float) value.getArray()->size());
else if(value.isBool() || value.isNumber())
return DynamicObject(1.0f);
else if(value.isRegex())
return DynamicObject((float) value.getRegex()->getPattern().size());
else if(value.isString())
return DynamicObject((float) value.getString().size());

return DynamicObject(0.0f);
}

0 comments on commit ac415fb

Please sign in to comment.