• R/O
  • SSH
  • HTTPS

npl: コミット


コミットメタ情報

リビジョン68 (tree)
日時2017-09-13 03:38:27
作者tamiya25

ログメッセージ

sblock削除(stackへ統合)

変更サマリ

差分

--- trunk/npl/sblock.c (revision 67)
+++ trunk/npl/sblock.c (revision 68)
@@ -1,110 +0,0 @@
1-// sblock.c -- スタック用ブロック
2-
3-#include "global.h"
4-
5-npl_sblock_t* npl_sblock_new(size_t block_size)
6-{
7- npl_block_t *block;
8- npl_sblock_t *sb;
9-
10- // オブジェクト生成
11- block = npl_block_alloc(npl_sblock_type, block_size);
12- if (block == NULL)
13- return NULL;
14-
15- // データ領域がないものは生成を拒否する。
16- if (NPL_CHUNK(block)->chunk == NULL) {
17- npl_object_unref(NPL_OBJECT(block));
18- return NULL;
19- }
20-
21- // 初期化
22- sb = NPL_SBLOCK(block);
23- sb->sp = 0;
24-
25- return sb;
26-}
27-
28-void npl_sblock_set_prev(npl_sblock_t *sb, npl_sblock_t *prev)
29-{
30- assert(sb);
31-
32- if (prev != NULL)
33- npl_block_set_link(NPL_BLOCK(sb), NPL_BLOCK(prev));
34- else
35- npl_block_set_link(NPL_BLOCK(sb), NULL);
36-}
37-
38-void* npl_sblock_push(npl_sblock_t *sb, size_t bytes)
39-{
40- size_t new_sp;
41- unsigned char *d;
42-
43- assert(sb);
44- assert(bytes > 0);
45-
46- if (npl_add(sb->sp, bytes, &new_sp))
47- return NULL; // プッシュできない
48- if (new_sp > NPL_CHUNK(sb)->bytes)
49- return NULL; // プッシュできない
50-
51- d = NPL_CHUNK(sb)->chunk;
52- d = d + sb->sp;
53- sb->sp = new_sp;
54-
55- return d;
56-}
57-
58-void* npl_sblock_get(npl_sblock_t *sb, size_t bytes)
59-{
60- size_t sp;
61- unsigned char *s;
62-
63- assert(sb);
64- assert(bytes > 0);
65-
66- sp = sb->sp;
67- if (npl_sub(sp, bytes, &sp))
68- return NULL; // 要素なし
69-
70- s = NPL_CHUNK(sb)->chunk;
71- s = s + sp;
72- return s;
73-}
74-
75-int npl_sblock_remove(npl_sblock_t *sb, size_t bytes)
76-{
77- size_t new_sp;
78-
79- assert(sb);
80- assert(bytes > 0);
81-
82- if (npl_sub(sb->sp, bytes, &new_sp))
83- return 1; // ポップできない
84-
85- sb->sp = new_sp;
86- return 0;
87-}
88-
89-void npl_sblock_clear(npl_sblock_t *sb)
90-{
91- assert(sb);
92- npl_sblock_set_prev(sb, NULL);
93- sb->sp = 0;
94-}
95-
96-// sblock クラスの型情報
97-npl_type_t _npl_sblock_type = {
98- .object = {
99- .type = &_npl_type_type,
100- .ref_count = 0,
101- .static_flag = 1,
102- },
103- .name = L"sblock",
104- .destructor = NULL,
105- .parent = &_npl_block_type,
106- .size = sizeof(npl_sblock_t),
107-};
108-
109-npl_type_t *npl_sblock_type = &_npl_sblock_type;
110-
--- trunk/npl/doc/index.html (revision 67)
+++ trunk/npl/doc/index.html (revision 68)
@@ -7,7 +7,7 @@
77 </head>
88 <body>
99
10-<div align="right">2017.9.3</div>
10+<div align="right">2017.9.13</div>
1111
1212 <h1>Nandemo Programming Language (NPL)</h1>
1313 <p>どんなプログラミング言語でも揃ってる、という意味です。</p>
@@ -38,7 +38,6 @@
3838 <li>func -- 関数</li>
3939 <li>member -- メンバ</li>
4040 <li>state -- 状態</li>
41- <li>sblock</li>
4241 <li>stack -- スタック</li>
4342 <li>stream -- ストリーム</li>
4443 </ul>
--- trunk/npl/Makefile (revision 67)
+++ trunk/npl/Makefile (revision 68)
@@ -2,12 +2,12 @@
22
33 libnpl.a: npl.o object.o stream.o str.o mbstr.o type.o sbuf.o \
44 array.o buf.o chunk.o val.o var.o tuple.o func.o \
5-member.o state.o stack.o def.o block.o sblock.o
5+member.o state.o stack.o def.o block.o
66 ar rcs libnpl.a npl.o object.o stream.o str.o \
77 mbstr.o type.o sbuf.o array.o buf.o \
88 chunk.o val.o var.o tuple.o \
99 func.o member.o state.o stack.o \
10- def.o block.o sblock.o
10+ def.o block.o
1111
1212 npl.o: npl.c npl.h global.h
1313 gcc -c npl.c
@@ -66,9 +66,6 @@
6666 block.o: block.c npl.h global.h
6767 gcc -c block.c
6868
69-sblock.o: sblock.c npl.h global.h
70- gcc -c sblock.c
71-
7269 clean:
7370 rm -f *.o
7471 rm -f libnpl.a
--- trunk/npl/npl.h (revision 67)
+++ trunk/npl/npl.h (revision 68)
@@ -24,7 +24,6 @@
2424 #define NPL_FUNC(x) _NPL_CAST(npl_func_t, x, npl_func_type)
2525 #define NPL_MEMBER(x) _NPL_CAST(npl_member_t, x, npl_member_type)
2626 #define NPL_STATE(x) _NPL_CAST(npl_state_t, x, npl_state_type)
27-#define NPL_SBLOCK(x) _NPL_CAST(npl_sblock_t, x, npl_sblock_type)
2827 #define NPL_STACK(x) _NPL_CAST(npl_stack_t, x, npl_stack_type)
2928 #define NPL_STREAM(x) _NPL_CAST(npl_stream_t, x, npl_stream_type)
3029
@@ -49,7 +48,6 @@
4948 typedef struct npl_func_t npl_func_t;
5049 typedef struct npl_member_t npl_member_t;
5150 typedef struct npl_state_t npl_state_t;
52-typedef struct npl_sblock_t npl_sblock_t;
5351 typedef struct npl_stack_t npl_stack_t;
5452 typedef struct npl_stream_t npl_stream_t;
5553
@@ -82,7 +80,6 @@
8280 extern npl_type_t _npl_func_type;
8381 extern npl_type_t _npl_member_type;
8482 extern npl_type_t _npl_state_type;
85-extern npl_type_t _npl_sblock_type;
8683 extern npl_type_t _npl_stack_type;
8784 extern npl_type_t _npl_stream_type;
8885
@@ -101,7 +98,6 @@
10198 extern npl_type_t *npl_func_type;
10299 extern npl_type_t *npl_member_type;
103100 extern npl_type_t *npl_state_type;
104-extern npl_type_t *npl_sblock_type;
105101 extern npl_type_t *npl_stack_type;
106102 extern npl_type_t *npl_stream_type;
107103
@@ -448,19 +444,6 @@
448444 npl_stack_t *stack;
449445 };
450446
451-// sblock class
452-struct npl_sblock_t {
453- npl_block_t block;
454- size_t sp; // このブロック中でのスタックポインタ位置
455-};
456-
457-npl_sblock_t* npl_sblock_new(size_t block_size);
458-void npl_sblock_set_prev(npl_sblock_t *sb, npl_sblock_t *prev);
459-void* npl_sblock_push(npl_sblock_t *sb, size_t bytes);
460-void* npl_sblock_get(npl_sblock_t *sb, size_t bytes);
461-int npl_sblock_remove(npl_sblock_t *sb, size_t bytes);
462-void npl_sblock_clear(npl_sblock_t *sb);
463-
464447 // stack class
465448 struct npl_stack_frame_info_t {
466449 size_t data_size;
@@ -475,8 +458,8 @@
475458
476459 struct npl_stack_t {
477460 npl_object_t object;
478- npl_sblock_t *stack;
479- npl_sblock_t *unused;
461+ npl_block_t *stack;
462+ npl_block_t *unused;
480463 size_t block_size;
481464 size_t count;
482465 size_t stack_count;
--- trunk/npl/lang/calcf.c (revision 67)
+++ trunk/npl/lang/calcf.c (revision 68)
@@ -75,6 +75,7 @@
7575 }
7676
7777 if (var == NULL) {
78+ int f = 0;
7879 npl_val_t val2;
7980
8081 if ((var = npl_var_new(name)) == NULL) {
@@ -95,8 +96,11 @@
9596 if (npl_val_check_null(&val))
9697 continue;
9798 npl_array_set(vlist, i, &val2);
99+ f = 1;
98100 break;
99101 }
102+ if (!f)
103+ npl_array_set(vlist, len, &val2);
100104 npl_val_end(&val2);
101105 }
102106 npl_val_end(&val);
--- trunk/npl/lang/calci.c (revision 67)
+++ trunk/npl/lang/calci.c (revision 68)
@@ -75,6 +75,7 @@
7575 }
7676
7777 if (var == NULL) {
78+ int f;
7879 npl_val_t val2;
7980
8081 if ((var = npl_var_new(name)) == NULL) {
@@ -95,8 +96,11 @@
9596 if (npl_val_check_null(&val))
9697 continue;
9798 npl_array_set(vlist, i, &val2);
99+ f = 1;
98100 break;
99101 }
102+ if (!f)
103+ npl_array_set(vlist, len, &val2);
100104 npl_val_end(&val2);
101105 }
102106 npl_val_end(&val);
--- trunk/npl/stack.c (revision 67)
+++ trunk/npl/stack.c (revision 68)
@@ -1,5 +1,128 @@
11 #include "global.h"
22
3+/*
4+ * スタック用のブロック(内部クラス)の定義
5+ */
6+#define _SBLOCK(x) _NPL_CAST(_SBlock, x, _sblock_type)
7+
8+typedef struct _SBlock _SBlock;
9+struct _SBlock {
10+ npl_block_t block;
11+ size_t sp; // このブロック中でのスタックポインタ位置
12+};
13+
14+// sblock クラスの型情報
15+static npl_type_t _sblock_type_in = {
16+ .object = {
17+ .type = &_npl_type_type,
18+ .ref_count = 0,
19+ .static_flag = 1,
20+ },
21+ .name = L"_sblock",
22+ .destructor = NULL,
23+ .parent = &_npl_block_type,
24+ .size = sizeof(_SBlock),
25+};
26+
27+static npl_type_t *_sblock_type = &_sblock_type_in;
28+
29+static _SBlock* _sblock_new(size_t block_size)
30+{
31+ npl_block_t *block;
32+ _SBlock *sb;
33+
34+ // オブジェクト生成
35+ block = npl_block_alloc(_sblock_type, block_size);
36+ if (block == NULL)
37+ return NULL;
38+
39+ // データ領域がないものは生成を拒否する。
40+ if (NPL_CHUNK(block)->chunk == NULL) {
41+ npl_object_unref(NPL_OBJECT(block));
42+ return NULL;
43+ }
44+
45+ // 初期化
46+ sb = _SBLOCK(block);
47+ sb->sp = 0;
48+
49+ return sb;
50+}
51+
52+static void _sblock_set_prev(_SBlock *sb, _SBlock *prev)
53+{
54+ assert(sb);
55+
56+ if (prev != NULL)
57+ npl_block_set_link(NPL_BLOCK(sb), NPL_BLOCK(prev));
58+ else
59+ npl_block_set_link(NPL_BLOCK(sb), NULL);
60+}
61+
62+static void* _sblock_push(_SBlock *sb, size_t bytes)
63+{
64+ size_t new_sp;
65+ unsigned char *d;
66+
67+ assert(sb);
68+ assert(bytes > 0);
69+
70+ if (npl_add(sb->sp, bytes, &new_sp))
71+ return NULL; // プッシュできない
72+ if (new_sp > NPL_CHUNK(sb)->bytes)
73+ return NULL; // プッシュできない
74+
75+ d = NPL_CHUNK(sb)->chunk;
76+ d = d + sb->sp;
77+ sb->sp = new_sp;
78+
79+ return d;
80+}
81+
82+static void* _sblock_get(_SBlock *sb, size_t bytes)
83+{
84+ size_t sp;
85+ unsigned char *s;
86+
87+ assert(sb);
88+ assert(bytes > 0);
89+
90+ sp = sb->sp;
91+ if (npl_sub(sp, bytes, &sp))
92+ return NULL; // 要素なし
93+
94+ s = NPL_CHUNK(sb)->chunk;
95+ s = s + sp;
96+ return s;
97+}
98+
99+static int _sblock_remove(_SBlock *sb, size_t bytes)
100+{
101+ size_t new_sp;
102+
103+ assert(sb);
104+ assert(bytes > 0);
105+
106+ if (npl_sub(sb->sp, bytes, &new_sp))
107+ return 1; // ポップできない
108+
109+ sb->sp = new_sp;
110+ return 0;
111+}
112+
113+static void _sblock_clear(_SBlock *sb)
114+{
115+ assert(sb);
116+ _sblock_set_prev(sb, NULL);
117+ sb->sp = 0;
118+}
119+
120+
121+
122+/*
123+ * スタッククラス
124+ */
125+
3126 #define _STACK_BLOCK_SIZE 4096
4127
5128 struct _StackFrameInfo {
@@ -10,12 +133,12 @@
10133 };
11134 };
12135
13-static npl_sblock_t* _get_prev(npl_sblock_t *s)
136+static _SBlock* _get_prev(_SBlock *s)
14137 {
15138 if (NPL_BLOCK(s)->link == NULL)
16139 return NULL;
17140
18- return NPL_SBLOCK(NPL_BLOCK(s)->link);
141+ return _SBLOCK(NPL_BLOCK(s)->link);
19142 }
20143
21144 // 不要なブロックを削除する。
@@ -22,16 +145,21 @@
22145 // 不要なブロックがなければ何も起きない。
23146 static void _delete_sblock(npl_stack_t *s)
24147 {
25- while (s->stack != NULL && s->stack->sp == 0) {
26- npl_sblock_t *sb = s->stack;
148+ while (s->stack != NULL && _SBLOCK(s->stack)->sp == 0) {
149+ _SBlock *sb = _SBLOCK(s->stack);
150+ _SBlock *sb2;
27151
28- s->stack = _get_prev(s->stack);
152+ sb2 = _get_prev(_SBLOCK(s->stack));
153+ if (sb2 == NULL)
154+ s->stack = NULL;
155+ else
156+ s->stack = NPL_BLOCK(sb2);
29157 if (s->stack != NULL)
30158 npl_object_ref(NPL_OBJECT(s->stack));
31- npl_sblock_clear(sb);
159+ _sblock_clear(sb);
32160
33161 if (s->unused == NULL)
34- s->unused = sb;
162+ s->unused = NPL_BLOCK(sb);
35163 else
36164 npl_object_unref(NPL_OBJECT(sb));
37165 }
@@ -44,35 +172,39 @@
44172 // まだ、リストがない
45173 if (s->stack == NULL) {
46174 if (s->unused != NULL) {
47- npl_sblock_clear(s->unused);
175+ _sblock_clear(_SBLOCK(s->unused));
48176 s->stack = s->unused;
49177 s->unused = NULL;
50178 } else {
51- s->stack = npl_sblock_new(s->block_size);
52- if (s->stack == NULL)
179+ _SBlock *sb;
180+ sb = _sblock_new(s->block_size);
181+ if (sb == NULL)
53182 return NULL; // error
183+ s->stack = NPL_BLOCK(sb);
54184 }
55185 }
56186
57- p = npl_sblock_push(s->stack, bytes);
187+ p = _sblock_push(_SBLOCK(s->stack), bytes);
58188
59189 // リストが一杯の場合
60190 if (p == NULL) {
61191 if (s->unused != NULL) {
62- npl_sblock_clear(s->unused);
63- npl_sblock_set_prev(s->unused, s->stack);
192+ _sblock_clear(_SBLOCK(s->unused));
193+ _sblock_set_prev(
194+ _SBLOCK(s->unused),
195+ _SBLOCK(s->stack));
64196 npl_object_unref(NPL_OBJECT(s->stack));
65197 s->stack = s->unused;
66198 s->unused = NULL;
67199 } else {
68- npl_sblock_t *sb = npl_sblock_new(s->block_size);
200+ _SBlock *sb = _sblock_new(s->block_size);
69201 if (sb == NULL)
70202 return NULL; // error
71- npl_sblock_set_prev(sb, s->stack);
203+ _sblock_set_prev(sb, _SBLOCK(s->stack));
72204 npl_object_unref(NPL_OBJECT(s->stack));
73- s->stack = sb;
205+ s->stack = NPL_BLOCK(sb);
74206 }
75- p = npl_sblock_push(s->stack, bytes);
207+ p = _sblock_push(_SBLOCK(s->stack), bytes);
76208 }
77209
78210 return p;
@@ -83,7 +215,7 @@
83215 {
84216 if (s->stack == NULL)
85217 return NULL; // 要素なし
86- return npl_sblock_get(s->stack, bytes);
218+ return _sblock_get(_SBLOCK(s->stack), bytes);
87219 }
88220
89221 // 要素を削除する
@@ -91,7 +223,7 @@
91223 {
92224 if (s->stack == NULL)
93225 return 1; // 要素なし
94- if (npl_sblock_remove(s->stack, bytes))
226+ if (_sblock_remove(_SBLOCK(s->stack), bytes))
95227 return 1; // 要素なし
96228 _delete_sblock(s);
97229 return 0;
@@ -204,6 +336,8 @@
204336
205337 int npl_stack_init(npl_stack_t *s, npl_stack_type_t type, size_t block_size)
206338 {
339+ _SBlock *sb;
340+
207341 assert(s);
208342
209343 if (block_size == 0)
@@ -211,9 +345,10 @@
211345 else
212346 s->block_size = block_size;
213347
214- s->stack = npl_sblock_new(s->block_size);
215- if (s->stack == NULL)
348+ sb = _sblock_new(s->block_size);
349+ if (sb == NULL)
216350 return 1; // error
351+ s->stack = NPL_BLOCK(sb);
217352
218353 switch (type) {
219354 case NPL_ST_OBJECT:
@@ -277,9 +412,12 @@
277412 assert(s);
278413
279414 if (s->sfi == NULL) { // プリミティブ・データ
280- npl_sblock_t *sb;
415+ _SBlock *sb;
281416
282- sb = s->stack;
417+ if (s->stack == NULL)
418+ sb = NULL;
419+ else
420+ sb = _SBLOCK(s->stack);
283421 while (sb != NULL && s->count > 0) {
284422 if (sb->sp > s->count) {
285423 sb->sp -= s->count;
旧リポジトリブラウザで表示