-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.js
More file actions
61 lines (55 loc) · 2 KB
/
compiler.js
File metadata and controls
61 lines (55 loc) · 2 KB
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
'use strict';
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var f = process.argv[2];
var rs = fs.createReadStream(path.join(process.cwd(), f), 'utf8');
rs.on('readable', () => {
interpretProc(rs, {});
});
function interpretProc (rs, parentScope) {
var scope = _.defaults({}, parentScope);
var context = [];
var currToken = '';
var lastToken;
for (let char = rs.read(1); char !== null; char = rs.read(1)) {
if (char.match(/\w/))
currToken += char;
else {
if (currToken)
lastToken = currToken;
currToken = '';
}
if (char === '<')
interpretProc(rs, scope);
if (char === '=' || char === ':')
context.push({type: 'ASSIGNMENT', value: lastToken});
if (char === '{')
context.push({type: 'OBJECT', value: {}});
if (char === '}')
context = context.slice(0, _.findLastIndex(context, i => i.type === 'OBJECT'));
if (char === '(')
context.push({type: 'FUNCTION'});
if (char === ')')
context = context.slice(0, _.findLastIndex(context, i => i.type === 'FUNCTION'));
if (char === ';') {
_.each(context, (i, ind) => {
if (i.type === 'ASSIGNMENT') {
if (!i.value)
scope = _.defaults({},)
else {
if (_.get(context[ind - 1], 'type') === 'OBJECT')
context[ind - 1].value[i.value] = 'PLACEHOLDER';
else
scope[i.value] = 'PLACEHOLDER';
}
}
});
console.log('!!!!', scope, context);
context = [];
}
if (char === '\'')
context = context.indexOf('STRING') !== -1 ? context.slice(0, context.lastIndexOf('STRING')) : context.concat(['STRING']);
console.log(char, currToken, context, scope);
}
}