Skip to content

Commit

Permalink
Added classes to loxfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berezin committed Jun 25, 2019
1 parent 427c6c1 commit e37a1e3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ formatButton.onclick = () => {
try {
const ast = parse(code.value)
global.ast = ast
// console.log(ast.map(stmt => printLoxAST(stmt)).join('\n'))
code.value = ast.map(stmt => printLoxAST(stmt)).join('\n')
handleError(null)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion docs/browser.e9f8be96.js

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

48 changes: 41 additions & 7 deletions transpilers/lox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const {
Literal,
While,
// @TODO: Support Classes
// Class,
// Get,
// Set,
Class,
Get,
Set: SetExpr,
Super,
This,
Grouping,
Return,
LoxFunction,
Expand Down Expand Up @@ -46,14 +48,16 @@ ASTNodeMap.set(Condition, ({ condition, thenBranch, elseBranch }, scope, options
return conditionSection + thenSection + (elseSection ? ` else ${elseSection}` : '')
})

ASTNodeMap.set(LoxFunction, ({ bodyStatements: body, name: { lexeme: name }, params}, scope, options) => {
const printFunction = ({ bodyStatements: body, name: { lexeme: name }, params}, scope, options, func = true) => {
const parameters = params.map(token => token.lexeme)
const head = `fun ${name}${condChar(options.spaceBeforeParams)}(${parameters.join(', ')}) {`
const id = func ? 'fun ' : ''
const head = id + `${name}${condChar(options.spaceBeforeParams)}(${parameters.join(', ')}) {`
const fnBody = body.map(stmt => printLoxAST(stmt, scope + 1, options))
const tail = options.indent.repeat(scope) + '}' + condChar(options.functionNewlines, '\n')
return [head, ...fnBody, tail].join('\n')
})
}

ASTNodeMap.set(LoxFunction, printFunction)

const handleWhileLoop = ({ body, condition }, scope, options) => {
const cond = printLoxAST(condition)
Expand Down Expand Up @@ -86,6 +90,36 @@ ASTNodeMap.set(While, (node, scope, options) => {
}
})

ASTNodeMap.set(Class, ({ name, superclass, methods }, scope, options) => {
let superclassStr = ''
if (superclass) {
const superClassName = superclass.name.lexeme
superclassStr = ` < ${superClassName}`
}
const head = `class ${name.lexeme}${superclassStr} {`
const tail = options.indent.repeat(scope) + '}'
let body = methods.map(node => options.indent.repeat(scope + 1) + printFunction(node, scope + 1, options, false))
return [head, ...body, tail].join('\n')
})

ASTNodeMap.set(Get, ({ name, object }, scope, options) => {
const nameStr = name.lexeme ? name.lexeme : printLoxAST(name, scope, options, false)
const objectStr = printLoxAST(object, scope, options, false)
return objectStr + '.' + nameStr
})

ASTNodeMap.set(SetExpr, ({ name, object, value }, scope, options) => {
const nameStr = name.lexeme ? name.lexeme : printLoxAST(name, scope, options, false)
const objectStr = printLoxAST(object, scope, options, false)
const val = printLoxAST(value, scope, options)
return objectStr + '.' + nameStr + ' = ' + val
})

ASTNodeMap.set(Super, ({ method: { lexeme: methodName }}) => {
return `super.${methodName}`
})

ASTNodeMap.set(This, ({ keyword }) => keyword.lexeme)

const blockForLoop = ({ statements }, scope, options) => {
const iter = printLoxAST(statements[0], scope, options, false)
Expand Down Expand Up @@ -162,7 +196,7 @@ const printLoxAST = (node, scope = 0, optionsOverride = {}, initialIndent = true
const indentation = (initialIndent ? options.indent.repeat(scope) : '')
return indentation + ASTNodeMap.get(node.constructor)(node, scope, options)
}
throw new Error(`Don't support classes yet`)
// throw new Error(`Don't support classes yet`)
}

module.exports = {
Expand Down

0 comments on commit e37a1e3

Please sign in to comment.