-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransform.js
executable file
·81 lines (68 loc) · 3.15 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
var fs = require('fs'),
extend = require('extend'),
path = require('path'),
getopt = require('node-getopt'),
graphlib = require('graphlib'),
jsonschema = require('jsonschema'),
colors = require('colors'),
Grammar = require('../index').Grammar
var defaultGrammarFilename = 'grammars/dungeon.js'
var defaultLLM = 'llm'
var opt = getopt.create([
['g' , 'grammar=PATH' , 'read grammar file (default "' + defaultGrammarFilename + '")'],
['c' , 'canonical' , 'use canonical schema (no syntactic sugar)'],
['j' , 'schema=PATH' , 'save JSON schema to file'],
['C' , 'canonize=PATH' , 'save canonical grammar to file'],
['i' , 'input=PATH' , 'read graphlib JSON file'],
['o' , 'output=PATH' , 'write graphlib JSON file'],
['d' , 'dot=PATH' , 'write graphviz DOT file'],
['L' , 'limit=N' , 'limit number of rule applications'],
['S' , 'stage=N' , 'only run one stage'],
['m' , 'llm=COMMAND' , 'command-line interface to LLM (default "' + defaultLLM + '")'],
['s' , 'seed=N' , 'seed random number generator'],
['q' , 'quiet' , 'do not print pretty log messages'],
['v' , 'verbose' , 'print MORE pretty log messages'],
['h' , 'help' , 'display this help message']
]) // create Getopt instance
.bindHelp() // bind option 'help' to default action
.parseSystem() // parse command line
var verbosity = opt.options.quiet ? 0 : (opt.options.verbose ? 2 : 1)
var grammarOpts = { canonical: opt.options.canonical, llm: opt.options.llm || defaultLLM, verbose: verbosity }
function makeGrammar (json) {
let g = new Grammar (json, grammarOpts)
g.registerRhsLabelExecFunction ('llm', opt.options.llm || 'llm', "Generate text using command-line LLM interface. This option is only enabled in the command-line tool. The default LLM toolname is llm, which must be separately installed: https://github.com/simonw/llm", true)
return g
}
if (opt.options.schema) {
fs.writeFileSync (opt.options.schema, JSON.stringify (makeGrammar(null).makeSchema(), null, 2))
process.exit()
}
var grammarFilename = opt.options.grammar || defaultGrammarFilename
var grammarText = fs.readFileSync(grammarFilename).toString()
var grammarJson = eval ('(' + grammarText + ')')
var grammar = makeGrammar (grammarJson)
if (opt.options.canonize)
fs.writeFileSync (opt.options.canonize, JSON.stringify (grammar.canonicalJson(), null, 2))
var graph
if (opt.options.input)
graph = graphlib.json.read (JSON.parse (fs.readFileSync (opt.options.input)))
var seed = opt.options.seed
if (typeof(seed) === 'undefined') {
seed = new Date().getTime()
console.warn ("Random number seed: " + seed)
}
var info = grammar.evolve ({ graph: graph,
verbose: verbosity,
limit: opt.options.limit,
stage: opt.options.stage,
seed: seed })
graph = info.graph
var dotFilename = opt.options.dot
if (dotFilename)
fs.writeFileSync (dotFilename, grammar.toDot(graph))
var output = JSON.stringify (graphlib.json.write (graph), null, 2)
if (opt.options.output)
fs.writeFileSync (opt.options.output, output)
else if (!dotFilename)
console.log (output)