-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.h
93 lines (78 loc) · 2.68 KB
/
functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "ast.h"
#include "variables.h"
class FunctionCallAST : public ExprAST {
public:
FunctionCallAST(Token tok,
BasicContext* ctx,
std::string function,
std::vector<std::unique_ptr<ExprAST>> args)
: ExprAST(std::move(tok), ctx), _function(std::move(function)), _args(std::move(args)) {}
llvm::Value* codegen() override;
static std::unique_ptr<ExprAST> parse(const Token& tok, BasicContext* ctx);
private:
std::string _function;
std::vector<std::unique_ptr<ExprAST>> _args;
};
class ArgumentAST;
class ProtoDefAST : public ExprAST {
public:
ProtoDefAST(Token tok,
BasicContext* ctx,
std::string name,
std::vector<ArgumentAST> args,
bool isVarArg,
VariableType returnType)
: ExprAST(std::move(tok), ctx),
_name(std::move(name)),
_args(std::move(args)),
_isVarArg(isVarArg),
_returnType(returnType) {}
llvm::Value* codegen() override;
static std::unique_ptr<ExprAST> parse(const Token& tok, BasicContext* ctx);
const std::string& name() const {
return _name;
}
std::vector<ArgumentAST>& args() {
return _args;
}
VariableType returnType() const {
return _returnType;
}
private:
std::string _name;
std::vector<ArgumentAST> _args;
bool _isVarArg;
VariableType _returnType;
llvm::Function* _function = nullptr;
};
class FunctionAST : public ExprAST {
public:
FunctionAST(Token tok,
BasicContext* ctx,
std::unique_ptr<ProtoDefAST> proto,
std::unique_ptr<ExprAST> body)
: ExprAST(std::move(tok), ctx), _proto(std::move(proto)), _body(std::move(body)) {}
FunctionAST(Token tok, BasicContext* ctx, std::unique_ptr<ProtoDefAST> proto)
: ExprAST(tok, ctx),
_proto(std::move(proto)),
_body(std::make_unique<BlockAST>(tok, ctx)) {}
llvm::Value* codegen() override;
llvm::Function* function() const;
static std::unique_ptr<ExprAST> parse(const Token& tok, BasicContext* ctx);
void addStatement(std::unique_ptr<ExprAST> statement);
ProtoDefAST* proto() const {
return _proto.get();
}
private:
void _setBody(std::unique_ptr<ExprAST> body);
llvm::Function* _function = nullptr;
std::unique_ptr<ProtoDefAST> _proto;
std::unique_ptr<ExprAST> _body;
};
class ReturnAST : public ExprAST {
public:
ReturnAST(Token tok, BasicContext* ctx) : ExprAST(std::move(tok), ctx) {}
llvm::Value* codegen() override;
static std::unique_ptr<ExprAST> parse(const Token& tok, BasicContext* ctx);
};