tcalc 0.2.0
 
Loading...
Searching...
No Matches
function.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <string>
15#include <utility>
16#include <vector>
17
18#include "tcalc/ast/node.hpp"
19#include "tcalc/common.hpp"
20
21namespace tcalc::ast {
22
27class FcallNode : public Node
28{
29private:
30 std::string _name;
31 std::vector<NodePtr<>> _args;
32
33public:
39 explicit FcallNode(std::string name)
40 : FcallNode{ std::move(name), {} }
41 {
42 }
43
50 FcallNode(std::string name, std::vector<NodePtr<>> args)
51 : Node{ NodeType::FCALL }
52 , _name{ std::move(name) }
53 , _args{ std::move(args) }
54 {
55 }
56
57 ~FcallNode() override = default;
58
64 [[nodiscard]] TCALC_INLINE auto& args() const noexcept { return _args; }
65
71 TCALC_INLINE auto& args() noexcept { return _args; }
72
78 TCALC_INLINE void push_arg(const NodePtr<>& arg) { _args.push_back(arg); }
79
85 [[nodiscard]] TCALC_INLINE auto& name() const noexcept { return _name; }
86
92 TCALC_INLINE void name(std::string name) { _name = std::move(name); }
93};
94
99class FdefNode : public Node
100{
101private:
102 std::string _name;
103 std::vector<std::string> _args;
104 NodePtr<> _body;
105
106public:
112 explicit FdefNode(std::string name)
113 : FdefNode{ std::move(name), {}, {} }
114 {
115 }
116
124 FdefNode(std::string name, std::vector<std::string> args, NodePtr<> body)
125 : Node{ NodeType::FDEF }
126 , _name{ std::move(name) }
127 , _args{ std::move(args) }
128 , _body{ std::move(body) }
129 {
130 }
131
132 ~FdefNode() override = default;
133
139 [[nodiscard]] TCALC_INLINE auto& args() const noexcept { return _args; }
140
146 TCALC_INLINE auto& args() noexcept { return _args; }
147
153 TCALC_INLINE void push_arg(const std::string& arg) { _args.push_back(arg); }
154
160 [[nodiscard]] TCALC_INLINE auto& body() const noexcept { return _body; }
161
167 TCALC_INLINE auto& body() noexcept { return _body; }
168
174 TCALC_INLINE void body(NodePtr<> body) { _body = std::move(body); }
175
181 [[nodiscard]] TCALC_INLINE auto& name() const noexcept { return _name; }
182
188 TCALC_INLINE void name(std::string name) { _name = std::move(name); }
189};
190
191}
Function node.
Definition function.hpp:28
FcallNode(std::string name)
Construct a new Function Node object.
Definition function.hpp:39
TCALC_INLINE auto & args() const noexcept
Get function arguments.
Definition function.hpp:64
TCALC_INLINE void name(std::string name)
Set function name.
Definition function.hpp:92
TCALC_INLINE auto & args() noexcept
Get function arguments.
Definition function.hpp:71
FcallNode(std::string name, std::vector< NodePtr<> > args)
Construct a new Function Node object.
Definition function.hpp:50
TCALC_INLINE void push_arg(const NodePtr<> &arg)
Push a function argument.
Definition function.hpp:78
TCALC_INLINE auto & name() const noexcept
Get function name.
Definition function.hpp:85
Function definition node.
Definition function.hpp:100
TCALC_INLINE auto & body() const noexcept
Get function body.
Definition function.hpp:160
TCALC_INLINE auto & name() const noexcept
Get function name.
Definition function.hpp:181
TCALC_INLINE auto & args() noexcept
Get function arguments.
Definition function.hpp:146
TCALC_INLINE void name(std::string name)
Set function name.
Definition function.hpp:188
TCALC_INLINE void body(NodePtr<> body)
Set function body.
Definition function.hpp:174
TCALC_INLINE auto & args() const noexcept
Get function arguments.
Definition function.hpp:139
TCALC_INLINE auto & body() noexcept
Get function body.
Definition function.hpp:167
FdefNode(std::string name)
Construct a new Fdef Node object only with name.
Definition function.hpp:112
FdefNode(std::string name, std::vector< std::string > args, NodePtr<> body)
Construct a new Fdef Node object with arguments and body.
Definition function.hpp:124
TCALC_INLINE void push_arg(const std::string &arg)
Push a function argument.
Definition function.hpp:153
Base class for AST nodes.
Definition node.hpp:87
tcalc common header.
Base class for AST nodes.
std::shared_ptr< NT > NodePtr
Shared pointer to a node.
Definition node.hpp:119
NodeType
AST node type.
Definition node.hpp:30