Skip to content

Commit

Permalink
Fixed blank function definitions, nil in python transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berezin committed Jun 14, 2019
1 parent ffd94a0 commit b43415d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/closureLinkedList.lox
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ fun printTail (n) {
var list = Link(1, Link(2, Link(3, nil)));

traverse(list, printValue);
printTail(list, printTail);
printTail(list);
11 changes: 4 additions & 7 deletions transpilers/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ const indentation = (scope, options) => options.indent.repeat(scope)
const ASTNodeMap = new Map()

// Declarations
ASTNodeMap.set(ExpressionStatement, (node, scope, options, initialIndent) => {
// console.log(initialIndent)
return indentation(scope, options) + loxToPython2(node.expression, 0, options, initialIndent)
})
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))

Expand All @@ -54,8 +51,8 @@ ASTNodeMap.set(Condition, ({ condition, thenBranch, elseBranch }, scope, options
ASTNodeMap.set(LoxFunction, ({ bodyStatements: body, name: { lexeme: name }, params}, scope, options) => {
const parameters = params.map(token => token.lexeme)
const head = indentation(scope, options) + `def ${name}(${parameters.join(', ')}):`
const fnBody = body.map(stmt => loxToPython2(stmt, scope + 1, options))
// console.log(fnBody)
// 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')
})

Expand Down Expand Up @@ -108,7 +105,7 @@ ASTNodeMap.set(Literal, ({ value }) => {
if (typeof value === 'string') {
return `"${value}"`
} else if (value === null) {
return 'nil'
return 'None'
} else {
return value
}
Expand Down

0 comments on commit b43415d

Please sign in to comment.