From e7cb2de19847cab9658fe4f9aec3a8c5a3957a1b Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Tue, 17 May 2022 02:49:21 +0200 Subject: [PATCH] Evaluation support variable resolution --- src/interpreter.cc | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/interpreter.cc b/src/interpreter.cc index 96fe4c1..8b93d3e 100644 --- a/src/interpreter.cc +++ b/src/interpreter.cc @@ -69,7 +69,17 @@ Result Interpreter::eval(Ast &&ast) { switch (ast.type) { case Ast::Type::Literal: - return Value::from(ast.token); + switch (ast.token.type) { + case Token::Type::Symbol: + { + auto const value = env().find(std::string(ast.token.source)); + assert(value, "Missing variable error is not implemented yet"); + return *value; + } + + default: + return Value::from(ast.token); + } case Ast::Type::Binary: { @@ -100,21 +110,23 @@ Result Interpreter::eval(Ast &&ast) case Ast::Type::Call: { - auto location = ast.arguments.front().location; Value func = Try(eval(std::move(ast.arguments.front()))); - if (func.type != Value::Type::Symbol) - return errors::not_callable(std::move(location), func.type); - if (auto body = env().find(func.s); body) { - std::vector values; - values.reserve(ast.arguments.size()); - for (auto& a : std::span(ast.arguments).subspan(1)) { - values.push_back(Try(eval(std::move(a)))); - } - return (*body)(std::move(values)); - } else { - return errors::function_not_defined(func); + std::vector values; + values.reserve(ast.arguments.size()); + for (auto& a : std::span(ast.arguments).subspan(1)) { + values.push_back(Try(eval(std::move(a)))); } + return std::move(func)(std::move(values)); + } + + case Ast::Type::Variable_Declaration: + { + assert(ast.arguments.size() == 2, "Only simple assigments are supported now"); + assert(ast.arguments.front().type == Ast::Type::Literal, "Only names are supported as LHS arguments now"); + assert(ast.arguments.front().token.type == Token::Type::Symbol, "Only names are supported as LHS arguments now"); + env().force_define(std::string(ast.arguments.front().token.source), Try(eval(std::move(ast.arguments.back())))); + return Value{}; } default: