Langphilia! /
KL1 /
KLIC-C /
Generic Objects, 2001-10-23
Generic Objects
Usage in KL1 Language
runtime/gen.kl1
- generic:new( +Class, -Object, ?Arg .. )
- Class のインスタンス Object を作る。
コンストラクタの引数 Arg .. はクラスによって異なる。
3種類
- Data Objects, 具体値を持つ
- Consumer Objects,
書き込みによって処理が行なわれる
- Generator Objects,
読み書きによって処理が行なわれる
KLIC-KL1 プログラムから見ると、Data Object は値、
Consumer/Generator はゴールの中断原因変数のように見える。
Methods
D は Data Object, C は Consumer Object, G は Generator Object,
* は3種すべてに共通するメソッド
- D passive_unify
- * active_unify
- G generate
- G suspend
- * print
- * gc
- * regist
- * deallocate
- * close
- D body_generic
- D g_generic
- D compare
- D hash
- * encode
- D shmcopy
Samples in KLIC Runtime
*.h は include/klic/ に、
*.c や *.kl1 は runtime/ にある。
klic/g_extinl.h
- 関数 void GD_suspend(), GD_make_argblock(), GD_make_var(),
GD_make_goal_of_new(), GD_make_goal_of_root(), GD_make_goal_of_method()
klic/g_extern.h
- 関数 q general_gc(q* term, q* allocp, q** sp)
- [runtime/gc.c]
- 関数 int general_print(q* a, FILE stream, unsigned long depth,length)
- 変数 struct data_object_method_table argblock_object_method_table
- 変数 struct predicate predicate_generic_xnew_3,
predicate_generic_xroot_4, predicate_generic_xmethod_4
klic/g_basic.h
- 前提
- klic/struct.h, klic/susp.h を include すること。
- 宣言 G_USE_PREDICATE(name)
- 関数 int G_ISATOMIC, G_ISCONS, G_ISFUNCTOR, G_ISREF (q x)
- 関数 unsigned long G_ISSTRUCT, G_FUNCTNOTCONS, G_ATOMICNOTREF (q x)
- 関数 int G_ISINT, G_ISSYM (q x)
- 関数 int G_ISGOBJ(q x)
- 関数 int G_EQATOM(q x, q y)
- 関数 q G_FUNCTOR_OF(q s), G_ARG(q s, int k)
- 関数 q G_CAR_OF, G_CDR_OF, G_PCAR_OF, G_PCDR_OF (q x)
- 関数 q G_DEREFONE(q x)
- 関数 q G_MAKEINT, G_MAKESYM (q n)
- 関数 long G_MAKECINT(q n)
- 関数 q G_MAKEREF(void* x)
- 関数 q G_MAKEATOMIC(long adr)
- 関数 q G_MAKEPF(q x)
- 関数 q G_MAKECONS, G_MAKEFUNCTOR (long x)
- 関数 long G_INTVAL(q x)
- 関数 unsigned long G_SYMVAL(q x)
- 関数 q* G_REFP(q x)
- 関数 struct cons* G_CONSP(q x)
- 関数 struct functor* G_FUNCTORP(q x)
- 関数 struct goalrec* G_GOALP(q x)
- 関数 struct susprec* G_SUSPP(q x)
- 関数 struct predicate* G_PREDP(q x)
- G_XXX imports xxx
- 定数 G_rappend0, G_rappend1 (class, post)
- 定数 G_atom, G_functor (fa)
- 定数 G_guard0, G_guard1 (class, fa)
- 定数 G_body0, G_body1 (class, fa)
- 関数 const char* G_stringify, G_stringify1 (s)
- 文 G_HEAPALLOC(T from, int size, T (*type)(q*))
- export from, g_allocp
- 文 G_HEAPALLOC_WITH_CHECK
(T from, int size, T (*type)(q*), q* allocp, q res)
- export from, res, heaplimit, this_more_space
- import allocp, real_heaplimit,
GENERIC_GCREQUEST, GENERIC_SUCCEEDED, G_HEAPALLOC
- 文 G_MAKE_VAR(q x)
- export x
- import q, derefone, G_HEAPALLOC
- 宣言 G_STD_DECL
- klic/struct.h の declare_globals と同じ。
- 関数 size_t G_SIZE_IN_Q(type)
- import sizeof(q)
- 関数 q** make_larger_stack(q** sp)
- [gc.c]
- 文 G_COPY_KL1_TERM_TO_NEWGEN(q from, q& to)
- export to, g_sp
- import gcmax, g_allocp, isref, makeref, derefone, make_larger_stack
- 文 G_PUSH_GOAL, G_PUSH_GOAL_TO_SPECIAL_QUEUE (q goal)
- import resume_same_prio
- 定数 int G_DATA, G_CONSUMER, G_GENERATOR
- 関数 volatile void G_error
(char* errmsg, char* where, char* object_kind, char* class)
klic/generic.h
- 前提
- klic/struct.h を include すること。
- 副作用
- klic/g_methtab.h が include される。
- 関数 void new_generic(T (*name)(), argc, T out, q* offset)
- generic:new する。
- 関数 q* call_generic(obj, funct, q* offset)
- generic:generic する。
- 関数 void guard_generic(q object, mf, inargc, label)
- generic:GuardMethod する。
- 関数 int isgobj(q x)
- x がデータオブジェクトか否かを返す。
- 型 data_object_method_table_of(class)
- `class'_g_data_method_table。klic/gb.h の TableName と同じ。
- 関数 int isclass(q x, class)
- x が class のインスタンスか否かを返す。
- 宣言 declare_method_table_of(class)
klic/gobjutil.h
klic/gobj.h
- 副作用
- klic/g_methtab.h が include される。
- 関数 method_table_of(obj)
- 関数 void generic_passive_unify(obj, another),
generic_active_unify(obj, another, q*& allocp)
- 関数 int generic_print
(q* obj, FILE stream, unsigned long depth,length)
- 関数 generic_gc(obj, allocp, sp)
- 関数 generic_regist, generic_deallocate (obj)
- 関数 generic_close, generic_generate (obj, allocp)
- 関数 generic_suspend(obj, ref, goal)
- 関数 generic_new(class, n, args)
- 関数 general_passive_unify(obj, another),
general_active_unify(obj, another, allocp)
- 変数 q suspension_reason
- 0: 正常終了、1: GC要求、other: フック。
- 変数 q rest_of_stream
- 0: 正常終了、other: 次のフック。
- 変数 q method_result
klic/g_methtab.h
- 副作用
- 複数回展開防止されている。
- 型 struct data_object, data_object_method_table
- 型 struct consumer_object, consumer_object_method_table
- 型 struct generator_object, generator_object_method_table
- 定数 const q GENERIC_SUCCEEDED, GENERIC_FAILED, GENERIC_GCREQUEST
- ジェネリックメソッドの結果。
- 関数 struct data_object* data_objectp(q obj)
- import FUNCTOR
Generic Objectの作り方
オブジェクトを定義した C ソースファイルと、
そのオブジェクトを使用する KL1 ソースファイルを一緒に
KLIC 翻訳系で翻訳する。
runtime/generic.c
runtime/otbl.c
Copyright 2001, TAKAGI Yusuke.