Skip to content

Commit

Permalink
Updated python transpiler to better handle empty functions and statem…
Browse files Browse the repository at this point in the history
…ents, Booleans and Classes. Updated cli standard lib
  • Loading branch information
Daniel Berezin committed Jun 26, 2019
1 parent b207b35 commit 78904a0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
15 changes: 15 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@ const printErrorMessage = (e, code) => {
}
}

let cache = []
const getChar = () => {
if (cache.length <= 0) {
const input = fs.readFileSync(0).toString()
input.split('').forEach(char => cache.push(char))
}
return cache.shift()
}

const makeCLIEnvironment = () => {
const env = new Environment()
env.setBuiltin('readFile', (_vars, args) => fs.readFileSync(args[0], 'utf8'))
env.setBuiltin('exit', (_vars, args) => process.exit(args[0] || 0))
env.setBuiltin('chr', (_vars, args) => String.fromCharCode(args[0]))
env.setBuiltin('getc', () => {
const val = getChar()
return val ? val.charCodeAt(0) : -1
})
return env
}

Expand Down
6 changes: 6 additions & 0 deletions examples/getc.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

var c = getc();

print c;

print "done";
19 changes: 0 additions & 19 deletions hi.py

This file was deleted.

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions transpilers/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const {
Condition
} = require('../types')

// const condChar = (condition, replacer = ' ') => condition ? replacer : ''
const indentation = (scope, options) => options.indent.repeat(scope)

let lastSuperclass = 'super'
Expand Down Expand Up @@ -104,7 +103,12 @@ ASTNodeMap.set(Super, ({ method: { lexeme: methodName }}) => {
ASTNodeMap.set(This, ({ keyword }) => keyword.lexeme)


ASTNodeMap.set(Block, ({ statements }, scope, options, initialIndent) => statements.map(stmt => loxToPython2(stmt, scope, options, initialIndent)).join('\n'))
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)
Expand All @@ -123,8 +127,10 @@ ASTNodeMap.set(Binary, handleBinary)

ASTNodeMap.set(Logical, handleBinary)

const handleUnary = unary => unary === '!' ? 'not ' : unary

ASTNodeMap.set(Unary, node => {
const operator = node.operator.lexeme
const operator = handleUnary(node.operator.lexeme)
const right = loxToPython2(node.right)
return operator + right
})
Expand All @@ -144,21 +150,25 @@ ASTNodeMap.set(Assignment, node => {
return name + ' = ' + value
})

ASTNodeMap.set(Literal, ({ 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)
}
Expand Down

0 comments on commit 78904a0

Please sign in to comment.