-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython.js
180 lines (147 loc) · 5.64 KB
/
python.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const {
Binary,
Unary,
Var,
Call,
Literal,
While,
Class,
Get,
Set: SetExpr,
Super,
This,
Grouping,
Return,
LoxFunction,
PrintStatement,
ExpressionStatement,
VarStatement,
Assignment,
Logical,
Block,
Condition
} = require('../types')
const indentation = (scope, options) => options.indent.repeat(scope)
let lastSuperclass = 'super'
const ASTNodeMap = new Map()
// Declarations
ASTNodeMap.set(ExpressionStatement, (node, scope, options, initialIndent) => indentation(scope, options) + loxToPython2(node.expression, 0, options, initialIndent))
ASTNodeMap.set(PrintStatement, (node, scope, options) => indentation(scope, options) + 'print ' + loxToPython2(node.expression))
ASTNodeMap.set(Return, (node, scope, options) => indentation(scope, options) + 'return ' + loxToPython2(node.value))
ASTNodeMap.set(VarStatement, (node, scope, options) => {
// Python doesn't have plain declarations...
if (!node.initializer) return ''
const name = node.name.lexeme
return indentation(scope, options) + `${name} = ${loxToPython2(node.initializer)}`
})
ASTNodeMap.set(Condition, ({ condition, thenBranch, elseBranch }, scope, options) => {
const cond = loxToPython2(condition)
const conditionSection = indentation(scope, options) + `if ${cond}:\n`
const thenSection = loxToPython2(thenBranch, scope + 1, options, false)
const elseSection = elseBranch && loxToPython2(elseBranch, scope + 1, options, false)
return conditionSection + thenSection + (elseSection ? `\n${indentation(scope, options)}else:\n${elseSection}` : '')
})
const printFunction = ({ bodyStatements: body, name: { lexeme: name }, params}, scope, options, func = true) => {
const parameters = params.map(token => token.lexeme)
if (!func) {
parameters.unshift('this')
if (name === 'init') name = '__init__'
}
const head = indentation(scope, options) + `def ${name}(${parameters.join(', ')}):`
// Python doesn't have blank function declarations
const fnBody = body.length > 0 ? body.map(stmt => loxToPython2(stmt, scope + 1, options)) : [indentation(scope + 1, options) + 'pass']
return [head, ...fnBody].join('\n')
}
ASTNodeMap.set(LoxFunction, printFunction)
ASTNodeMap.set(While, ({ body, condition }, scope, options) => {
const cond = loxToPython2(condition)
const conditionSection = indentation(scope, options) + `while ${cond}:\n`
const bodySection = loxToPython2(body, scope + 1, options, false)
return conditionSection + bodySection
})
ASTNodeMap.set(Class, ({ name, superclass, methods }, scope, options) => {
let superclassStr = '()'
if (superclass) {
lastSuperclass = superclass.name.lexeme
superclassStr = `(${superclass.name.lexeme})`
}
const head = indentation(scope, options) + `class ${name.lexeme}${superclassStr}:`
let body = methods.map(node => printFunction(node, scope + 1, options, false))
return [head, ...body].join('\n')
})
ASTNodeMap.set(Get, ({ name, object }, scope, options) => {
let nameStr = name.lexeme ? name.lexeme : loxToPython2(name, scope, options, false)
const objectStr = loxToPython2(object, scope, options, false)
return objectStr + '.' + nameStr
})
ASTNodeMap.set(SetExpr, ({ name, object, value }, scope, options) => {
const nameStr = name.lexeme ? name.lexeme : loxToPython2(name, scope, options, false)
const objectStr = loxToPython2(object, scope, options, false)
const val = loxToPython2(value, scope, options)
return objectStr + '.' + nameStr + ' = ' + val
})
ASTNodeMap.set(Super, ({ method: { lexeme: methodName }}) => {
if (methodName === 'init') methodName = '__init__'
return `${lastSuperclass}.${methodName}`
})
ASTNodeMap.set(This, ({ keyword }) => keyword.lexeme)
ASTNodeMap.set(Block, ({ statements }, scope, options, initialIndent) => {
if (statements.length <= 0) {
return indentation(scope, options) + 'pass'
}
return statements.map(stmt => loxToPython2(stmt, scope, options, initialIndent)).join('\n')
})
// Expressions
ASTNodeMap.set(Var, ({ name: { lexeme } }) => lexeme)
ASTNodeMap.set(Grouping, ({ expression }) => '(' + loxToPython2(expression) + ')')
const handleBinary = node => {
const left = loxToPython2(node.left)
const operator = node.operator.lexeme
const right = loxToPython2(node.right)
return [left, operator, right].join(' ')
}
ASTNodeMap.set(Binary, handleBinary)
ASTNodeMap.set(Logical, handleBinary)
const handleUnary = unary => unary === '!' ? 'not ' : unary
ASTNodeMap.set(Unary, node => {
const operator = handleUnary(node.operator.lexeme)
const right = loxToPython2(node.right)
return operator + right
})
ASTNodeMap.set(Call, node => {
const args = node.arguments.map(args => loxToPython2(args))
const callee = loxToPython2(node.callee)
if (String(callee).endsWith('__init__')) args.unshift('this')
const argsStr = args.join(', ')
return `${callee}(${argsStr})`
})
ASTNodeMap.set(Assignment, node => {
const name = node.name.lexeme
const value = loxToPython2(node.value)
return name + ' = ' + value
})
const handleLiteral = value => {
if (typeof value === 'string') {
return `"${value}"`
} else if (typeof value === 'boolean') {
return value ? 'True' : 'False'
} else if (value === null) {
return 'None'
} else {
return value
}
}
ASTNodeMap.set(Literal, ({ value }) => handleLiteral(value))
const loxToPython2 = (node, scope = 0, optionsOverride = {}) => {
const options = Object.assign({}, {
indent: '\t',
}, optionsOverride)
if (!(node instanceof Object)) return handleLiteral(node)
if (ASTNodeMap.has(node.constructor)) {
return ASTNodeMap.get(node.constructor)(node, scope, options)
}
throw new Error(`Don't support that AST node yet`, node.constructor)
}
module.exports = {
loxToPython2
}