|
| 1 | +import create_ds from "./ds.mjs"; |
| 2 | +import type * as dst from "./ds.d.mts"; |
| 3 | + |
| 4 | +const ds: dst.EmbindModule = await create_ds(); |
| 5 | + |
| 6 | +let _buffer_size: number = 1024; |
| 7 | + |
| 8 | +export function buffer_size(size: number = 0): number { |
| 9 | + const old_size = _buffer_size; |
| 10 | + if (size != 0) { |
| 11 | + _buffer_size = size; |
| 12 | + } |
| 13 | + return old_size; |
| 14 | +} |
| 15 | + |
| 16 | +interface Common { |
| 17 | + clone(): Common; |
| 18 | + data_size(): number; |
| 19 | +} |
| 20 | + |
| 21 | +interface StaticCommon<T extends Common> { |
| 22 | + from_binary(buffer: dst.Buffer): T; |
| 23 | + to_binary(value: T): dst.Buffer; |
| 24 | + from_string(text: string, size: number): T; |
| 25 | + to_string(value: T, size: number): string; |
| 26 | +} |
| 27 | + |
| 28 | +class _common_t<T extends Common> { |
| 29 | + type: StaticCommon<T>; |
| 30 | + value: T; |
| 31 | + capacity: number; |
| 32 | + |
| 33 | + constructor( |
| 34 | + type: StaticCommon<T>, |
| 35 | + value: _common_t<T> | T | string | dst.Buffer, |
| 36 | + size: number = 0, |
| 37 | + ) { |
| 38 | + this.type = type; |
| 39 | + if (value instanceof _common_t) { |
| 40 | + this.value = value.value; |
| 41 | + this.capacity = value.capacity; |
| 42 | + if (size !== 0) { |
| 43 | + throw new Error("Cannot set capacity when copying from another instance."); |
| 44 | + } |
| 45 | + } else if (value instanceof (this.type as any)) { |
| 46 | + this.value = value as T; |
| 47 | + this.capacity = size; |
| 48 | + } else if (typeof value === "string") { |
| 49 | + this.capacity = size !== 0 ? size : buffer_size(); |
| 50 | + this.value = this.type.from_string(value, this.capacity); |
| 51 | + if (this.value === null) { |
| 52 | + throw new Error("Initialization from a string failed."); |
| 53 | + } |
| 54 | + } else if (value instanceof ds.Buffer) { |
| 55 | + this.value = this.type.from_binary(value); |
| 56 | + this.capacity = this.size(); |
| 57 | + if (size !== 0) { |
| 58 | + throw new Error("Cannot set capacity when initializing from bytes."); |
| 59 | + } |
| 60 | + } else { |
| 61 | + throw new Error("Unsupported type for initialization."); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + toString(): string { |
| 66 | + const result = this.type.to_string(this.value, buffer_size()); |
| 67 | + if (result === "") { |
| 68 | + throw new Error("Conversion to string failed."); |
| 69 | + } |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + data(): dst.Buffer { |
| 74 | + return this.type.to_binary(this.value); |
| 75 | + } |
| 76 | + |
| 77 | + size(): number { |
| 78 | + return this.value.data_size(); |
| 79 | + } |
| 80 | + |
| 81 | + copy(): this { |
| 82 | + const constructor = this.constructor as new (value: T, size: number) => this; |
| 83 | + return new constructor(this.value.clone() as T, this.size()); |
| 84 | + } |
| 85 | + |
| 86 | + key(): string { |
| 87 | + return this.toString(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export class string_t extends _common_t<dst.String> { |
| 92 | + constructor(value: string_t | dst.String | string | dst.Buffer, size: number = 0) { |
| 93 | + super(ds.String, value, size); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export class variable_t extends _common_t<dst.Variable> { |
| 98 | + constructor(value: variable_t | dst.Variable | string | dst.Buffer, size: number = 0) { |
| 99 | + super(ds.Variable, value, size); |
| 100 | + } |
| 101 | + |
| 102 | + name(): string_t { |
| 103 | + return new string_t(this.value.name()); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +export class item_t extends _common_t<dst.Item> { |
| 108 | + constructor(value: item_t | dst.Item | string | dst.Buffer, size: number = 0) { |
| 109 | + super(ds.Item, value, size); |
| 110 | + } |
| 111 | + |
| 112 | + name(): string_t { |
| 113 | + return new string_t(this.value.name()); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export class list_t extends _common_t<dst.List> { |
| 118 | + constructor(value: list_t | dst.List | string | dst.Buffer, size: number = 0) { |
| 119 | + super(ds.List, value, size); |
| 120 | + } |
| 121 | + |
| 122 | + length(): number { |
| 123 | + return this.value.length(); |
| 124 | + } |
| 125 | + |
| 126 | + getitem(index: number): term_t { |
| 127 | + return new term_t(this.value.getitem(index)); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export class term_t extends _common_t<dst.Term> { |
| 132 | + constructor(value: term_t | dst.Term | string | dst.Buffer, size: number = 0) { |
| 133 | + super(ds.Term, value, size); |
| 134 | + } |
| 135 | + |
| 136 | + term(): variable_t | item_t | list_t { |
| 137 | + const term_type: dst.TermType = this.value.get_type(); |
| 138 | + if (term_type === ds.TermType.Variable) { |
| 139 | + return new variable_t(this.value.variable()); |
| 140 | + } else if (term_type === ds.TermType.Item) { |
| 141 | + return new item_t(this.value.item()); |
| 142 | + } else if (term_type === ds.TermType.List) { |
| 143 | + return new list_t(this.value.list()); |
| 144 | + } else { |
| 145 | + throw new Error("Unexpected term type."); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + ground(other: term_t, scope: string = ""): term_t | null { |
| 150 | + const capacity = buffer_size(); |
| 151 | + const term = ds.Term.ground(this.value, other.value, scope, capacity); |
| 152 | + if (term === null) { |
| 153 | + return null; |
| 154 | + } |
| 155 | + return new term_t(term, capacity); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +export class rule_t extends _common_t<dst.Rule> { |
| 160 | + constructor(value: rule_t | dst.Rule | string | dst.Buffer, size: number = 0) { |
| 161 | + super(ds.Rule, value, size); |
| 162 | + } |
| 163 | + |
| 164 | + length(): number { |
| 165 | + return this.value.length(); |
| 166 | + } |
| 167 | + |
| 168 | + getitem(index: number): term_t { |
| 169 | + return new term_t(this.value.getitem(index)); |
| 170 | + } |
| 171 | + |
| 172 | + conclusion(): term_t { |
| 173 | + return new term_t(this.value.conclusion()); |
| 174 | + } |
| 175 | + |
| 176 | + ground(other: rule_t, scope: string = ""): rule_t | null { |
| 177 | + const capacity = buffer_size(); |
| 178 | + const rule = ds.Rule.ground(this.value, other.value, scope, capacity); |
| 179 | + if (rule === null) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + return new rule_t(rule, capacity); |
| 183 | + } |
| 184 | + |
| 185 | + match(other: rule_t): rule_t | null { |
| 186 | + const capacity = buffer_size(); |
| 187 | + const rule = ds.Rule.match(this.value, other.value, capacity); |
| 188 | + if (rule === null) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + return new rule_t(rule, capacity); |
| 192 | + } |
| 193 | +} |
0 commit comments