-
Notifications
You must be signed in to change notification settings - Fork 0
/
ty_string.c
93 lines (76 loc) · 2.6 KB
/
ty_string.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
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
lac -- a laconic lisp interpreter
Copyright (C) 2010 Gianluca Guida
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 2 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.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* String type */
#include "laconic.h"
#include <gc/gc.h>
#include <stdio.h>
#include <string.h>
#define ARGEVAL(_lr, _e) ((_e) == NULL ? _lr : eval((_lr), (_e)))
static void string_print(FILE *fd, lreg_t lr)
{
char *s;
s = (char *)lreg_raw_ptr(lr);
fprintf(fd, "\"%s\" ", s);
}
static lreg_t string_eq(lreg_t arg1, lreg_t arg2)
{
char *s1, *s2;
s1 = (char *)lreg_raw_ptr(arg1);
s2 = (char *)lreg_raw_ptr(arg2);
if ( s1 == s2 )
return sym_true;
return sym_false;
}
static int string_compare(lreg_t arg1, lreg_t arg2)
{
int d;
char *s1, *s2;
s1 = (char *)lreg_raw_ptr(arg1);
s2 = (char *)lreg_raw_ptr(arg2);
d = strcmp(s1, s2);
return d;
}
#define BINARY_STR_OP_CHECKS(args) \
_EXPECT_ARGS(args, 2); \
lreg_t s1 = ARGEVAL(car(args), argenv); \
lreg_t s2 = ARGEVAL(car(cdr(args)), argenv); \
\
if ( lreg_type(s1) != lreg_type(s2) \
|| !(lreg_type(s1) == LREG_STRING) ) \
_ERROR_AND_RET("Function requires two strings!\n");
LAC_API lreg_t proc_string_lessp(lreg_t args, lenv_t *argenv, lenv_t *env)
{
BINARY_STR_OP_CHECKS(args);
return (string_compare(s1, s2) >= 0 ? sym_false : sym_true);
}
LAC_API static lreg_t proc_string_greaterp(lreg_t args, lenv_t *argenv, lenv_t *env)
{
BINARY_STR_OP_CHECKS(args);
return (string_compare(s1, s2) <= 0 ? sym_false : sym_true);
}
LAC_API static lreg_t proc_string_equal(lreg_t args, lenv_t *argenv, lenv_t *env)
{
BINARY_STR_OP_CHECKS(args);
return (string_compare(s1, s2) != 0 ? sym_false : sym_true);
}
LAC_DEFINE_TYPE_PFUNC(string, LREG_STRING)
void string_init(lenv_t *env)
{
lac_extproc_register(env, "STRINGP", LAC_TYPE_PFUNC(string));
lac_extproc_register(env, "STRING-LESS", proc_string_lessp);
lac_extproc_register(env, "STRING-GREATER", proc_string_greaterp);
lac_extproc_register(env, "STRING-EQUAL", proc_string_equal);
}