Commit 4b76c397 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the expr_build convenience function

parent 224e7e2f
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -41,6 +41,13 @@ ExprStorage expr_create_list() {
  return e;
}

ExprStorage expr_create_list(ExprStorage items) {
  auto e = ExprStorage(new Expr, bind(&expr_destroy, _1));
  e->type = ExprType::LIST;
  e->list = std::move(items);
  return e;
}

ExprStorage expr_create_value(const std::string& str) {
  auto e = ExprStorage(new Expr, bind(&expr_destroy, _1));
  e->type = ExprType::VALUE;
@@ -59,6 +66,10 @@ const Expr* expr_next(const Expr* expr) {
  return expr->next.get();
}

void expr_set_next(Expr* expr, ExprStorage next) {
  expr->next = std::move(next);
}

ExprStorage* expr_get_next_storage(Expr* expr) {
  return &expr->next;
}
+2 −0
Original line number Diff line number Diff line
@@ -22,10 +22,12 @@ struct Expr;
using ExprStorage = std::unique_ptr<Expr, std::function<void (Expr*)>>;

ExprStorage expr_create_list();
ExprStorage expr_create_list(ExprStorage items);
ExprStorage expr_create_value(const std::string& str);
ExprStorage expr_create_value_literal(const std::string& str);

const Expr* expr_next(const Expr* expr);
void expr_set_next(Expr* expr, ExprStorage next);
ExprStorage* expr_get_next_storage(Expr* expr);

bool expr_is_list(const Expr* expr);
+5 −0
Original line number Diff line number Diff line
@@ -33,5 +33,10 @@ ExprVisitor expr_calln_fn(const std::initializer_list<ExprVisitor>& fns);

std::vector<const Expr*> expr_collect(const Expr* expr);

template <typename... T>
ExprStorage expr_build(T&&... items);

} // namespace fviz

#include "sexpr_util_impl.h"

core/sexpr_util_impl.h

0 → 100644
+45 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

namespace fviz {

template <typename T=void>
ExprStorage expr_build_next() {
  return ExprStorage(nullptr);
}

template <typename... T>
ExprStorage expr_build_next(ExprStorage head, T&&... tail) {
  auto e = std::move(head);
  auto n = expr_build_next(std::forward<T>(tail)...);
  expr_set_next(e.get(), std::move(n));
  return e;
}

template <typename... T>
ExprStorage expr_build_next(std::string head, T&&... tail) {
  auto e = expr_create_value(head);
  auto n = expr_build_next(std::forward<T>(tail)...);
  expr_set_next(e.get(), std::move(n));
  return e;
}

template <typename... T>
ExprStorage expr_build(T&&... items) {
  return expr_create_list(expr_build_next(std::forward<T>(items)...));
}

} // namespace fviz