-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
96 lines (76 loc) · 2.29 KB
/
compile.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Object.assign(global, require('./token'))
//const shortid = require('shortid')
//shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_')
module.exports = function compile(tree, panic) {
console.dir(tree, { depth: null, colors: true })
let ind = ''
function indent() {
ind += 1
}
function outdent() {
ind -= 1
}
let res = ''
function add(what, k) {
res += (k ? '' : ' '.repeat(ind)) + what
}
let idents = { display: 'console.log' }
function gen(o) {
if (idents[o]) return idents[o]
const g = o // '_' + shortid.generate()
idents[o] = g
return g
}
for (let fn of tree) {
if (fn.shift() !== FUNCTION) panic('Root declaration is not function')
const [oldName, args, returnType, body] = fn
const name = gen(oldName)
add(`function ${name}(${args.map(a => gen(a[1]))}) {\n`)
indent()
statement(body)
outdent()
add(`}\n\n`)
}
function statement(d) {
// Not using switch() {} here because it has weird syntax
if (d[0] === DO) {
const statements = d[1]
d[1].forEach(s => statement(s))
} else if (d[0] === CALL) {
add(`${gen(d[1])}(${d[2].map(k => expression(k)).join(', ')})\n`)
} else if (d[0] === DECLARATION) {
add(`var ${gen(d[2])}\n`)
} else if (d[0] === SET) {
const is_declaration = d[1][0][0] === DECLARATION
if (is_declaration) {
add(`var `)
d[1] = d[1][0][2]
}
add(`${gen(d[1])} = ${expression(d[2])}\n`, is_declaration)
} else {
panic(`Unknown statement type`)
}
}
function expression(d) {
if (d[0] === STRING) {
return `"${d[1].replace(/"/g, '\\"')}"`
} else if (d[0] === INTEGER || d[0] === FLOAT) {
return `${d[1]}`
} else if (d[0] === VARIABLE) {
return `${gen(d[1])}`
} else if (d[0] === ADD) {
return `${expression(d[1])} + ${expression(d[2])}`
} else if (d[0] === SUBTRACT) {
return `${expression(d[1])} - ${expression(d[2])}`
} else if (d[0] === DIVIDE) {
return `${expression(d[1])} / ${expression(d[2])}`
} else if (d[0] === MULTIPLY) {
return `${expression(d[1])} * ${expression(d[2])}`
} else {
panic(`Unknown expression token`)
}
}
add(`${gen('main')}()\n`)
console.log('\n' + res)
return res
}