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
|
#include "uscript.h"
#include <stdio.h>
#include "dyn_arr.h"
#include "lex.h"
#include "val.h"
#include "vm.h"
#include "parser.h"
void core_print(int argc)
{
(void)argc;
char *str = val_to_str(vm_peek(), NULL);
olog(str);
mem_free(str);
vm_push(create_zilch());
}
void core_len(int argc)
{
(void)argc;
struct us_val val = vm_peek();
switch (val.type) {
case VAL_ARR:
vm_push(create_num(da_len(get_arr(val)->e)));
break;
case VAL_STR:
vm_push(create_num(get_str(val)->len));
break;
default:
vm_push(create_zilch());
}
}
void us_init(void)
{
init_vm();
us_set_cfunc("core:len", core_len, 1);
us_set_cfunc("core:log", core_print, 1);
}
void us_deinit(void)
{
deinit_vm();
}
void us_load_file(const char *file_path)
{
char *file = read_file(file_path, NULL);
us_load_src(file);
mem_free(file);
}
void us_load_src(const char *src)
{
struct us_proto *proto = compile("main", src);
if (!proto)
return;
us_exec(create_func(proto));
}
int us_declare_global(const char *name)
{
return declare_global(copy_str(name, -1));
}
void us_set_cfunc(const char *c_name, us_cfunc_sig c, int argc)
{
struct us_str *name = copy_str(c_name, -1);
int global = declare_global(name);
set_global(global, wrap_cfunc(create_cfunc(name, c, argc)));
}
|