-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
54 lines (47 loc) · 1.67 KB
/
function.c
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
#include "function.h"
#include "interpreter.h"
#include "environment.h"
#include "lexer.h"
#include <sys/time.h>
long long current_time_millis()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (long long)(tv.tv_sec) * 1000 + (tv.tv_usec / 1000);
}
struct Error* native_clock_fun(struct callable_value value, struct Interpreter* intp, Arguments* args, struct lexer_token_value* result)
{
*result = (struct lexer_token_value) {
.type = VALUE_TYPE_INT_LONG_LONG,
.int_long_long_value = current_time_millis() / 1000,
};
return NULL;
}
struct Error* callable_function(struct callable_value value, struct Interpreter* intp, Arguments* args, struct lexer_token_value* result)
{
struct Error* error = NULL;
struct Enviroment* env = env_init(intp->global_env);
for (int i = 0; i < value.declaration->function_stmt.params->count; i++) {
env_define(env, value.declaration->function_stmt.params->items[i].lexeme, args->items[i]);
}
if (has_error(execute_block(intp, value.declaration->function_stmt.body, env))) {
if (error->type == ERROR_RETURN) {
// TODO: change to something else
*result = *(struct lexer_token_value*)(error->message);
env_destroy(env);
return NULL;
}
return trace(error);
}
env_destroy(env);
return NULL;
}
struct lexer_token_value create_function(struct Stmt* declaration)
{
struct lexer_token_value func;
func.type = VALUE_TYPE_CALLABLE;
func.callable_value.call = callable_function;
func.callable_value.declaration = declaration;
func.callable_value.arity = declaration->function_stmt.params->count;
return func;
}