program.hpp 1.37 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
9
10
11
12

namespace rtg {

struct program
{
Paul's avatar
Paul committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    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
28
29
30
31
32
33
34

    instruction * add_parameter(std::string name, shape s)
    {
        instructions.push_back({"param:"+std::move(name), s, {}});
        return std::addressof(instructions.back());
    }

Paul's avatar
Paul committed
35
36
37
38
39
40
41
42
43
44
    template<class Op, class Shape>
    void add_operator(std::string name, Op op, Shape s)
    {
        operand result;
        result.name = name;
        result.compute = op;
        result.compute_shape = s;
        ops.emplace(name, result);
    }

Paul's avatar
Paul committed
45
    literal eval(std::unordered_map<std::string, argument> params) const;
Paul's avatar
Paul committed
46
47
48
49

private:
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
Paul's avatar
Paul committed
50
51
52
53
54
55
56
57

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

};

}

#endif