1 | // This file is part of SmallBASIC |
2 | // |
3 | // SmallBASIC Unit (SB units) manager |
4 | // |
5 | // This program is distributed under the terms of the GPL v2.0 or later |
6 | // Download the GNU Public License (GPL) from www.gnu.org |
7 | // |
8 | // Copyright(C) 2000 Nicholas Christopoulos |
9 | |
10 | #if !defined(__sb_units_h) |
11 | #define __sb_units_h |
12 | |
13 | #include "common/sys.h" |
14 | #include "common/var.h" |
15 | |
16 | #if defined(__cplusplus) |
17 | extern "C" { |
18 | #endif |
19 | |
20 | #define UID_UNIT_BIT 0x8000 |
21 | |
22 | /** |
23 | * @ingroup exec |
24 | * |
25 | * @typedef sym_type_t |
26 | * symbol type (export/import) |
27 | */ |
28 | typedef enum { |
29 | stt_variable, stt_procedure, stt_function |
30 | } sym_type_t; |
31 | |
32 | /** |
33 | * @ingroup exec |
34 | * |
35 | * @typedef unit_sym_t |
36 | * symbol structure |
37 | */ |
38 | typedef struct { |
39 | char symbol[SB_KEYWORD_SIZE]; /**< symbol name */ |
40 | sym_type_t type; /**< type of symbol (function, procedure, variable) */ |
41 | bcip_t address; /**< code address if proc/func */ |
42 | bcip_t vid; /**< return Variable-ID if func; or Variable-ID if variable */ |
43 | } unit_sym_t; |
44 | |
45 | typedef struct { |
46 | int count; |
47 | unit_sym_t **elem; |
48 | } unit_sym_table_t; |
49 | |
50 | /** |
51 | * @ingroup exec |
52 | * |
53 | * unit status |
54 | */ |
55 | typedef enum { |
56 | unit_undefined, /**< unused record */ |
57 | unit_loaded /**< unit is loaded */ |
58 | } unit_status_t; |
59 | |
60 | /** |
61 | * @ingroup exec |
62 | * @typedef unit_file_t |
63 | * unit: file header |
64 | */ |
65 | typedef struct { |
66 | char sign[4]; /**< Always "SBUn" */ |
67 | int version; /**< version of this structure */ |
68 | char base[SB_KEYWORD_SIZE + 1]; /**< unit-base name */ |
69 | int sym_count; /**< number of symbols */ |
70 | } unit_file_t; |
71 | |
72 | /** |
73 | * @ingroup exec |
74 | * @typedef unit_t |
75 | * unit-memory structure |
76 | */ |
77 | typedef struct { |
78 | unit_status_t status; /**< status of this record */ |
79 | |
80 | char name[OS_FILENAME_SIZE + 1]; /**< unit/file name */ |
81 | unit_file_t hdr; /**< data from file */ |
82 | |
83 | unit_sym_t *symbols; /**< table of symbols */ |
84 | }unit_t; |
85 | |
86 | /** |
87 | * @ingroup exec |
88 | * |
89 | * initialization |
90 | */ |
91 | void unit_mgr_init(); |
92 | |
93 | /** |
94 | * @ingroup exec |
95 | * |
96 | * close up |
97 | */ |
98 | void unit_mgr_close(); |
99 | |
100 | /** |
101 | * @ingroup exec |
102 | * |
103 | * returns the full-pathname of unit |
104 | * |
105 | * @param name unit's name |
106 | * @param file buffer to store the filename |
107 | * @return non-zero on success |
108 | */ |
109 | int find_unit(const char *name, char *file); |
110 | |
111 | /** |
112 | * @ingroup exec |
113 | * |
114 | * open unit |
115 | * |
116 | * @param file is the filename |
117 | * @param alias alternative unit name |
118 | * @return the unit handle or -1 on error |
119 | */ |
120 | int open_unit(const char *file, const char *alias); |
121 | |
122 | /** |
123 | * @ingroup exec |
124 | * |
125 | * closes a unit |
126 | * |
127 | * @param uid is the unit's handle |
128 | * @return 0 on success |
129 | */ |
130 | int close_unit(int uid); |
131 | |
132 | /** |
133 | * @ingroup exec |
134 | * |
135 | * imports unit's names |
136 | * |
137 | * @param uid unit's handle |
138 | * @return 0 on success |
139 | */ |
140 | int import_unit(int uid); |
141 | |
142 | /** |
143 | * @ingroup exec |
144 | * |
145 | * execute |
146 | */ |
147 | int unit_exec(int lib_id, int index, var_t *ret); |
148 | |
149 | #if defined(__cplusplus) |
150 | } |
151 | #endif |
152 | #endif |
153 | |