-
Notifications
You must be signed in to change notification settings - Fork 30
/
snek-builtin.c
80 lines (71 loc) · 1.77 KB
/
snek-builtin.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
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
/*
* Copyright © 2018 Keith Packard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "snek.h"
#include <math.h>
snek_poly_t
snek_builtin_len(snek_poly_t a)
{
return snek_soffset_to_poly(snek_poly_len(a));
}
snek_poly_t
snek_builtin_print(uint8_t nposition, uint8_t nnamed, snek_poly_t *args)
{
while (nposition--) {
snek_poly_t arg = *args++;
snek_poly_print(stdout, arg, 's');
if (nposition)
putc(' ', stdout);
}
snek_poly_t end = SNEK_NULL;
while (nnamed--) {
snek_id_t id = (snek_id_t) ((*args++).f);
snek_poly_t value = *args++;
if (id == SNEK_BUILTIN_end)
end = value;
}
if (!snek_is_null(end))
snek_poly_print(stdout, end, 's');
else
putc('\n', stdout);
return SNEK_NULL;
}
snek_poly_t
snek_builtin_sys_stdout_flush(void)
{
fflush(stdout);
return SNEK_NULL;
}
snek_poly_t
snek_builtin_ord(snek_poly_t a)
{
if (snek_poly_type(a) != snek_string)
return snek_error_type_1(a);
return snek_soffset_to_poly(snek_poly_to_string(a)[0]);
}
snek_poly_t
snek_builtin_chr(snek_poly_t a)
{
snek_soffset_t s = snek_poly_get_soffset(a);
return snek_string_make(s);
}
snek_poly_t
snek_builtin_math_sqrt(snek_poly_t a)
{
return snek_float_to_poly(sqrtf(snek_poly_get_float(a)));
}
snek_poly_t
snek_builtin_abs(snek_poly_t a)
{
return snek_float_to_poly(fabs(snek_poly_get_float(a)));
}