Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

focus on extrude literal when extruding sketch #2

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ export const Toolbar = () => {
onClick={() => {
if (!ast) return
const pathToNode = getNodePathFromSourceRange(ast, selectionRange)
const { modifiedAst } = extrudeSketch(ast, pathToNode)
updateAst(modifiedAst)
const { modifiedAst, pathToExtrudeArg } = extrudeSketch(
ast,
pathToNode
)
updateAst(modifiedAst, pathToExtrudeArg)
}}
className="border m-1 px-1 rounded"
>
Expand All @@ -73,8 +76,12 @@ export const Toolbar = () => {
onClick={() => {
if (!ast) return
const pathToNode = getNodePathFromSourceRange(ast, selectionRange)
const { modifiedAst } = extrudeSketch(ast, pathToNode, false)
updateAst(modifiedAst)
const { modifiedAst, pathToExtrudeArg } = extrudeSketch(
ast,
pathToNode,
false
)
updateAst(modifiedAst, pathToExtrudeArg)
}}
className="border m-1 px-1 rounded"
>
Expand Down
5 changes: 4 additions & 1 deletion src/components/RenderViewerArtifacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ function MovingSphere({
const { originalXY } = useMemo(() => {
if (ast) {
const thePath = getNodePathFromSourceRange(ast, sourceRange)
const callExpression = getNodeFromPath(ast, thePath) as CallExpression
const { node: callExpression } = getNodeFromPath<CallExpression>(
ast,
thePath
)
const [xArg, yArg] = callExpression?.arguments || []
const x = xArg?.type === 'Literal' ? xArg.value : -1
const y = yArg?.type === 'Literal' ? yArg.value : -1
Expand Down
139 changes: 76 additions & 63 deletions src/lang/abstractSyntaxTree.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
import { PathToNode } from './executor'
import { Token } from './tokeniser'

type syntaxType =
| 'Program'
| 'ExpressionStatement'
| 'BinaryExpression'
| 'NumberLiteral'
| 'StringLiteral'
| 'CallExpression'
| 'Identifier'
| 'BlockStatement'
| 'IfStatement'
| 'WhileStatement'
| 'FunctionDeclaration'
| 'ReturnStatement'
| 'VariableDeclaration'
| 'VariableDeclarator'
| 'AssignmentExpression'
| 'UnaryExpression'
| 'MemberExpression'
| 'ArrayExpression'
| 'ObjectExpression'
| 'ObjectProperty'
| 'Property'
| 'LogicalExpression'
| 'ConditionalExpression'
| 'ForStatement'
| 'ForInStatement'
| 'ForOfStatement'
| 'BreakStatement'
| 'ContinueStatement'
| 'SwitchStatement'
| 'SwitchCase'
| 'ThrowStatement'
| 'TryStatement'
| 'CatchClause'
| 'ClassDeclaration'
| 'ClassBody'
| 'MethodDefinition'
| 'NewExpression'
| 'ThisExpression'
| 'UpdateExpression'
// | "ArrowFunctionExpression"
| 'FunctionExpression'
| 'SketchExpression'
| 'PipeExpression'
| 'PipeSubstitution'
| 'YieldExpression'
| 'AwaitExpression'
| 'ImportDeclaration'
| 'ImportSpecifier'
| 'ImportDefaultSpecifier'
| 'ImportNamespaceSpecifier'
| 'ExportNamedDeclaration'
| 'ExportDefaultDeclaration'
| 'ExportAllDeclaration'
| 'ExportSpecifier'
| 'TaggedTemplateExpression'
| 'TemplateLiteral'
| 'TemplateElement'
| 'SpreadElement'
| 'RestElement'
| 'SequenceExpression'
| 'DebuggerStatement'
| 'LabeledStatement'
| 'DoWhileStatement'
| 'WithStatement'
| 'EmptyStatement'
| 'Literal'
| 'ArrayPattern'
| 'ObjectPattern'
| 'AssignmentPattern'
| 'MetaProperty'
| 'Super'
| 'Import'
| 'RegExpLiteral'
| 'BooleanLiteral'
| 'NullLiteral'
| 'TypeAnnotation'
// | 'NumberLiteral'
// | 'StringLiteral'
// | 'IfStatement'
// | 'WhileStatement'
// | 'FunctionDeclaration'
// | 'AssignmentExpression'
// | 'UnaryExpression'
// | 'Property'
// | 'LogicalExpression'
// | 'ConditionalExpression'
// | 'ForStatement'
// | 'ForInStatement'
// | 'ForOfStatement'
// | 'BreakStatement'
// | 'ContinueStatement'
// | 'SwitchStatement'
// | 'SwitchCase'
// | 'ThrowStatement'
// | 'TryStatement'
// | 'CatchClause'
// | 'ClassDeclaration'
// | 'ClassBody'
// | 'MethodDefinition'
// | 'NewExpression'
// | 'ThisExpression'
// | 'UpdateExpression'
// | 'YieldExpression'
// | 'AwaitExpression'
// | 'ImportDeclaration'
// | 'ImportSpecifier'
// | 'ImportDefaultSpecifier'
// | 'ImportNamespaceSpecifier'
// | 'ExportNamedDeclaration'
// | 'ExportDefaultDeclaration'
// | 'ExportAllDeclaration'
// | 'ExportSpecifier'
// | 'TaggedTemplateExpression'
// | 'TemplateLiteral'
// | 'TemplateElement'
// | 'SpreadElement'
// | 'RestElement'
// | 'SequenceExpression'
// | 'DebuggerStatement'
// | 'LabeledStatement'
// | 'DoWhileStatement'
// | 'WithStatement'
// | 'EmptyStatement'
// | 'ArrayPattern'
// | 'ObjectPattern'
// | 'AssignmentPattern'
// | 'MetaProperty'
// | 'Super'
// | 'Import'
// | 'RegExpLiteral'
// | 'BooleanLiteral'
// | 'NullLiteral'
// | 'TypeAnnotation'

export interface Program {
type: syntaxType
Expand Down Expand Up @@ -1345,27 +1345,37 @@ function debuggerr(tokens: Token[], indexes: number[], msg = ''): string {
return debugResult
}

export function getNodeFromPath(
export function getNodeFromPath<T>(
node: Program,
path: (string | number)[],
stopAt: string = '',
returnEarly = false
) {
): {
node: T
path: PathToNode
} {
let currentNode = node as any
let stopAtNode = null
let successfulPaths: (string | number)[] = []
let successfulPaths: PathToNode = []
let pathsExplored: PathToNode = []
for (const pathItem of path) {
try {
if (typeof currentNode[pathItem] !== 'object')
throw new Error('not an object')
currentNode = currentNode[pathItem]
successfulPaths.push(pathItem)
if (!stopAtNode) {
pathsExplored.push(pathItem)
}
if (currentNode.type === stopAt) {
// it will match the deepest node of the type
// instead of returning at the first match
stopAtNode = currentNode
if (returnEarly) {
return stopAtNode
return {
node: stopAtNode,
path: pathsExplored,
}
}
}
} catch (e) {
Expand All @@ -1378,7 +1388,10 @@ export function getNodeFromPath(
)
}
}
return stopAtNode || currentNode
return {
node: stopAtNode || currentNode,
path: pathsExplored,
}
}

type Path = (string | number)[]
Expand Down
2 changes: 1 addition & 1 deletion src/lang/getNodePathFromSourceRange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('testing getNodePathFromSourceRange', () => {

const ast = abstractSyntaxTree(lexer(code))
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
const node = getNodeFromPath(ast, nodePath)
const { node } = getNodeFromPath<any>(ast, nodePath)

expect([node.start, node.end]).toEqual(sourceRange)
expect(node.type).toBe('CallExpression')
Expand Down
68 changes: 50 additions & 18 deletions src/lang/modifyAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ export function addLine(
): { modifiedAst: Program; pathToNode: (string | number)[] } {
const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 }
const sketchExpression = getNodeFromPath(
const { node: sketchExpression } = getNodeFromPath<SketchExpression>(
_node,
pathToNode,
'SketchExpression'
) as SketchExpression
)
const line: ExpressionStatement = {
type: 'ExpressionStatement',
...dumbyStartend,
Expand Down Expand Up @@ -251,7 +251,10 @@ export function changeArguments(
const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 }
// const thePath = getNodePathFromSourceRange(_node, sourceRange)
const callExpression = getNodeFromPath(_node, pathToNode) as CallExpression
const { node: callExpression } = getNodeFromPath<CallExpression>(
_node,
pathToNode
)
const newXArg: CallExpression['arguments'][number] =
callExpression.arguments[0].type === 'Literal'
? {
Expand Down Expand Up @@ -285,24 +288,29 @@ export function extrudeSketch(
node: Program,
pathToNode: (string | number)[],
shouldPipe = true
): { modifiedAst: Program; pathToNode: (string | number)[] } {
): {
modifiedAst: Program
pathToNode: PathToNode
pathToExtrudeArg: PathToNode
} {
const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 }
const sketchExpression = getNodeFromPath(
const { node: sketchExpression } = getNodeFromPath<SketchExpression>(
_node,
pathToNode,
'SketchExpression'
) as SketchExpression
)

// determine if sketchExpression is in a pipeExpression or not
const pipeExpression = getNodeFromPath(_node, pathToNode, 'PipeExpression')
const isInPipeExpression = pipeExpression.type === 'PipeExpression'

const variableDeclorator = getNodeFromPath(
const { node: pipeExpression } = getNodeFromPath<PipeExpression>(
_node,
pathToNode,
'VariableDeclarator'
) as VariableDeclarator
'PipeExpression'
)
const isInPipeExpression = pipeExpression.type === 'PipeExpression'

const { node: variableDeclorator, path: pathToDecleration } =
getNodeFromPath<VariableDeclarator>(_node, pathToNode, 'VariableDeclarator')

const extrudeCall: CallExpression = {
type: 'CallExpression',
Expand Down Expand Up @@ -346,10 +354,19 @@ export function extrudeSketch(
}

variableDeclorator.init = pipeChain
const pathToExtrudeArg = [
...pathToDecleration,
'init',
'body',
pipeChain.body.length - 1,
'arguments',
0,
]

return {
modifiedAst: _node,
pathToNode,
pathToExtrudeArg,
}
}
const name = findUniqueName(node, 'part')
Expand All @@ -372,9 +389,19 @@ export function extrudeSketch(
}
const showCallIndex = getShowIndex(_node)
_node.body.splice(showCallIndex, 0, VariableDeclaration)
const pathToExtrudeArg = [
'body',
showCallIndex,
'declarations',
0,
'init',
'arguments',
0,
]
return {
modifiedAst: addToShow(_node, name),
pathToNode: [...pathToNode.slice(0, -1), showCallIndex],
pathToExtrudeArg,
}
}

Expand All @@ -385,22 +412,27 @@ export function sketchOnExtrudedFace(
const _node = { ...node }
const dumbyStartend = { start: 0, end: 0 }
const newSketchName = findUniqueName(node, 'part')
const oldSketchName = getNodeFromPath(
const oldSketchName = getNodeFromPath<VariableDeclarator>(
_node,
pathToNode,
'VariableDeclarator',
true
).id.name
const expression = getNodeFromPath(_node, pathToNode, 'CallExpression') as
| VariableDeclarator
| CallExpression
).node.id.name
const { node: expression } = getNodeFromPath<
VariableDeclarator | CallExpression
>(_node, pathToNode, 'CallExpression')

const pathName =
expression.type === 'VariableDeclarator'
? expression.id.name
: findUniqueName(node, 'path', 2)

if (expression.type === 'CallExpression') {
const block = getNodeFromPath(_node, pathToNode, 'BlockStatement')
const { node: block } = getNodeFromPath<BlockStatement>(
_node,
pathToNode,
'BlockStatement'
)
const expressionIndex = getLastIndex(pathToNode)
if (expression.callee.name !== 'lineTo')
throw new Error('expected a lineTo call')
Expand Down
Loading