|
| 1 | +/* jshint esversion:8 */ |
| 2 | + |
| 3 | +import { |
| 4 | + readFile, |
| 5 | +} from "node:fs/promises"; |
| 6 | +import { |
| 7 | + argv, |
| 8 | + stdin, |
| 9 | + stdout, |
| 10 | + exit, |
| 11 | +} from "node:process"; |
| 12 | +import { |
| 13 | + createInterface |
| 14 | +} from "node:readline/promises"; |
| 15 | +import { |
| 16 | + buffer_size, |
| 17 | + rule_t, |
| 18 | +} |
| 19 | +from "../jsds/jsds.mjs"; |
| 20 | + |
| 21 | +function* search(input_strings) { |
| 22 | + buffer_size(1000); |
| 23 | + |
| 24 | + let rules = {}; |
| 25 | + let facts = {}; |
| 26 | + |
| 27 | + let cycle = -1; |
| 28 | + |
| 29 | + for (let input_string of input_strings) { |
| 30 | + let rule = new rule_t(input_string); |
| 31 | + if (rule.length() !== 0) { |
| 32 | + rules[rule.key()] = [rule, cycle]; |
| 33 | + } else { |
| 34 | + facts[rule.key()] = [rule, cycle]; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + while (true) { |
| 39 | + let temp_rules = {}; |
| 40 | + let temp_facts = {}; |
| 41 | + |
| 42 | + for (let r_hash in rules) { |
| 43 | + for (const f_hash in facts) { |
| 44 | + const [rule, r_cycle] = rules[r_hash]; |
| 45 | + const [fact, f_cycle] = facts[f_hash]; |
| 46 | + if (r_cycle !== cycle && f_cycle !== cycle) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + const candidate = rule.match(fact); |
| 50 | + if (candidate === null) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + const candidate_hash = candidate.key(); |
| 54 | + if (candidate.length() !== 0) { |
| 55 | + // rule |
| 56 | + if (candidate_hash in rules || candidate_hash in temp_rules) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + temp_rules[candidate_hash] = candidate; |
| 60 | + } else { |
| 61 | + // fact |
| 62 | + if (candidate_hash in facts || candidate_hash in temp_facts) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + temp_facts[candidate_hash] = candidate; |
| 66 | + } |
| 67 | + console.log(candidate.toString()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + cycle++; |
| 72 | + for (let r_hash in temp_rules) { |
| 73 | + const rule = temp_rules[r_hash]; |
| 74 | + rules[rule.key()] = [rule, cycle]; |
| 75 | + } |
| 76 | + for (let f_hash in temp_facts) { |
| 77 | + const fact = temp_facts[f_hash]; |
| 78 | + facts[fact.key()] = [fact, cycle]; |
| 79 | + } |
| 80 | + |
| 81 | + yield; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +async function read_file_to_string_array(file_path) { |
| 86 | + const content = await readFile(file_path, "utf-8"); |
| 87 | + const sections = content.split(/\n\n/); |
| 88 | + const results = sections |
| 89 | + .filter(section => section.trim().length > 0) |
| 90 | + .map(section => section.trim()); |
| 91 | + return results; |
| 92 | +} |
| 93 | + |
| 94 | +async function main() { |
| 95 | + if (argv.length < 3) { |
| 96 | + console.error("Usage: node engine.mjs <file_path>"); |
| 97 | + exit(1); |
| 98 | + } |
| 99 | + const file_path = argv[2]; |
| 100 | + const data = await read_file_to_string_array(file_path); |
| 101 | + const generator = search(data); |
| 102 | + const handle = createInterface({ |
| 103 | + input: stdin, |
| 104 | + output: stdout |
| 105 | + }); |
| 106 | + while (true) { |
| 107 | + generator.next(); |
| 108 | + await handle.question("Press Enter to continue..."); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +await main(); // jshint ignore:line |
0 commit comments