-
Notifications
You must be signed in to change notification settings - Fork 2
/
code_writer.js
50 lines (35 loc) · 1005 Bytes
/
code_writer.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
const parser = require('./geekspec_parser');
const fs = require('fs');
var code = fs.readFileSync('./geekapk_v1b_api.geekspec').toString();
code = code.replace(/#.*/g, '');
console.log(code);
console.log("\n\n\n")
let ast = parser.parse(code);
console.log(JSON.stringify(ast, null, 2));
function walkOption(o) {
return o;
}
function walkOptions(os) {
if (os == null) return "";
return "{" + os.map(walkOption).join(",") + "}";
}
function walkArg(a) {
return `${a.name}${a.required ? '!' : '?'}${walkOptions(a.options)}`;
}
function walkArgs(as) {
return as.map(walkArg)
}
function walkReturn(r) {
if (r == null) return "noting";
if (typeof r != 'object') return r;
if (Array.isArray(r)) {
return r.map(e => `${e.type}:${e.name}`)
}
return `${r.type} of ${r.of}`;
}
function walkInterface(i) {
console.log()
console.log(`${i.method} ${i.url}: \t ${i.name}(${walkArgs(i.args)})`);
console.log(` Returning ${walkReturn(i.return)}`)
}
ast.forEach(walkInterface);