You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
918 B
62 lines
918 B
/* |
|
* Copyright (C) 2023 Riyyi |
|
* |
|
* SPDX-License-Identifier: MIT |
|
*/ |
|
|
|
#include "ruc/format/print.h" |
|
|
|
#include "ast.h" |
|
#include "environment.h" |
|
|
|
namespace blaze { |
|
|
|
Environment::Environment(EnvironmentPtr outer) |
|
: m_outer(outer) |
|
{ |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
bool Environment::exists(const std::string& symbol) |
|
{ |
|
return m_values.find(symbol) != m_values.end(); |
|
} |
|
|
|
ASTNodePtr Environment::set(const std::string& symbol, ASTNodePtr value) |
|
{ |
|
if (exists(symbol)) { |
|
m_values.erase(symbol); |
|
} |
|
|
|
m_values.emplace(symbol, value); |
|
|
|
return value; |
|
} |
|
|
|
ASTNodePtr Environment::get(const std::string& symbol) |
|
{ |
|
m_current_key = symbol; |
|
|
|
if (exists(symbol)) { |
|
return m_values[symbol]; |
|
} |
|
|
|
if (m_outer) { |
|
return m_outer->get(symbol); |
|
} |
|
|
|
return nullptr; |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
GlobalEnvironment::GlobalEnvironment() |
|
{ |
|
add(); |
|
sub(); |
|
mul(); |
|
div(); |
|
} |
|
|
|
} // namespace blaze
|
|
|