-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started python transpiler, cleaned up parser code. Added LL example
- Loading branch information
Daniel Berezin
committed
Jun 11, 2019
1 parent
518749f
commit 53e237e
Showing
6 changed files
with
259 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.linting.pylintEnabled": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#! /usr/bin/env node | ||
const fs = require('fs') | ||
const chalk = require('chalk') | ||
const { parse } = require('./index') | ||
const { loxToPython2 } = require('./transpilers/python') | ||
const { formatLoxError } = require('./errors') | ||
|
||
let options = {} | ||
|
||
const printErrorMessage = (e, code) => { | ||
const { oneLiner, preErrorSection, errorSection, postErrorSection } = formatLoxError(e, code) | ||
console.error(oneLiner) | ||
if (errorSection) { | ||
console.error(preErrorSection + chalk.bgRed(errorSection) + postErrorSection) | ||
} | ||
} | ||
|
||
const fmtFile = (filename, outputfile = 'a.py') => { | ||
try { | ||
const file = fs.readFileSync(filename, 'utf8') | ||
try { | ||
const newText = parse(file) | ||
.map(stmt => loxToPython2(stmt, 0, options)) | ||
.join('\n') | ||
if (!options.silent) console.log(newText) | ||
if (options.write) fs.writeFileSync(outputfile, newText) | ||
} catch (e) { | ||
printErrorMessage(e, file) | ||
} | ||
} catch (e) { | ||
console.error(`YALI could not read the file ${filename}`) | ||
console.error(e) | ||
} | ||
} | ||
|
||
const optionRegex = /--(\w+)(?:=(.+))?/ | ||
const processOptions = args => | ||
args | ||
.map(arg => { | ||
const match = optionRegex.exec(arg) | ||
if (match) { | ||
const [, option, value] = match | ||
if (!value) { | ||
options[option] = !options[option] | ||
} else { | ||
options[option] = value | ||
} | ||
} else { | ||
return arg | ||
} | ||
}) | ||
.filter(Boolean) | ||
|
||
const main = argv => { | ||
process.title = 'lox2python' | ||
const args = processOptions(argv.slice(2)) | ||
if (options.help) { | ||
console.log('Usage: lox2python [script] --out="a.py"') | ||
return 0 | ||
} | ||
|
||
if (args.length === 1) { | ||
fmtFile(args[0]) | ||
} else { | ||
console.error('Usage: lox2python [script]') | ||
return 64 | ||
} | ||
} | ||
|
||
main(process.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
const { | ||
Binary, | ||
Unary, | ||
Var, | ||
Call, | ||
Literal, | ||
While, | ||
// @TODO: Support Classes | ||
// Class, | ||
// Get, | ||
// Set, | ||
Grouping, | ||
Return, | ||
LoxFunction, | ||
PrintStatement, | ||
ExpressionStatement, | ||
VarStatement, | ||
Assignment, | ||
Logical, | ||
Block, | ||
Condition | ||
} = require('../types') | ||
|
||
// const condChar = (condition, replacer = ' ') => condition ? replacer : '' | ||
|
||
const ASTNodeMap = new Map() | ||
|
||
// Declarations | ||
ASTNodeMap.set(ExpressionStatement, (node, scope, options, initialIndent) => { | ||
// console.log(initialIndent) | ||
return loxToPython2(node.expression, 0, options, initialIndent) | ||
}) | ||
|
||
ASTNodeMap.set(PrintStatement, node => 'print ' + loxToPython2(node.expression)) | ||
|
||
ASTNodeMap.set(Return, node => 'return ' + loxToPython2(node.value)) | ||
|
||
ASTNodeMap.set(VarStatement, (node) => { | ||
const name = node.name.lexeme | ||
const initializer = node.initializer ? loxToPython2(node.initializer) : null | ||
// Python doesn't have plain declarations... | ||
if (!node.initializer) return '' | ||
return `${name}` + (initializer ? ` = ${initializer}` : '') | ||
}) | ||
|
||
ASTNodeMap.set(Condition, ({ condition, thenBranch, elseBranch }, scope, options) => { | ||
const cond = loxToPython2(condition) | ||
const conditionSection = `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${options.indent.repeat(scope)}else:\n${elseSection}` : '') | ||
}) | ||
|
||
ASTNodeMap.set(LoxFunction, ({ bodyStatements: body, name: { lexeme: name }, params}, scope, options) => { | ||
const parameters = params.map(token => token.lexeme) | ||
const head = `def ${name}(${parameters.join(', ')}):` | ||
const fnBody = body.map(stmt => loxToPython2(stmt, scope + 1, options)) | ||
// console.log(fnBody) | ||
return [head, ...fnBody].join('\n') | ||
}) | ||
|
||
ASTNodeMap.set(While, ({ body, condition }, scope, options) => { | ||
const cond = loxToPython2(condition) | ||
const conditionSection = `while ${cond}:\n` | ||
const bodySection = loxToPython2(body, scope + 1, options, false) | ||
return conditionSection + bodySection | ||
}) | ||
|
||
ASTNodeMap.set(Block, ({ statements }, scope, options, initialIndent) => 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) | ||
|
||
ASTNodeMap.set(Unary, node => { | ||
const operator = node.operator.lexeme | ||
const right = loxToPython2(node.right) | ||
return operator + right | ||
}) | ||
|
||
|
||
ASTNodeMap.set(Call, node => { | ||
const args = node.arguments.map(args => loxToPython2(args)).join(', ') | ||
const callee = loxToPython2(node.callee) | ||
return `${callee}(${args})` | ||
}) | ||
|
||
ASTNodeMap.set(Assignment, node => { | ||
const name = node.name.lexeme | ||
const value = loxToPython2(node.value) | ||
return name + ' = ' + value | ||
}) | ||
|
||
ASTNodeMap.set(Literal, ({ value }) => { | ||
if (typeof value === 'string') { | ||
return `"${value}"` | ||
} else if (value === null) { | ||
return 'nil' | ||
} else { | ||
return value | ||
} | ||
}) | ||
|
||
const loxToPython2 = (node, scope = 0, optionsOverride = {}, initialIndent = true) => { | ||
const options = Object.assign({}, { | ||
indent: ' ', | ||
}, optionsOverride) | ||
|
||
if (ASTNodeMap.has(node.constructor)) { | ||
const indentation = (initialIndent ? options.indent.repeat(scope) : '') | ||
return indentation + ASTNodeMap.get(node.constructor)(node, scope, options) | ||
} | ||
throw new Error(`Don't support that context yet`, node.constructor) | ||
} | ||
|
||
module.exports = { | ||
loxToPython2 | ||
} |