1
0
Fork 0
mirror of https://github.com/ganelson/inform.git synced 2024-07-08 18:14:21 +03:00
inform7/inter/bytecode-module/Chapter 4/The Package Construct.w

180 lines
7.1 KiB
OpenEdge ABL
Raw Normal View History

2022-01-31 01:49:12 +02:00
[InterPackage::] The Package Construct.
2019-02-05 02:44:07 +02:00
Defining the package construct.
@
@e PACKAGE_IST
=
2022-01-31 01:49:12 +02:00
void InterPackage::define(void) {
2019-02-05 02:44:07 +02:00
inter_construct *IC = Inter::Defn::create_construct(
PACKAGE_IST,
L"package (%i+) (%i+)",
I"package", I"packages");
IC->usage_permissions = OUTSIDE_OF_PACKAGES + INSIDE_PLAIN_PACKAGE + CAN_HAVE_CHILDREN;
2022-01-31 01:49:12 +02:00
METHOD_ADD(IC, CONSTRUCT_READ_MTID, InterPackage::read);
METHOD_ADD(IC, CONSTRUCT_TRANSPOSE_MTID, InterPackage::transpose);
METHOD_ADD(IC, CONSTRUCT_VERIFY_MTID, InterPackage::verify);
METHOD_ADD(IC, CONSTRUCT_WRITE_MTID, InterPackage::write);
METHOD_ADD(IC, VERIFY_INTER_CHILDREN_MTID, InterPackage::verify_children);
2019-02-05 02:44:07 +02:00
}
@
@d PTYPE_PACKAGE_IFLD 2
@d SYMBOLS_PACKAGE_IFLD 3
@d PID_PACKAGE_IFLD 4
2019-02-05 02:44:07 +02:00
=
2022-01-31 01:49:12 +02:00
void InterPackage::read(inter_construct *IC, inter_bookmark *IBM, inter_line_parse *ilp, inter_error_location *eloc, inter_error_message **E) {
2019-07-14 12:44:07 +03:00
*E = Inter::Defn::vet_level(IBM, PACKAGE_IST, ilp->indent_level, eloc);
2019-07-09 08:45:46 +03:00
if (*E) return;
2019-02-05 02:44:07 +02:00
2022-01-27 01:59:02 +02:00
inter_symbol *ptype_name = Inter::Textual::find_symbol(InterBookmark::tree(IBM), eloc, InterTree::global_scope(InterBookmark::tree(IBM)), ilp->mr.exp[1], PACKAGETYPE_IST, E);
2019-07-09 08:45:46 +03:00
if (*E) return;
2019-02-05 02:44:07 +02:00
inter_package *pack = NULL;
2022-01-31 01:49:12 +02:00
*E = InterPackage::new_package_named(IBM, ilp->mr.exp[0], FALSE, ptype_name, (inter_ti) ilp->indent_level, eloc, &pack);
2019-07-09 08:45:46 +03:00
if (*E) return;
2019-02-05 02:44:07 +02:00
2022-01-27 01:59:02 +02:00
InterBookmark::move_into_package(IBM, pack);
2019-02-05 02:44:07 +02:00
}
2022-01-31 01:49:12 +02:00
inter_error_message *InterPackage::new_package_named(inter_bookmark *IBM, text_stream *name, int uniquely,
2020-07-01 02:58:55 +03:00
inter_symbol *ptype_name, inter_ti level, inter_error_location *eloc, inter_package **created) {
2019-07-27 17:31:50 +03:00
if (uniquely) {
2020-06-28 01:18:54 +03:00
TEMPORARY_TEXT(mutable)
2019-07-27 17:31:50 +03:00
WRITE_TO(mutable, "%S", name);
inter_package *pack;
int N = 1, A = 0;
2022-02-03 17:51:44 +02:00
while ((pack = InterPackage::from_name(InterBookmark::package(IBM), mutable)) != NULL) {
2020-06-28 01:18:54 +03:00
TEMPORARY_TEXT(TAIL)
2019-07-27 17:31:50 +03:00
WRITE_TO(TAIL, "_%d", N++);
if (A > 0) Str::truncate(mutable, Str::len(mutable) - A);
A = Str::len(TAIL);
WRITE_TO(mutable, "%S", TAIL);
Str::truncate(mutable, 31);
2020-06-28 01:18:54 +03:00
DISCARD_TEXT(TAIL)
2019-07-27 17:31:50 +03:00
}
2022-01-31 01:49:12 +02:00
inter_error_message *E = InterPackage::new_package(IBM, mutable, ptype_name, level, eloc, created);
2020-06-28 01:18:54 +03:00
DISCARD_TEXT(mutable)
2019-07-27 17:31:50 +03:00
return E;
}
2022-01-31 01:49:12 +02:00
return InterPackage::new_package(IBM, name, ptype_name, level, eloc, created);
2019-07-27 13:16:22 +03:00
}
2022-01-31 01:49:12 +02:00
inter_error_message *InterPackage::new_package(inter_bookmark *IBM, text_stream *name_text, inter_symbol *ptype_name, inter_ti level, inter_error_location *eloc, inter_package **created) {
2022-01-30 15:32:38 +02:00
inter_ti STID = InterWarehouse::create_symbols_table(InterBookmark::warehouse(IBM));
2022-01-28 01:38:14 +02:00
inter_tree_node *P = Inode::new_with_3_data_fields(IBM,
PACKAGE_IST,
2022-02-03 17:51:44 +02:00
InterSymbolsTable::id_from_symbol(InterBookmark::tree(IBM), NULL, ptype_name), STID, 0, eloc, level);
2022-01-30 15:32:38 +02:00
inter_ti PID = InterWarehouse::create_package(InterBookmark::warehouse(IBM), InterBookmark::tree(IBM));
inter_package *pack = InterWarehouse::get_package(InterBookmark::warehouse(IBM), PID);
pack->package_head = P;
2022-01-30 15:32:38 +02:00
P->W.instruction[PID_PACKAGE_IFLD] = PID;
2022-01-31 01:49:12 +02:00
InterPackage::set_scope(pack, InterWarehouse::get_symbols_table(InterBookmark::warehouse(IBM), STID));
2022-01-30 15:32:38 +02:00
InterWarehouse::set_symbols_table_owner(InterBookmark::warehouse(IBM), STID, pack);
2022-01-27 01:59:02 +02:00
inter_error_message *E = Inter::Defn::verify_construct(InterBookmark::package(IBM), P);
2019-02-05 02:44:07 +02:00
if (E) return E;
2022-01-27 01:59:02 +02:00
NodePlacement::move_to_moving_bookmark(P, IBM);
2019-02-05 02:44:07 +02:00
2022-02-03 01:35:38 +02:00
InterPackage::set_name(InterPackage::tree(InterBookmark::package(IBM)), InterBookmark::package(IBM), pack, name_text);
if (Str::eq(ptype_name->symbol_name, I"_code"))
2022-01-31 01:49:12 +02:00
InterPackage::mark_as_a_function_body(pack);
if (Str::eq(ptype_name->symbol_name, I"_linkage"))
2022-01-31 01:49:12 +02:00
InterPackage::mark_as_a_linkage_package(pack);
2019-02-05 02:44:07 +02:00
if (created) *created = pack;
2019-07-26 21:20:27 +03:00
LOGIF(INTER_SYMBOLS, "Package $6 at IBM $5\n", pack, IBM);
2019-02-05 02:44:07 +02:00
return NULL;
}
2022-01-31 01:49:12 +02:00
void InterPackage::transpose(inter_construct *IC, inter_tree_node *P, inter_ti *grid, inter_ti grid_extent, inter_error_message **E) {
2022-01-30 15:32:38 +02:00
P->W.instruction[PID_PACKAGE_IFLD] = grid[P->W.instruction[PID_PACKAGE_IFLD]];
P->W.instruction[SYMBOLS_PACKAGE_IFLD] = grid[P->W.instruction[SYMBOLS_PACKAGE_IFLD]];
}
2022-01-31 01:49:12 +02:00
void InterPackage::verify(inter_construct *IC, inter_tree_node *P, inter_package *owner, inter_error_message **E) {
2022-01-30 15:32:38 +02:00
inter_package *pack = Inode::ID_to_package(P, P->W.instruction[PID_PACKAGE_IFLD]);
2019-07-27 13:16:22 +03:00
if (pack) pack->package_head = P;
else internal_error("uh?");
2022-01-31 01:49:12 +02:00
inter_symbols_table *T = InterPackage::scope(owner);
2020-05-11 17:21:29 +03:00
if (T == NULL) T = Inode::globals(P);
Inter::Defn::set_latest_block_package(pack);
2019-02-05 02:44:07 +02:00
}
2022-01-31 01:49:12 +02:00
void InterPackage::write(inter_construct *IC, OUTPUT_STREAM, inter_tree_node *P, inter_error_message **E) {
inter_package *pack = InterPackage::at_this_head(P);
2022-02-03 17:51:44 +02:00
inter_symbol *ptype_name = InterSymbolsTable::global_symbol_from_ID_at_node(P, PTYPE_PACKAGE_IFLD);
if ((pack) && (ptype_name)) {
2022-01-31 01:49:12 +02:00
WRITE("package %S %S", InterPackage::name(pack), ptype_name->symbol_name);
2019-02-05 02:44:07 +02:00
} else {
2020-05-11 17:21:29 +03:00
if (pack == NULL) { *E = Inode::error(P, I"package can't be written - no name", NULL); return; }
*E = Inode::error(P, I"package can't be written - no type", NULL); return;
2019-02-05 02:44:07 +02:00
}
}
2022-01-31 01:49:12 +02:00
inter_error_message *InterPackage::write_symbols(OUTPUT_STREAM, inter_tree_node *P) {
inter_package *pack = InterPackage::at_this_head(P);
if (pack) {
2022-01-31 01:49:12 +02:00
inter_symbols_table *locals = InterPackage::scope(pack);
2022-02-03 17:51:44 +02:00
int L = (int) (P->W.instruction[LEVEL_IFLD] + 1);
LOOP_OVER_SYMBOLS_TABLE(S, locals) {
InterSymbol::write_declaration(OUT, S, L);
2022-02-03 17:51:44 +02:00
WRITE("\n");
}
2019-02-05 02:44:07 +02:00
}
return NULL;
}
2022-01-31 01:49:12 +02:00
int InterPackage::is(inter_symbol *package_name) {
2019-02-05 02:44:07 +02:00
if (package_name == NULL) return FALSE;
inter_tree_node *D = InterSymbol::definition(package_name);
if (D == NULL) return FALSE;
2022-01-30 15:32:38 +02:00
if (D->W.instruction[ID_IFLD] != PACKAGE_IST) return FALSE;
2019-02-05 02:44:07 +02:00
return TRUE;
}
2022-01-31 01:49:12 +02:00
inter_package *InterPackage::which(inter_symbol *package_name) {
2019-02-05 02:44:07 +02:00
if (package_name == NULL) return NULL;
inter_tree_node *D = InterSymbol::definition(package_name);
if (D == NULL) return NULL;
2022-01-30 15:32:38 +02:00
return Inode::ID_to_package(D, D->W.instruction[PID_PACKAGE_IFLD]);
2019-02-05 02:44:07 +02:00
}
2022-01-31 01:49:12 +02:00
inter_package *InterPackage::at_this_head(inter_tree_node *D) {
2019-07-24 22:29:29 +03:00
if (D == NULL) return NULL;
2022-01-30 15:32:38 +02:00
if (D->W.instruction[ID_IFLD] != PACKAGE_IST) return NULL;
return Inode::ID_to_package(D, D->W.instruction[PID_PACKAGE_IFLD]);
2019-07-13 13:03:54 +03:00
}
2022-01-31 01:49:12 +02:00
inter_symbol *InterPackage::type(inter_package *pack) {
2019-07-27 13:16:22 +03:00
if (pack == NULL) return NULL;
inter_tree_node *D = pack->package_head;
2022-02-03 17:51:44 +02:00
inter_symbol *ptype_name = InterSymbolsTable::global_symbol_from_ID_at_node(D, PTYPE_PACKAGE_IFLD);
2019-02-05 02:44:07 +02:00
return ptype_name;
}
2022-01-31 01:49:12 +02:00
inter_symbols_table *InterPackage::local_symbols(inter_symbol *package_name) {
2019-02-05 02:44:07 +02:00
if (package_name == NULL) return NULL;
inter_tree_node *D = InterSymbol::definition(package_name);
if (D == NULL) return NULL;
2022-01-30 15:32:38 +02:00
if (D->W.instruction[ID_IFLD] != PACKAGE_IST) return NULL;
return Inode::ID_to_symbols_table(D, D->W.instruction[SYMBOLS_PACKAGE_IFLD]);
2019-02-05 02:44:07 +02:00
}
2022-01-31 01:49:12 +02:00
void InterPackage::verify_children(inter_construct *IC, inter_tree_node *P, inter_error_message **E) {
2022-02-03 17:51:44 +02:00
inter_symbol *ptype_name = InterSymbolsTable::global_symbol_from_ID_at_node(P, PTYPE_PACKAGE_IFLD);
if (Str::eq(ptype_name->symbol_name, I"_code")) {
LOOP_THROUGH_INTER_CHILDREN(C, P) {
2022-01-30 15:32:38 +02:00
if ((C->W.instruction[0] != LABEL_IST) && (C->W.instruction[0] != LOCAL_IST) && (C->W.instruction[0] != SYMBOL_IST)) {
2020-05-11 17:21:29 +03:00
*E = Inode::error(C, I"only a local or a symbol can be at the top level", NULL);
return;
}
}
2019-02-05 02:44:07 +02:00
}
}