Skip to content

Commit

Permalink
node_context::lookup returns optional instead of (unsigned) -1
Browse files Browse the repository at this point in the history
  • Loading branch information
dsharlet committed Jan 1, 2024
1 parent 592e917 commit eec082e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ std::string node_context::name(symbol_id i) const {
}

symbol_id node_context::insert(const std::string& name) {
symbol_id id = lookup(name);
if (id == -1) {
std::optional<symbol_id> id = lookup(name);
if (!id) {
id = id_to_name.size();
id_to_name.push_back(name);
}
return id;
return *id;
}
symbol_id node_context::insert_unique(const std::string& prefix) {
symbol_id id = id_to_name.size();
id_to_name.push_back(prefix + std::to_string(id));
return id;
}
symbol_id node_context::lookup(const std::string& name) const {
std::optional<symbol_id> node_context::lookup(const std::string& name) const {
// TODO: At some point we might need a better data structure than doing this linear search.
for (symbol_id i = 0; i < id_to_name.size(); ++i) {
if (id_to_name[i] == name) {
return i;
}
}
return -1;
return {};
}

template <typename T>
Expand Down
3 changes: 2 additions & 1 deletion src/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <span>
#include <string>
#include <vector>
#include <optional>

namespace slinky {

Expand All @@ -28,7 +29,7 @@ class node_context {
// Get or insert a new symbol_id for a name.
symbol_id insert(const std::string& name);
symbol_id insert_unique(const std::string& prefix = "_");
symbol_id lookup(const std::string& name) const;
std::optional<symbol_id> lookup(const std::string& name) const;
};

enum class node_type {
Expand Down
12 changes: 6 additions & 6 deletions test/evaluate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ using namespace slinky;

TEST(evaluate_arithmetic) {
node_context ctx;
expr x = make_variable(ctx, "x");
var x(ctx, "x");

eval_context context;
context[ctx.lookup("x")] = 4;
context[x] = 4;

ASSERT_EQ(evaluate(x + 5, context), 9);
ASSERT_EQ(evaluate(x - 3, context), 1);
Expand All @@ -37,7 +37,7 @@ TEST(evaluate_arithmetic) {

TEST(evaluate_call) {
node_context ctx;
expr x = make_variable(ctx, "x");
var x(ctx, "x");
std::vector<index_t> calls;
stmt c = call_func::make(
[&](std::span<const index_t> scalars, std::span<raw_buffer*> buffers) -> index_t {
Expand All @@ -47,7 +47,7 @@ TEST(evaluate_call) {
{x}, {}, nullptr);

eval_context context;
context[ctx.lookup("x")] = 2;
context[x] = 2;

int result = evaluate(c, context);
ASSERT_EQ(result, 0);
Expand All @@ -57,7 +57,7 @@ TEST(evaluate_call) {

TEST(evaluate_loop) {
node_context ctx;
expr x = make_variable(ctx, "x");
var x(ctx, "x");
std::vector<index_t> calls;
stmt c = call_func::make(
[&](std::span<const index_t> scalars, std::span<raw_buffer*> buffers) -> index_t {
Expand All @@ -66,7 +66,7 @@ TEST(evaluate_loop) {
},
{x}, {}, nullptr);

stmt l = loop::make(ctx.lookup("x"), range(2, 12), c);
stmt l = loop::make(x.name(), range(2, 12), c);

int result = evaluate(l);
ASSERT_EQ(result, 0);
Expand Down

0 comments on commit eec082e

Please sign in to comment.