-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cpp
161 lines (147 loc) · 3.99 KB
/
Program.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Program.h"
#include "Object.h"
#include "AST.hpp"
#define NATIVE_FUNC(name) definedFunctions[ name ] = static_cast<Function*>(new NativeFunction( name , []([[maybe_unused]] Interpreter& interp, [[maybe_unused]] const std::vector<Value>& args)
HashTable<std::string,ObjectType*> Program::construct_natives()
{
globals.table["__VERSION"] = Value(std::string(VERSION_STRING));
globals.table["__VERSION_MAJOR"] = Value(VERSION_MAJOR);
globals.table["__VERSION_MINOR"] = Value(VERSION_MINOR);
globals.table["__VERSION_PATCH"] = Value(VERSION_PATCH);
//TEXT MANIPULATION
NATIVE_FUNC("print") // Lua-style print() function
{
if (args.size())
{
std::cout << args[0].to_string();
for (int i = 1; i < args.size(); ++i)
{
std::cout << '\t' << args[i].to_string();
}
}
std::cout << "\n";
return Value();
}));
#ifndef JOAO_SAFE
NATIVE_FUNC("input") // Analogous to Lua's io.read() function.
{
if (args.size())
{
const Value& arg = args[0];
if (arg.t_vType == Value::vType::Integer) // Attempts to read in a specific number of characters
{
char* str = new char[arg.t_value.as_int + 1];
std::cin.read(str, arg.t_value.as_int);
str[arg.t_value.as_int] = '\0'; // Just making absolutely sure that this is null-terminated
return Value(std::string(str));
}
}
std::string imp;
std::cin >> imp;
if(imp.empty())
return Value();
return Value(imp);
}));
#endif
NATIVE_FUNC("tostring")
{
return Value(args[0].to_string());
}));
NATIVE_FUNC("tointeger")
{
if(!args.size())
return Value(Value::vType::Null, int(ErrorCode::NotEnoughArgs));
Value arg = args[0];
switch (arg.t_vType)
{
case(Value::vType::Double):
return Value(int(arg.t_value.as_double));
case(Value::vType::Integer):
return arg;
case(Value::vType::Bool):
if (arg.t_value.as_bool)
return Value(1);
else
return Value(0);
default: // FIXME: Add string-to-int
return Value(Value::vType::Null, int(ErrorCode::BadArgType));
}
return Value(args[0].to_string());
}));
NATIVE_FUNC("typeof")
{
if(!args.size())
return Value(Value::vType::Null, int(ErrorCode::NotEnoughArgs));
Value arg = args[0];
std::string str;
switch (arg.t_vType)
{
case(Value::vType::Double):
str = "Double";
break;
case(Value::vType::Integer):
str = "Integer";
break;
case(Value::vType::Bool):
str = "Boolean";
break;
case(Value::vType::Object):
str = "Object";
break;
case(Value::vType::String):
str = "String";
break;
case(Value::vType::Null):
str = "Null";
break;
case(Value::vType::Function):
str = "Function";
break;
default:
str = "Unknown"; // This is very strange, if it were to happen.
break;
}
return Value(str);
}));
NATIVE_FUNC("istable")
{
if(!args.size())
return Value(Value::vType::Null, int(ErrorCode::NotEnoughArgs));
const Value& arg = args[0];
switch (arg.t_vType)
{
case(Value::vType::Object):
return Value(arg.t_value.as_object_ptr->is_table());
default:
return Value(false);
}
}));
NATIVE_FUNC("isnull")
{
if(!args.size())
return Value(Value::vType::Null, int(ErrorCode::NotEnoughArgs));
return Value(args[0].t_vType == Value::vType::Null);
}));
NATIVE_FUNC("classof")
{
if(!args.size())
return Value(Value::vType::Null, int(ErrorCode::NotEnoughArgs));
Value arg = args[0];
if(arg.t_vType != Value::vType::Object)
return Value(Value::vType::Null, int(ErrorCode::BadArgType));
return Value(arg.t_value.as_object_ptr->object_type.to_string());
}));
NATIVE_FUNC("void_stellakafuhparenthessisluaunderscorestatewithacapitalscommaluaunderscoreallocfvoidstarud")
{
return Value(7); // No I'm never removing this function fuck you
}));
HashTable<std::string, ObjectType*> cooked_classes;
construct_math_library();
construct_string_library();
cooked_classes["/table"] = construct_table_library();
#ifndef JOAO_SAFE
cooked_classes["/file"] = construct_file_library();
#endif
cooked_classes["/error"] = construct_error_library();
return cooked_classes;
}