summaryrefslogtreecommitdiff
path: root/src/runtime/fscan.r
blob: 9e974d87b8fb920d2025b098334aec5f65efd08f (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
/*
 * File: fscan.r
 *  Contents: move, pos, tab.
 */

"move(i) - move &pos by i, return substring of &subject spanned."
" Reverses effects if resumed."

function{0,1+} move(i)

   if !cnv:C_integer(i) then
      runerr(101,i)

   abstract {
      return string
      }

   body {
      register C_integer j;
      C_integer oldpos;

      /*
       * Save old &pos.  Local variable j holds &pos before the move.
       */
      oldpos = j = k_pos;

      /*
       * If attempted move is past either end of the string, fail.
       */
      if (i + j <= 0 || i + j > StrLen(k_subject) + 1)
         fail;

      /*
       * Set new &pos.
       */
      k_pos += i;

      /*
       * Make sure i >= 0.
       */
      if (i < 0) {
         j += i;
         i = -i;
         }

      /*
       * Suspend substring of &subject that was moved over.
       */
      suspend string(i, StrLoc(k_subject) + j - 1);

      /*
       * If move is resumed, restore the old position and fail.
       */
      if (oldpos > StrLen(k_subject) + 1)
         runerr(205, kywd_pos);
      else {
         k_pos = oldpos;
         }

      fail;
      }
end


"pos(i) - test if &pos is at position i in &subject."

function{0,1} pos(i)

   if !cnv:C_integer(i) then
      runerr(101, i)

   abstract {
      return integer
      }
   body {
      /*
       * Fail if &pos is not equivalent to i, return i otherwise.
       */
      if ((i = cvpos(i, StrLen(k_subject))) != k_pos)
         fail;
      return C_integer i;
      }
end


"tab(i) - set &pos to i, return substring of &subject spanned."
"Reverses effects if resumed."

function{0,1+} tab(i)

   if !cnv:C_integer(i) then
      runerr(101, i);

   abstract {
      return string
      }

   body {
      C_integer j, t, oldpos;

      /*
       * Convert i to an absolute position.
       */
      i = cvpos(i, StrLen(k_subject));
      if (i == CvtFail)
         fail;

      /*
       * Save old &pos.  Local variable j holds &pos before the tab.
       */
      oldpos = j = k_pos;

      /*
       * Set new &pos.
       */
      k_pos = i;

      /*
       *  Make i the length of the substring &subject[i:j]
       */
      if (j > i) {
         t = j;
         j = i;
         i = t - j;
         }
      else
         i = i - j;

      /*
       * Suspend the portion of &subject that was tabbed over.
       */
      suspend string(i, StrLoc(k_subject) + j - 1);

      /*
       * If tab is resumed, restore the old position and fail.
       */
      if (oldpos > StrLen(k_subject) + 1)
         runerr(205, kywd_pos);
      else {
         k_pos = oldpos;
         }

      fail;
      }
end