aboutsummaryrefslogtreecommitdiffstats
path: root/sc2.c
blob: 6264aaba08d16740a43ca518dfd037b0077446a7 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* "sc2.c" R2RS and R3RS procedures not in R4RS.
 * Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/* Author: Aubrey Jaffer */

#include "scm.h"

static char s_last_pair[] = "last-pair";
SCM last_pair(sx)
     SCM sx;
{
  register SCM res = sx;
  register SCM x;
  ASRTER(NIMP(res) && CONSP(res), res, ARG1, s_last_pair);
  while (!0) {
    x = CDR(res);
    if (IMP(x) || NCONSP(x)) return res;
    res = x;
    x = CDR(res);
    if (IMP(x) || NCONSP(x)) return res;
    res = x;
    sx = CDR(sx);
    ASRTER(x != sx, sx, ARG1, s_last_pair);
  }
}

static char s_subml[] = "substring-move-left!";
SCM subml(str1, start1, args)
     SCM str1, start1, args;
{
  SCM end1, str2, start2;
  long i, j, e;
  ASRTER(3==ilength(args), args, WNA, s_subml);
  end1 = CAR(args); args = CDR(args);
  str2 = CAR(args); args = CDR(args);
  start2 = CAR(args);
  ASRTER(NIMP(str1) && STRINGP(str1), str1, ARG1, s_subml);
  ASRTER(INUMP(start1), start1, ARG2, s_subml);
  ASRTER(INUMP(end1), end1, ARG3, s_subml);
  ASRTER(NIMP(str2) && STRINGP(str2), str2, ARG4, s_subml);
  ASRTER(INUMP(start2), start2, ARG5, s_subml);
  i = INUM(start1), j = INUM(start2), e = INUM(end1);
  ASRTER(i <= LENGTH(str1) && i >= 0, start1, OUTOFRANGE, s_subml);
  ASRTER(j <= LENGTH(str2) && j >= 0, start2, OUTOFRANGE, s_subml);
  ASRTER(e <= LENGTH(str1) && e >= 0, end1, OUTOFRANGE, s_subml);
  ASRTER(e-i+j <= LENGTH(str2), start2, OUTOFRANGE, s_subml);
  while(i<e) CHARS(str2)[j++] = CHARS(str1)[i++];
  return UNSPECIFIED;
}
static char s_submr[] = "substring-move-right!";
SCM submr(str1, start1, args)
     SCM str1, start1, args;
{
  SCM end1, str2, start2;
  long i, j, e;
  ASRTER(3==ilength(args), args, WNA, s_submr);
  end1 = CAR(args); args = CDR(args);
  str2 = CAR(args); args = CDR(args);
  start2 = CAR(args);
  ASRTER(NIMP(str1) && STRINGP(str1), str1, ARG1, s_submr);
  ASRTER(INUMP(start1), start1, ARG2, s_submr);
  ASRTER(INUMP(end1), end1, ARG3, s_submr);
  ASRTER(NIMP(str2) && STRINGP(str2), str2, ARG4, s_submr);
  ASRTER(INUMP(start2), start2, ARG5, s_submr);
  i = INUM(start1), j = INUM(start2), e = INUM(end1);
  ASRTER(i <= LENGTH(str1) && i >= 0, start1, OUTOFRANGE, s_submr);
  ASRTER(j <= LENGTH(str2) && j >= 0, start2, OUTOFRANGE, s_submr);
  ASRTER(e <= LENGTH(str1) && e >= 0, end1, OUTOFRANGE, s_submr);
  ASRTER((j = e-i+j) <= LENGTH(str2), start2, OUTOFRANGE, s_submr);
  while(i<e) CHARS(str2)[--j] = CHARS(str1)[--e];
  return UNSPECIFIED;
}
static char s_subfl[] = "substring-fill!";
SCM subfl(str, start, args)
     SCM str, start, args;
{
  SCM end, fill;
  long i, e;
  char c;
  ASRTER(2==ilength(args), args, WNA, s_subfl);
  end = CAR(args); args = CDR(args);
  fill = CAR(args);
  ASRTER(NIMP(str) && STRINGP(str), str, ARG1, s_subfl);
  ASRTER(INUMP(start), start, ARG2, s_subfl);
  ASRTER(INUMP(end), end, ARG3, s_subfl);
  ASRTER(ICHRP(fill), fill, ARG4, s_subfl);
  i = INUM(start), e = INUM(end);c = ICHR(fill);
  ASRTER(i <= LENGTH(str) && i >= 0, start, OUTOFRANGE, s_subfl);
  ASRTER(e <= LENGTH(str) && e >= 0, end, OUTOFRANGE, s_subfl);
  while(i<e) CHARS(str)[i++] = c;
  return UNSPECIFIED;
}

static char s_strnullp[] = "string-null?";
SCM strnullp(str)
     SCM str;
{
  ASRTER(NIMP(str) && STRINGP(str), str, ARG1, s_strnullp);
  if (LENGTH(str)) return BOOL_F;
  else return BOOL_T;
}

static char s_appendb[] = "append!";
SCM appendb(args)
     SCM args;
{
  SCM arg;
 tail:
  if (NULLP(args)) return EOL;
  arg = CAR(args);
  args = CDR(args);
  if (NULLP(args)) return arg;
  if (NULLP(arg)) goto tail;
  ASRTER(NIMP(arg) && CONSP(arg), arg, ARG1, s_appendb);
  CDR(last_pair(arg)) = appendb(args);
  return arg;
}

static iproc lsubr2s[] = {
  {s_subml, subml},
  {s_submr, submr},
  {s_subfl, subfl},
  {0, 0}};

SCM_DLL_EXPORT void init_sc2 P((void));

void init_sc2()
{
  make_subr(s_last_pair, tc7_subr_1, last_pair);
  make_subr(s_strnullp, tc7_subr_1, strnullp);
  make_subr(s_appendb, tc7_lsubr, appendb);
  init_iprocs(lsubr2s, tc7_lsubr_2);
  add_feature("rev2-procedures");
  add_feature("rev3-procedures");
}