program.hpp 1.23 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#ifndef GUARD_RTGLIB_PROGRAM_HPP
#define GUARD_RTGLIB_PROGRAM_HPP

Paul's avatar
Paul committed
4
#include <list>
Paul's avatar
Paul committed
5
6
#include <unordered_map>
#include <rtg/instruction.hpp>
Paul's avatar
Paul committed
7
#include <rtg/operand.hpp>
Paul's avatar
Paul committed
8
#include <rtg/builtin.hpp>
Paul's avatar
Paul committed
9
10
11
12
13

namespace rtg {

struct program
{
Paul's avatar
Paul committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    template<class... Ts>
    instruction * add_instruction(std::string name, Ts*... args)
    {
        auto&& op = ops.at(name);
        shape r = op.compute_shape({args->result...});
        instructions.push_back({name, r, {args...}});
        return std::addressof(instructions.back());
    }
    template<class... Ts>
    instruction * add_literal(Ts&&... xs)
    {
        instructions.emplace_back(literal{std::forward<Ts>(xs)...});
        return std::addressof(instructions.back());
    }

Paul's avatar
Paul committed
29
30
    instruction * add_parameter(std::string name, shape s)
    {
Paul's avatar
Paul committed
31
        instructions.push_back({builtin::param+std::move(name), s, {}});
Paul's avatar
Paul committed
32
33
34
        return std::addressof(instructions.back());
    }

Paul's avatar
Paul committed
35
    void add_operator(operand op)
Paul's avatar
Paul committed
36
    {
Paul's avatar
Paul committed
37
        ops.emplace(op.name(), op);
Paul's avatar
Paul committed
38
39
    }

Paul's avatar
Paul committed
40
    literal eval(std::unordered_map<std::string, argument> params) const;
Paul's avatar
Paul committed
41
42
43
44

private:
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
Paul's avatar
Paul committed
45
46
47
48
49
50
51

    std::unordered_map<std::string, operand> ops;
};

}

#endif