mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-13 18:38:44 +00:00
v0.0.1
This commit is contained in:
parent
a2e305c27f
commit
d38d75c9d8
8 changed files with 90 additions and 56 deletions
|
|
@ -63,9 +63,14 @@ void ExecuteVisitor::Visit(FunctionDeclaration* node) {
|
|||
void ExecuteVisitor::Visit(FunctionDefinitionStatement* node) { // visited on function call
|
||||
context_manager_.EnterContext();
|
||||
|
||||
size_t context_count = context_manager_.ContextCount();
|
||||
try {
|
||||
Visitor::Visit(node->value);
|
||||
} catch (utils::ValueReturnedMarker&) {}
|
||||
} catch (utils::ValueReturnedMarker&) {
|
||||
while (context_manager_.ContextCount() > context_count) {
|
||||
context_manager_.ExitContext();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: sometimes return references, choose by declaration
|
||||
current_value_ = context_manager_.ToTemporaryValue(current_value_);
|
||||
|
|
@ -345,9 +350,13 @@ void ExecuteVisitor::Visit(Block* node) {
|
|||
bool value_brought = false;
|
||||
|
||||
for (auto& statement : node->statements) {
|
||||
size_t context_count = context_manager_.ContextCount();
|
||||
try {
|
||||
Visitor::Visit(statement);
|
||||
} catch (utils::ValueBroughtMarker&) { // current_value_ passed from ReturnExpression
|
||||
while (context_manager_.ContextCount() > context_count) {
|
||||
context_manager_.ExitContext();
|
||||
}
|
||||
value_brought = true;
|
||||
break;
|
||||
}
|
||||
|
|
@ -459,7 +468,7 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
context_manager_.ToModifiedValue(current_value_, utils::ValueType::Const);
|
||||
}
|
||||
if (!context_manager_.DefineVariable(utils::ClassInternalVarName, current_value_)) {
|
||||
error_handling::HandleRuntimeError("Variable redefinition (mathod caller var)", node->base);
|
||||
error_handling::HandleRuntimeError("Variable redefinition (self var)", node->base);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -481,7 +490,7 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
if (!maybe_function_definition_info.has_value()) {
|
||||
if (HandleBuiltinFunctionCall(node)) {
|
||||
context_manager_.ExitContext();
|
||||
return;
|
||||
return; // TODO: return from end of function
|
||||
}
|
||||
|
||||
error_handling::HandleRuntimeError("Function definition not found (by graph_id_)", node->base);
|
||||
|
|
@ -490,7 +499,6 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
function_definition = maybe_function_definition_info.value().node;
|
||||
|
||||
} else if (node->graph_id_.has_value()) {
|
||||
error_handling::DebugPrint(typeclass_graph_.GetVertex(node->graph_id_.value()).name);
|
||||
|
||||
utils::IdType defined_type_graph_id; // try to find defined or basic type
|
||||
if (typeclass_graph_.GetVertex(node->graph_id_.value()).type_id.has_value()
|
||||
|
|
@ -510,7 +518,7 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
std::optional<FunctionDefinitionStatement*> maybe_function_definition;
|
||||
|
||||
auto maybe_function_graph_info = typeclass_graph_.GetFunctionInfo(node->name,
|
||||
node->graph_id_.value());
|
||||
defined_type_graph_id);
|
||||
|
||||
if (!maybe_function_graph_info.has_value()) {
|
||||
error_handling::HandleRuntimeError("Function info not found (by graph_id_)", node->base);
|
||||
|
|
@ -520,8 +528,8 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
maybe_function_definition = maybe_function_graph_info.value()->definition;
|
||||
|
||||
if (!maybe_function_definition.has_value()) {
|
||||
if (typeclass_graph_.GetVertex(node->graph_id_.value()).modifier == info::TypeclassGraph::Modifier::Type) {
|
||||
auto maybe_internal_type = info::type::ToInternalType(typeclass_graph_.GetVertex(node->graph_id_.value()).name);
|
||||
if (typeclass_graph_.GetVertex(defined_type_graph_id).modifier == info::TypeclassGraph::Modifier::Type) {
|
||||
auto maybe_internal_type = info::type::ToInternalType(typeclass_graph_.GetVertex(defined_type_graph_id).name);
|
||||
|
||||
if (maybe_internal_type.has_value()) {
|
||||
if (HandleBuiltinTypeFunctionCall(node, maybe_internal_type.value())) {
|
||||
|
|
@ -533,37 +541,6 @@ void ExecuteVisitor::Visit(FunctionCallExpression* node) {
|
|||
|
||||
error_handling::HandleRuntimeError("Type function definition not found (by graph_id_)", node->base);
|
||||
}
|
||||
|
||||
// if (!node->abstract_type_name_.has_value()) {
|
||||
// error_handling::HandleInternalError("Typeclass function's abstract_type_name_ has no value",
|
||||
// "ExecuteVisitor.FunctionCallExpression",
|
||||
// &node->base);
|
||||
// }
|
||||
|
||||
auto maybe_type_id = typeclass_graph_.GetVertex(defined_type_graph_id).type_id;
|
||||
|
||||
if (!maybe_type_id.has_value()) {
|
||||
error_handling::HandleInternalError("Function's abstract type type_id not found",
|
||||
"ExecuteVisitor.FunctionCallExpression",
|
||||
&node->base);
|
||||
}
|
||||
|
||||
auto maybe_type_info = global_info_.GetTypeInfo<info::definition::AnyType>(maybe_type_id.value());
|
||||
|
||||
if (!maybe_type_info.has_value()) {
|
||||
error_handling::HandleRuntimeError("Function's abstract type replacement defined type is not AnyType", node->base);
|
||||
}
|
||||
|
||||
maybe_function_graph_info = typeclass_graph_.GetFunctionInfo(node->name, maybe_type_info.value()->type.node->graph_id_);
|
||||
|
||||
if (!maybe_function_graph_info.has_value()) {
|
||||
error_handling::HandleRuntimeError("Function info not found (by abstract type graph_id_)", node->base);
|
||||
}
|
||||
|
||||
maybe_function_definition = maybe_function_graph_info.value()->definition;
|
||||
}
|
||||
|
||||
if (!maybe_function_definition.has_value()) {
|
||||
error_handling::HandleRuntimeError("Function definition not found (by graph_id_)", node->base);
|
||||
}
|
||||
|
||||
|
|
@ -1041,8 +1018,16 @@ bool ExecuteVisitor::HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
|
|||
switch (type) {
|
||||
case info::type::InternalType::Float:
|
||||
if (name == "show") {
|
||||
try {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(std::to_string(*arguments[0]->GetValue<double>().value())),
|
||||
utils::ValueType::Tmp);
|
||||
} catch(...) {
|
||||
error_handling::HandleRuntimeError("Can't read Float", node->base);
|
||||
}
|
||||
} else if (name == "read") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(std::to_string(*arguments[0]->GetValue<double>().value())),
|
||||
info::value::InternalValue(std::stod(*arguments[0]->GetValue<std::string>().value())),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "<") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
|
|
@ -1094,6 +1079,14 @@ bool ExecuteVisitor::HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
|
|||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(std::to_string(*arguments[0]->GetValue<int64_t>().value())),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "read") {
|
||||
try {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(std::stoi(*arguments[0]->GetValue<std::string>().value())),
|
||||
utils::ValueType::Tmp);
|
||||
} catch(...) {
|
||||
error_handling::HandleRuntimeError("Can't read Int", node->base);
|
||||
}
|
||||
} else if (name == "<") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(*arguments[0]->GetValue<int64_t>().value() < *arguments[1]->GetValue<int64_t>().value()),
|
||||
|
|
@ -1147,6 +1140,10 @@ bool ExecuteVisitor::HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
|
|||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(*arguments[0]->GetValue<std::string>().value()),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "read") { // do not copy ??
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(*arguments[0]->GetValue<std::string>().value()),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "<") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(*arguments[0]->GetValue<std::string>().value() < *arguments[1]->GetValue<std::string>().value()),
|
||||
|
|
@ -1177,6 +1174,14 @@ bool ExecuteVisitor::HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
|
|||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(std::string{*arguments[0]->GetValue<char>().value()}),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "read") {
|
||||
if (arguments[0]->GetValue<std::string>().value()->size() != 1) {
|
||||
error_handling::HandleRuntimeError("Can't read Char", node->base);
|
||||
} else {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue((*arguments[0]->GetValue<std::string>().value())[0]),
|
||||
utils::ValueType::Tmp);
|
||||
}
|
||||
} else if (name == "<") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(*arguments[0]->GetValue<char>().value() < *arguments[1]->GetValue<char>().value()),
|
||||
|
|
@ -1199,6 +1204,18 @@ bool ExecuteVisitor::HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
|
|||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(*arguments[0]->GetValue<bool>().value() ? "true" : "false"),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "read") {
|
||||
if (*arguments[0]->GetValue<std::string>().value() == "true") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(true),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (*arguments[0]->GetValue<std::string>().value() == "false") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(false),
|
||||
utils::ValueType::Tmp);
|
||||
} else {
|
||||
error_handling::HandleRuntimeError("Can't read Bool", node->base);
|
||||
}
|
||||
} else if (name == "=") {
|
||||
*arguments[0]->GetValue<bool>().value() = *arguments[1]->GetValue<bool>().value();
|
||||
current_value_ = context_manager_.AddValue(
|
||||
|
|
@ -1213,6 +1230,13 @@ bool ExecuteVisitor::HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
|
|||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue("()"),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "read") {
|
||||
if (*arguments[0]->GetValue<std::string>().value() != "()") {
|
||||
error_handling::HandleRuntimeError("Can't read Unit", node->base);
|
||||
}
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(info::value::Unit()),
|
||||
utils::ValueType::Tmp);
|
||||
} else if (name == "=") {
|
||||
current_value_ = context_manager_.AddValue(
|
||||
info::value::InternalValue(info::value::Unit()),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue