-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.h
More file actions
69 lines (60 loc) · 1.82 KB
/
builtin.h
File metadata and controls
69 lines (60 loc) · 1.82 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef BUILTIN_H
#define BUILTIN_H
#include "object.h"
#include "utils.h"
#include "cleaner.h"
#include <cstdlib>
#include <map>
#include <string>
#include <string_view>
#include <vector>
#include <fmt/format.h>
using obj::Builtin;
using obj::BuiltinFunction;
using obj::Object;
using obj::Error;
static constexpr std::string_view UNSUPPORTED_ARGUMENT_TYPE = "Argumento para longitud sin soporte, se recibió {} cerca de la línea {}";
static constexpr std::string_view WRONG_ARGS_BUILTIN_FN = "Número incorrecto de argumentos para {}, se recibieron {}, se esperaba 1, cerca de la línea {}";
static const BuiltinFunction longitud = [](const std::vector<Object*>& args, const int line) -> Object*
{
if(args.size() != 1)
{
auto error = new Error{
fmt::format(WRONG_ARGS_BUILTIN_FN,
"longitud",
args.size(),
line
)};
eval_errors.push_back(error);
return error;
}
auto argument = dynamic_cast<obj::String*>(args.at(0));
if(argument)
{
if(argument->value.empty())
{
auto integer = new obj::Integer(0);
cleaner.push_back(integer);
return integer;
}
auto integer = new obj::Integer(argument->value.size());
cleaner.push_back(integer);
return integer;
}
auto error = new Error{
fmt::format(UNSUPPORTED_ARGUMENT_TYPE,
args.at(0)->type_string(),
line
)};
eval_errors.push_back(error);
return error;
};
static const BuiltinFunction salir = [](const std::vector<Object*>&, const int) -> Object*
{
exit(EXIT_SUCCESS);
};
static std::map<std::string_view, Builtin> BUILTINS {
{"longitud", Builtin(longitud)},
{"salir", Builtin(salir)},
};
#endif // BUILTIN_H