-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.h
More file actions
63 lines (56 loc) · 1.58 KB
/
Copy pathbuiltin.h
File metadata and controls
63 lines (56 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef MICROL_STDLIB_H
#define MICROL_STDLIB_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ctx.h"
void stdlib_error_arg_count(const char *from, size_t expected, size_t got)
{
fprintf(stderr, "error: %s -> expected %zu argument%s, but got %zu\n",
from,
expected,
expected == 1 ? "" : "s", got);
}
// no more than 256 types i suppose.
void stdlib_error_arg_type(const char *from, char expected, char got)
{
fprintf(stderr, "error: %s -> expected %s, but got %s\n",
from, string_type(expected), string_type(got));
}
obj_t *stdlib_decl_tostring(ctx_t *ctx, size_t count, obj_t *args[])
{
//puts("tostring");
if(count != 1)
{
stdlib_error_arg_count("tostring()", 1, count);
return NULL;
}
int sz = string_obj(NULL, 0, args[0]);
if(sz == 0) return create_str_obj(ctx, "");
char str[sz + 1];
string_obj(str, sz + 1, args[0]);
return create_str_obj(ctx, str);
}
obj_t *stdlib_decl_write(ctx_t *ctx, size_t count, obj_t *args[])
{
//puts("write");
if(count != 1)
{
stdlib_error_arg_count("__write()", 1, count);
return NULL;
}
if(args[0]->type != ot_str)
{
stdlib_error_arg_type("__write()", ot_str, args[0]->type);
return NULL;
}
printf("%s\n", ((obj_str_t*)args[0])->str);
return NULL;
}
void stdlib_context(ctx_t *ctx)
{
if(!ctx) return;
add_var(ctx, create_var("__write", create_nat_obj(ctx, stdlib_decl_write), false));
add_var(ctx, create_var("tostring", create_nat_obj(ctx, stdlib_decl_tostring), false));
}
#endif//MICROL_STDLIB_H