diff --git a/src/executor/executor.cpp b/src/executor/executor.cpp index 0242eae..312c38f 100644 --- a/src/executor/executor.cpp +++ b/src/executor/executor.cpp @@ -45,11 +45,7 @@ Value execute(Instruction inst) { return modules::assert(inst.args); } - // For all other instructions, evaluate the arguments first - for(auto& arg : inst.args) { - arg = evaluate(arg); - } - + // Handle variable operations that require unevaluated arguments if (inst.instruction == InstructionType::Variable) { auto& args = inst.args; @@ -123,8 +119,6 @@ Value execute(Instruction inst) { return instance; } - - // Indexed assignment: $var index = value if (args.size() == 4 && args[2].valtype == ValueType::Identifier && args[2].string_val == "=") { std::string var_name; @@ -172,14 +166,23 @@ Value execute(Instruction inst) { data::modifyValue(var_name, new_val); return Value(); } + } - // Variable access or indexed get - // If args[0] is an Identifier and there are no args, it's a variable access. - if (args[0].valtype == ValueType::Identifier && args.empty()) { - return data::getValue(args[0].string_val); + // For all other instructions, evaluate the arguments first + for(auto& arg : inst.args) { + arg = evaluate(arg); + } + + if (inst.instruction == InstructionType::Variable) { + // This part handles variable access and member/method access on evaluated arguments + auto& args = inst.args; + + if (args.empty()) { + error("Empty variable instruction after evaluation"); + return Value(); } - Value subject = evaluate(args[0]); + Value subject = args[0]; // First arg is already evaluated std::vector op_args(args.begin() + 1, args.end()); if (op_args.empty()) { @@ -189,14 +192,14 @@ Value execute(Instruction inst) { // Indexed get: subject index if (subject.valtype == ValueType::List) { return handleListGet(subject, op_args); - } else if (subject.valtype == ValueType::String) { + } else if (subject.valtype == ValueType::String) { return handleStringGet(subject, op_args); } else if (subject.valtype == ValueType::Map) { if (op_args.empty()) { return subject; } - if (op_args[0].valtype != ValueType::Identifier) { - error("Struct member name must be an identifier."); + if (op_args[0].valtype != ValueType::Identifier && op_args[0].valtype != ValueType::String) { + error("Struct member name must be an identifier or string."); return Value(); } std::string member_name = op_args[0].string_val; @@ -218,7 +221,7 @@ Value execute(Instruction inst) { data::pushScope(); data::scopes.back()["self"] = subject; for (size_t i = 0; i < func.arg_names.size(); ++i) { - data::scopes.back()[func.arg_names[i]] = evaluate(func_args[i]); + data::scopes.back()[func.arg_names[i]] = func_args[i]; } Value return_val; for (const auto& body_inst : func.body) {