add variable declaration with binary expressions
This commit is contained in:
@ -1,27 +1,46 @@
|
||||
import { Program, BinaryExpression, BinaryPart } from "./abstractSyntaxTree";
|
||||
import { Program, BinaryExpression, BinaryPart } from './abstractSyntaxTree'
|
||||
|
||||
export function recast(ast: Program, previousWrittenCode = ''): string {
|
||||
return ast.body
|
||||
.map((statement) => {
|
||||
if(statement.type === "ExpressionStatement") {
|
||||
if(statement.expression.type === "BinaryExpression") {
|
||||
return recastBinaryExpression(statement.expression);
|
||||
}
|
||||
if (statement.type === 'ExpressionStatement') {
|
||||
if (statement.expression.type === 'BinaryExpression') {
|
||||
return recastBinaryExpression(statement.expression)
|
||||
}
|
||||
return statement.type;
|
||||
} else if (statement.type === 'VariableDeclaration') {
|
||||
return statement.declarations
|
||||
.map((declaration) => {
|
||||
if (declaration.init.type === 'BinaryExpression') {
|
||||
return `${statement.kind} ${
|
||||
declaration.id.name
|
||||
} = ${recastBinaryExpression(declaration.init)}`
|
||||
} else if (declaration.init.type === 'Literal') {
|
||||
return `${statement.kind} ${declaration.id.name} = ${declaration.init.value}`
|
||||
}
|
||||
return ''
|
||||
})
|
||||
.join('')
|
||||
}
|
||||
return statement.type
|
||||
})
|
||||
.join("\n");
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
function recastBinaryExpression(expression: BinaryExpression): string {
|
||||
return `${recastBinaryPart(expression.left)} ${expression.operator} ${recastBinaryPart(expression.right)}`
|
||||
return `${recastBinaryPart(expression.left)} ${
|
||||
expression.operator
|
||||
} ${recastBinaryPart(expression.right)}`
|
||||
}
|
||||
|
||||
function recastBinaryPart(part: BinaryPart): string {
|
||||
if(part.type === "Literal") {
|
||||
return String(part?.value);
|
||||
} else if(part.type === "Identifier") {
|
||||
return part.name;
|
||||
if (part.type === 'Literal') {
|
||||
if (typeof part.value === 'string') {
|
||||
const quote = part.raw.includes('"') ? '"' : "'"
|
||||
return `${quote}${part.value}${quote}`
|
||||
}
|
||||
throw new Error(`Cannot recast ${part}`);
|
||||
return String(part?.value)
|
||||
} else if (part.type === 'Identifier') {
|
||||
return part.name
|
||||
}
|
||||
throw new Error(`Cannot recast ${part}`)
|
||||
}
|
||||
|
Reference in New Issue
Block a user