-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.js
132 lines (120 loc) · 3.19 KB
/
interpreter.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict'
var Lexer = require('simpler-lexer').default
var fp = require('lodash/fp')
var fs = require('fs')
var util = require('util')
var logger = require('log4js').getLogger()
logger.setLevel('debug')
logger.info('Running file: '+process.argv[2])
var standardLib = {
print: (text)=>{
console.log(text)
},
'+': fp.add,
'*': fp.multiply,
'/': fp.divide,
'div': (n1,maybeN2)=>maybeN2?Math.floor(n1/maybeN2):(n2)=>Math.floor(n1/n2),
'-': fp.subtract,
'%': (n1,maybeN2)=>maybeN2?n1%maybeN2:(n2)=>n1%n2,
'=': (n1)=>{
return (n2)=>n1==n2
},
concat: piece1=>piece2=> ''+piece1+piece2,
compose: fp.compose,
if: trueFun=>falseFun=>boolean=>boolean?trueFun():falseFun(),
wrap: fun=>{
return args=>{
return ()=>fun.apply(null, args)
}
},
cons: list=>{
if(list instanceof Array)
return item=>list.concat(item)
else
return [list]
},
list: function(){
return Array.prototype.slice.call(arguments)
},
reduce: fp.reduce,
map: fp.map,
range: fp.range
}
let lexer = new Lexer([
{name:'startFunctionCallArgs', rule: /:/},
{name:'endFunctionCallArgs', rule: /;/},
{name:'argSeparator', rule: /,/},
{name:'string', rule: /'[^']*'/},
{name:'whitespace', rule: /\s+/},
{name:'number', rule: /\d+/},
{name:'functionIdentifier', rule: /(\w+|\+|\*|\/|-|%|=)/}
])
var types = ['string', 'number', 'function', 'list']
var evl = function(tokens){
var nextToken = tokens.shift()
logger.debug('evl called with next token of: '+nextToken.value+' '+nextToken.type)
if(nextToken.type == "string"){
return {
type:'string',
value: nextToken.value.slice(1,-1)//trim dem quotes
}
}
if(nextToken.type == 'startFunctionCallArgs' ){
logger.debug('parsing function call')
var args = []
while ( tokens.length != 0 ) {
var arg = evl(tokens)
if(arg.type != 'backup')
args.push(arg)
var comma = tokens.shift()
if(comma.type == 'endFunctionCallArgs')
break
if(comma.type != 'argSeparator')
throw new Error('Expected comma found value:'+util.inspect(comma))
}
var fun = evl(tokens)
if(fun.type != 'function')
throw new Error(fun.type +' cannot be evaluated')
logger.debug('applying with args: ')
logger.debug(args)
var returnVal = fun.value.apply(null, fp.map('value', args))
return {
type: typeof returnVal,
value: returnVal
}
}
if(nextToken.type == 'number'){
logger.debug('found number: '+nextToken.value)
return {
type: 'number',
value: parseFloat(nextToken.value)
}
}
if(nextToken.type == 'argSeparator'){
throw Error('unexpected comma: '+util.inspect(nextToken))
}
if(nextToken.type == 'endFunctionCallArgs'){
tokens.unshift(nextToken)
return {
type: 'backup',
value: null
}
}
if(nextToken.type == 'functionIdentifier'){
var stdLibFunction = standardLib[nextToken.value]
if(stdLibFunction){
logger.debug('found function: '+nextToken.value)
return {
type:'function',
value: stdLibFunction
}
}
throw new Error('Could not find function with name: '+nextToken.value)
}
if(nextToken.end != true){
logger.debug('falling off the end with token: ')
logger.debug(nextToken)
}
}
var tokens = lexer.tokenize(fs.readFileSync(process.argv[2]).toString())
evl(fp.filter(token=>token.type!='whitespace')(tokens))