-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCall.hpp
More file actions
48 lines (42 loc) · 1.58 KB
/
Call.hpp
File metadata and controls
48 lines (42 loc) · 1.58 KB
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
#ifndef Call_h
#define Call_h
#include "FwdTypes.hpp"
#include "Stmt.hpp"
#include <vector>
#include <string>
#include <ctime>
class Environment;
class Interpreter;
class Expr;
class LoxInstance;
class LoxCallable {
public:
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) = 0;
virtual const size_t arity() const = 0;
virtual const std::string toString() const = 0;
};
class LoxFunction : public LoxCallable {
public:
std::string name;
std::vector<std::string> params;
std::shared_ptr<StmtBlock> body;
std::shared_ptr<Environment> closure;
bool isInitializer;
public:
LoxFunction() = default;
LoxFunction(const StmtFunction& declaration, std::shared_ptr<Environment> closure, bool isInitializer)
: name{ declaration.getName() }, params{ declaration.getParams() },
body { declaration.getBody() }, closure{ closure }, isInitializer{ isInitializer } {}
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override;
virtual const size_t arity() const override { return params.size(); }
virtual const std::string toString() const override { return "<fn " + name + '>'; }
std::shared_ptr<LoxFunction> bind(std::shared_ptr<LoxInstance>);
};
class Clock : public LoxCallable {
virtual Value call(const Interpreter&, const std::vector<std::shared_ptr<Expr>>&) override {
return static_cast<double>(clock());
}
virtual const size_t arity() const override { return 0; }
virtual const std::string toString() const override { return "<native fn>"; }
};
#endif // Call_h