2022-11-26 08:34:23 +11:00
|
|
|
import { Token } from './tokeniser'
|
2022-11-13 11:14:30 +11:00
|
|
|
|
|
|
|
type syntaxType =
|
2022-11-26 08:34:23 +11:00
|
|
|
| 'Program'
|
|
|
|
| 'ExpressionStatement'
|
|
|
|
| 'BinaryExpression'
|
|
|
|
| 'NumberLiteral'
|
|
|
|
| 'StringLiteral'
|
|
|
|
| 'CallExpression'
|
|
|
|
| 'Identifier'
|
|
|
|
| 'BlockStatement'
|
|
|
|
| 'IfStatement'
|
|
|
|
| 'WhileStatement'
|
|
|
|
| 'FunctionDeclaration'
|
|
|
|
| 'ReturnStatement'
|
|
|
|
| 'VariableDeclaration'
|
|
|
|
| 'VariableDeclarator'
|
|
|
|
| 'AssignmentExpression'
|
|
|
|
| 'UnaryExpression'
|
|
|
|
| 'MemberExpression'
|
|
|
|
| 'ArrayExpression'
|
|
|
|
| 'ObjectExpression'
|
|
|
|
| 'Property'
|
|
|
|
| 'LogicalExpression'
|
|
|
|
| 'ConditionalExpression'
|
|
|
|
| 'ForStatement'
|
|
|
|
| 'ForInStatement'
|
|
|
|
| 'ForOfStatement'
|
|
|
|
| 'BreakStatement'
|
|
|
|
| 'ContinueStatement'
|
|
|
|
| 'SwitchStatement'
|
|
|
|
| 'SwitchCase'
|
|
|
|
| 'ThrowStatement'
|
|
|
|
| 'TryStatement'
|
|
|
|
| 'CatchClause'
|
|
|
|
| 'ClassDeclaration'
|
|
|
|
| 'ClassBody'
|
|
|
|
| 'MethodDefinition'
|
|
|
|
| 'NewExpression'
|
|
|
|
| 'ThisExpression'
|
|
|
|
| 'UpdateExpression'
|
2022-11-17 20:17:00 +11:00
|
|
|
// | "ArrowFunctionExpression"
|
2022-11-26 08:34:23 +11:00
|
|
|
| 'FunctionExpression'
|
|
|
|
| 'SketchExpression'
|
2022-12-02 21:00:57 +11:00
|
|
|
| 'PipeExpression'
|
|
|
|
| 'PipeSubstitution'
|
2022-11-26 08:34:23 +11:00
|
|
|
| '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'
|
2022-11-13 11:14:30 +11:00
|
|
|
|
|
|
|
export interface Program {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: syntaxType
|
|
|
|
start: number
|
|
|
|
end: number
|
|
|
|
body: Body[]
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
interface GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: syntaxType
|
|
|
|
start: number
|
|
|
|
end: number
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ExpressionStatement extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'ExpressionStatement'
|
|
|
|
expression: Value
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeExpressionStatement(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
2022-11-17 20:17:00 +11:00
|
|
|
): { expression: ExpressionStatement; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const { token: nextToken } = nextMeaningfulToken(tokens, index)
|
|
|
|
if (nextToken.type === 'brace' && nextToken.value === '(') {
|
|
|
|
const { expression, lastIndex } = makeCallExpression(tokens, index)
|
2022-11-14 13:28:16 +11:00
|
|
|
return {
|
2022-11-17 20:17:00 +11:00
|
|
|
expression: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'ExpressionStatement',
|
2022-11-17 20:17:00 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: expression.end,
|
|
|
|
expression,
|
|
|
|
},
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
const { expression, lastIndex } = makeBinaryExpression(tokens, index)
|
2022-11-13 11:14:30 +11:00
|
|
|
return {
|
2022-11-17 20:17:00 +11:00
|
|
|
expression: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'ExpressionStatement',
|
2022-11-17 20:17:00 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: expression.end,
|
|
|
|
expression,
|
|
|
|
},
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 19:03:09 +11:00
|
|
|
export interface CallExpression extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'CallExpression'
|
|
|
|
callee: Identifier
|
|
|
|
arguments: Value[]
|
|
|
|
optional: boolean
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeCallExpression(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): {
|
2022-11-26 08:34:23 +11:00
|
|
|
expression: CallExpression
|
|
|
|
lastIndex: number
|
2022-11-14 13:28:16 +11:00
|
|
|
} {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const braceToken = nextMeaningfulToken(tokens, index)
|
2022-11-14 13:28:16 +11:00
|
|
|
// const firstArgumentToken = nextMeaningfulToken(tokens, braceToken.index);
|
2022-11-26 08:34:23 +11:00
|
|
|
const callee = makeIdentifier(tokens, index)
|
|
|
|
const args = makeArguments(tokens, braceToken.index)
|
2022-11-14 13:28:16 +11:00
|
|
|
// const closingBraceToken = nextMeaningfulToken(tokens, args.lastIndex);
|
2022-11-26 08:34:23 +11:00
|
|
|
const closingBraceToken = tokens[args.lastIndex]
|
2022-11-14 13:28:16 +11:00
|
|
|
return {
|
|
|
|
expression: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'CallExpression',
|
2022-11-14 13:28:16 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: closingBraceToken.end,
|
|
|
|
callee,
|
|
|
|
arguments: args.arguments,
|
|
|
|
optional: false,
|
|
|
|
},
|
|
|
|
lastIndex: args.lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeArguments(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
2022-11-18 08:20:18 +11:00
|
|
|
previousArgs: Value[] = []
|
2022-11-14 13:28:16 +11:00
|
|
|
): {
|
2022-11-26 08:34:23 +11:00
|
|
|
arguments: Value[]
|
|
|
|
lastIndex: number
|
2022-11-14 13:28:16 +11:00
|
|
|
} {
|
2022-11-26 08:34:23 +11:00
|
|
|
const braceOrCommaToken = tokens[index]
|
|
|
|
const argumentToken = nextMeaningfulToken(tokens, index)
|
2022-11-17 20:17:00 +11:00
|
|
|
const shouldFinishRecursion =
|
2022-11-26 08:34:23 +11:00
|
|
|
braceOrCommaToken.type === 'brace' && braceOrCommaToken.value === ')'
|
2022-11-14 13:28:16 +11:00
|
|
|
if (shouldFinishRecursion) {
|
|
|
|
return {
|
|
|
|
arguments: previousArgs,
|
|
|
|
lastIndex: index,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
const nextBraceOrCommaToken = nextMeaningfulToken(tokens, argumentToken.index)
|
2022-11-17 20:17:00 +11:00
|
|
|
const isIdentifierOrLiteral =
|
2022-11-26 08:34:23 +11:00
|
|
|
nextBraceOrCommaToken.token.type === 'comma' ||
|
|
|
|
nextBraceOrCommaToken.token.type === 'brace'
|
2022-11-14 13:28:16 +11:00
|
|
|
if (!isIdentifierOrLiteral) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const { expression, lastIndex } = makeBinaryExpression(tokens, index)
|
|
|
|
return makeArguments(tokens, lastIndex, [...previousArgs, expression])
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
2022-12-02 21:00:57 +11:00
|
|
|
if (
|
|
|
|
argumentToken.token.type === 'operator' &&
|
|
|
|
argumentToken.token.value === '%'
|
|
|
|
) {
|
|
|
|
const value: PipeSubstitution = {
|
|
|
|
type: 'PipeSubstitution',
|
|
|
|
start: argumentToken.token.start,
|
|
|
|
end: argumentToken.token.end,
|
|
|
|
}
|
|
|
|
return makeArguments(tokens, nextBraceOrCommaToken.index, [
|
|
|
|
...previousArgs,
|
|
|
|
value,
|
|
|
|
])
|
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (argumentToken.token.type === 'word') {
|
|
|
|
const identifier = makeIdentifier(tokens, argumentToken.index)
|
2022-11-14 13:28:16 +11:00
|
|
|
return makeArguments(tokens, nextBraceOrCommaToken.index, [
|
|
|
|
...previousArgs,
|
|
|
|
identifier,
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
2022-11-14 13:28:16 +11:00
|
|
|
} else if (
|
2022-11-26 08:34:23 +11:00
|
|
|
argumentToken.token.type === 'number' ||
|
|
|
|
argumentToken.token.type === 'string'
|
2022-11-14 13:28:16 +11:00
|
|
|
) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const literal = makeLiteral(tokens, argumentToken.index)
|
2022-11-17 20:17:00 +11:00
|
|
|
return makeArguments(tokens, nextBraceOrCommaToken.index, [
|
|
|
|
...previousArgs,
|
|
|
|
literal,
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
|
|
|
} else if (
|
|
|
|
argumentToken.token.type === 'brace' &&
|
|
|
|
argumentToken.token.value === ')'
|
|
|
|
) {
|
2022-11-20 17:43:21 +11:00
|
|
|
return makeArguments(tokens, argumentToken.index, previousArgs)
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
2022-12-02 21:00:57 +11:00
|
|
|
throw new Error('Expected a previous Argument if statement to match')
|
2022-11-14 13:28:16 +11:00
|
|
|
}
|
|
|
|
|
2022-11-13 11:14:30 +11:00
|
|
|
interface VariableDeclaration extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'VariableDeclaration'
|
|
|
|
declarations: VariableDeclarator[]
|
|
|
|
kind: 'const' | 'unknown' | 'fn' | 'sketch' | 'path' //| "solid" | "surface" | "face"
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeVariableDeclaration(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { declaration: VariableDeclaration; lastIndex: number } {
|
2022-12-03 22:50:46 +11:00
|
|
|
// token index should point to a declaration keyword i.e. const, fn, sketch, path
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const declarationStartToken = nextMeaningfulToken(tokens, index)
|
2022-11-13 11:14:30 +11:00
|
|
|
const { declarations, lastIndex } = makeVariableDeclarators(
|
|
|
|
tokens,
|
|
|
|
declarationStartToken.index
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
2022-11-13 11:14:30 +11:00
|
|
|
return {
|
|
|
|
declaration: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'VariableDeclaration',
|
2022-11-13 11:14:30 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: declarations[declarations.length - 1].end,
|
2022-11-17 20:17:00 +11:00
|
|
|
kind:
|
2022-11-26 08:34:23 +11:00
|
|
|
currentToken.value === 'const'
|
|
|
|
? 'const'
|
|
|
|
: currentToken.value === 'fn'
|
|
|
|
? 'fn'
|
|
|
|
: currentToken.value === 'sketch'
|
|
|
|
? 'sketch'
|
|
|
|
: currentToken.value === 'path'
|
|
|
|
? 'path'
|
|
|
|
: 'unknown',
|
2022-11-13 11:14:30 +11:00
|
|
|
declarations,
|
|
|
|
},
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 19:03:09 +11:00
|
|
|
export type Value =
|
2022-11-18 08:20:18 +11:00
|
|
|
| Literal
|
|
|
|
| Identifier
|
|
|
|
| BinaryExpression
|
|
|
|
| FunctionExpression
|
2022-11-20 17:43:21 +11:00
|
|
|
| CallExpression
|
2022-11-26 08:34:23 +11:00
|
|
|
| SketchExpression
|
2022-12-02 21:00:57 +11:00
|
|
|
| PipeExpression
|
|
|
|
| PipeSubstitution
|
2022-11-18 08:20:18 +11:00
|
|
|
|
|
|
|
function makeValue(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { value: Value; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const { token: nextToken } = nextMeaningfulToken(tokens, index)
|
|
|
|
if (nextToken.type === 'brace' && nextToken.value === '(') {
|
|
|
|
const { expression, lastIndex } = makeCallExpression(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
value: expression,
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-12-03 22:50:46 +11:00
|
|
|
if (
|
|
|
|
(currentToken.type === 'word' || currentToken.type === 'number') &&
|
|
|
|
nextToken.type === 'operator'
|
|
|
|
) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const { expression, lastIndex } = makeBinaryExpression(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
value: expression,
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (currentToken.type === 'word') {
|
|
|
|
const identifier = makeIdentifier(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
value: identifier,
|
|
|
|
lastIndex: index,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (currentToken.type === 'number' || currentToken.type === 'string') {
|
|
|
|
const literal = makeLiteral(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
value: literal,
|
|
|
|
lastIndex: index,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-12-02 21:00:57 +11:00
|
|
|
throw new Error('Expected a previous Value if statement to match')
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
|
|
|
|
2022-11-13 11:14:30 +11:00
|
|
|
interface VariableDeclarator extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'VariableDeclarator'
|
|
|
|
id: Identifier
|
|
|
|
init: Value
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeVariableDeclarators(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
previousDeclarators: VariableDeclarator[] = []
|
|
|
|
): {
|
2022-11-26 08:34:23 +11:00
|
|
|
declarations: VariableDeclarator[]
|
|
|
|
lastIndex: number
|
2022-11-13 11:14:30 +11:00
|
|
|
} {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const assignmentToken = nextMeaningfulToken(tokens, index)
|
|
|
|
const declarationToken = previousMeaningfulToken(tokens, index)
|
|
|
|
const contentsStartToken = nextMeaningfulToken(tokens, assignmentToken.index)
|
|
|
|
const nextAfterInit = nextMeaningfulToken(tokens, contentsStartToken.index)
|
2022-12-04 15:50:52 +11:00
|
|
|
const pipeStartIndex =
|
|
|
|
assignmentToken?.token?.type === 'operator'
|
|
|
|
? contentsStartToken.index
|
|
|
|
: assignmentToken.index
|
2022-12-03 22:50:46 +11:00
|
|
|
const nextPipeOperator = hasPipeOperator(tokens, pipeStartIndex)
|
2022-11-26 08:34:23 +11:00
|
|
|
let init: Value
|
|
|
|
let lastIndex = contentsStartToken.index
|
2022-12-02 21:00:57 +11:00
|
|
|
if (nextPipeOperator) {
|
|
|
|
const { expression, lastIndex: pipeLastIndex } = makePipeExpression(
|
|
|
|
tokens,
|
|
|
|
assignmentToken.index
|
|
|
|
)
|
|
|
|
init = expression
|
|
|
|
lastIndex = pipeLastIndex
|
|
|
|
} else if (
|
2022-11-26 08:34:23 +11:00
|
|
|
contentsStartToken.token.type === 'brace' &&
|
|
|
|
contentsStartToken.token.value === '('
|
2022-11-17 20:17:00 +11:00
|
|
|
) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const closingBraceIndex = findClosingBrace(tokens, contentsStartToken.index)
|
|
|
|
const arrowToken = nextMeaningfulToken(tokens, closingBraceIndex)
|
2022-11-17 20:17:00 +11:00
|
|
|
if (
|
2022-11-26 08:34:23 +11:00
|
|
|
arrowToken.token.type === 'operator' &&
|
|
|
|
arrowToken.token.value === '=>'
|
2022-11-17 20:17:00 +11:00
|
|
|
) {
|
|
|
|
const { expression, lastIndex: arrowFunctionLastIndex } =
|
2022-11-26 08:34:23 +11:00
|
|
|
makeFunctionExpression(tokens, contentsStartToken.index)
|
|
|
|
init = expression
|
|
|
|
lastIndex = arrowFunctionLastIndex
|
2022-11-17 20:17:00 +11:00
|
|
|
} else {
|
2022-11-26 08:34:23 +11:00
|
|
|
throw new Error('TODO - handle expression with braces')
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-20 17:43:21 +11:00
|
|
|
} else if (
|
2022-11-26 08:34:23 +11:00
|
|
|
declarationToken.token.type === 'word' &&
|
|
|
|
declarationToken.token.value === 'sketch'
|
2022-11-20 17:43:21 +11:00
|
|
|
) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const sketchExp = makeSketchExpression(tokens, assignmentToken.index)
|
|
|
|
init = sketchExp.expression
|
|
|
|
lastIndex = sketchExp.lastIndex
|
|
|
|
} else if (nextAfterInit.token?.type === 'operator') {
|
|
|
|
const binExp = makeBinaryExpression(tokens, contentsStartToken.index)
|
|
|
|
init = binExp.expression
|
|
|
|
lastIndex = binExp.lastIndex
|
2022-11-20 17:43:21 +11:00
|
|
|
} else if (
|
2022-11-26 08:34:23 +11:00
|
|
|
nextAfterInit.token?.type === 'brace' &&
|
|
|
|
nextAfterInit.token.value === '('
|
2022-11-20 17:43:21 +11:00
|
|
|
) {
|
2022-11-26 08:34:23 +11:00
|
|
|
const callExInfo = makeCallExpression(tokens, contentsStartToken.index)
|
|
|
|
init = callExInfo.expression
|
|
|
|
lastIndex = callExInfo.lastIndex
|
2022-11-13 11:14:30 +11:00
|
|
|
} else {
|
2022-11-26 08:34:23 +11:00
|
|
|
init = makeLiteral(tokens, contentsStartToken.index)
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
const currentDeclarator: VariableDeclarator = {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'VariableDeclarator',
|
2022-11-13 11:14:30 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: tokens[lastIndex].end,
|
|
|
|
id: makeIdentifier(tokens, index),
|
|
|
|
init,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
return {
|
|
|
|
declarations: [...previousDeclarators, currentDeclarator],
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
export type BinaryPart = Literal | Identifier
|
2022-11-13 11:14:30 +11:00
|
|
|
// | BinaryExpression
|
|
|
|
// | CallExpression
|
|
|
|
// | MemberExpression
|
|
|
|
// | ArrayExpression
|
|
|
|
// | ObjectExpression
|
|
|
|
// | UnaryExpression
|
|
|
|
// | LogicalExpression
|
|
|
|
// | ConditionalExpression
|
|
|
|
|
2022-11-26 19:03:09 +11:00
|
|
|
export interface Literal extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'Literal'
|
|
|
|
value: string | number | boolean | null
|
|
|
|
raw: string
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Identifier extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'Identifier'
|
|
|
|
name: string
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeIdentifier(token: Token[], index: number): Identifier {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = token[index]
|
2022-11-13 11:14:30 +11:00
|
|
|
return {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'Identifier',
|
2022-11-13 11:14:30 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: currentToken.end,
|
|
|
|
name: currentToken.value,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-12-02 21:00:57 +11:00
|
|
|
interface PipeSubstitution extends GeneralStatement {
|
|
|
|
type: 'PipeSubstitution'
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:14:30 +11:00
|
|
|
function makeLiteral(tokens: Token[], index: number): Literal {
|
2022-11-26 08:34:23 +11:00
|
|
|
const token = tokens[index]
|
2022-11-14 13:28:16 +11:00
|
|
|
const value =
|
2022-11-26 08:34:23 +11:00
|
|
|
token.type === 'number' ? Number(token.value) : token.value.slice(1, -1)
|
2022-11-13 11:14:30 +11:00
|
|
|
return {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'Literal',
|
2022-11-13 11:14:30 +11:00
|
|
|
start: token.start,
|
|
|
|
end: token.end,
|
|
|
|
value,
|
|
|
|
raw: token.value,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-20 09:41:21 +11:00
|
|
|
export interface BinaryExpression extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'BinaryExpression'
|
|
|
|
operator: string
|
|
|
|
left: BinaryPart
|
|
|
|
right: BinaryPart
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-18 08:20:18 +11:00
|
|
|
function makeBinaryPart(
|
2022-11-13 11:14:30 +11:00
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
2022-11-18 08:20:18 +11:00
|
|
|
): { part: BinaryPart; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
if (currentToken.type === 'word') {
|
|
|
|
const identifier = makeIdentifier(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
part: identifier,
|
|
|
|
lastIndex: index,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (currentToken.type === 'number' || currentToken.type === 'string') {
|
|
|
|
const literal = makeLiteral(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
part: literal,
|
|
|
|
lastIndex: index,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-12-02 21:00:57 +11:00
|
|
|
throw new Error('Expected a previous BinaryPart if statement to match')
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeBinaryExpression(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { expression: BinaryExpression; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const { part: left } = makeBinaryPart(tokens, index)
|
2022-11-13 11:14:30 +11:00
|
|
|
const { token: operatorToken, index: operatorIndex } = nextMeaningfulToken(
|
|
|
|
tokens,
|
|
|
|
index
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
|
|
|
const rightToken = nextMeaningfulToken(tokens, operatorIndex)
|
|
|
|
const { part: right } = makeBinaryPart(tokens, rightToken.index)
|
2022-11-13 11:14:30 +11:00
|
|
|
return {
|
|
|
|
expression: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'BinaryExpression',
|
2022-11-13 11:14:30 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: right.end,
|
|
|
|
left,
|
|
|
|
operator: operatorToken.value,
|
|
|
|
right,
|
|
|
|
},
|
|
|
|
lastIndex: rightToken.index,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 19:03:09 +11:00
|
|
|
export interface SketchExpression extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'SketchExpression'
|
|
|
|
body: BlockStatement
|
2022-11-20 17:43:21 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeSketchExpression(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { expression: SketchExpression; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const { block, lastIndex: bodyLastIndex } = makeBlockStatement(tokens, index)
|
|
|
|
const endToken = tokens[bodyLastIndex]
|
2022-11-20 17:43:21 +11:00
|
|
|
|
|
|
|
return {
|
|
|
|
expression: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'SketchExpression',
|
2022-11-20 17:43:21 +11:00
|
|
|
start: currentToken.start,
|
2022-11-26 08:34:23 +11:00
|
|
|
end: endToken.end,
|
2022-11-20 17:43:21 +11:00
|
|
|
body: block,
|
|
|
|
},
|
|
|
|
lastIndex: bodyLastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-20 17:43:21 +11:00
|
|
|
}
|
|
|
|
|
2022-12-02 21:00:57 +11:00
|
|
|
export interface PipeExpression extends GeneralStatement {
|
|
|
|
type: 'PipeExpression'
|
|
|
|
body: Value[]
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePipeExpression(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { expression: PipeExpression; lastIndex: number } {
|
|
|
|
const currentToken = tokens[index]
|
|
|
|
const { body, lastIndex: bodyLastIndex } = makePipeBody(tokens, index)
|
|
|
|
const endToken = tokens[bodyLastIndex]
|
|
|
|
return {
|
|
|
|
expression: {
|
|
|
|
type: 'PipeExpression',
|
|
|
|
start: currentToken.start,
|
|
|
|
end: endToken.end,
|
|
|
|
body,
|
|
|
|
},
|
|
|
|
lastIndex: bodyLastIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePipeBody(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
previousValues: Value[] = []
|
|
|
|
): { body: Value[]; lastIndex: number } {
|
|
|
|
const currentToken = tokens[index]
|
|
|
|
const expressionStart = nextMeaningfulToken(tokens, index)
|
|
|
|
let value: Value
|
|
|
|
let lastIndex: number
|
|
|
|
if (currentToken.type === 'operator') {
|
|
|
|
const beep = makeValue(tokens, expressionStart.index)
|
|
|
|
value = beep.value
|
|
|
|
lastIndex = beep.lastIndex
|
|
|
|
} else if (currentToken.type === 'brace' && currentToken.value === '{') {
|
|
|
|
const sketch = makeSketchExpression(tokens, index)
|
|
|
|
value = sketch.expression
|
|
|
|
lastIndex = sketch.lastIndex
|
|
|
|
} else {
|
|
|
|
throw new Error('Expected a previous PipeValue if statement to match')
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextPipeToken = hasPipeOperator(tokens, index)
|
|
|
|
if (!nextPipeToken) {
|
|
|
|
return {
|
|
|
|
body: [...previousValues, value],
|
|
|
|
lastIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// const nextToken = nextMeaningfulToken(tokens, nextPipeToken.index + 1)
|
|
|
|
return makePipeBody(tokens, nextPipeToken.index, [...previousValues, value])
|
|
|
|
}
|
|
|
|
|
2022-11-26 19:03:09 +11:00
|
|
|
export interface FunctionExpression extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'FunctionExpression'
|
|
|
|
id: Identifier | null
|
|
|
|
params: Identifier[]
|
|
|
|
body: BlockStatement
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeFunctionExpression(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { expression: FunctionExpression; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const closingBraceIndex = findClosingBrace(tokens, index)
|
|
|
|
const arrowToken = nextMeaningfulToken(tokens, closingBraceIndex)
|
|
|
|
const bodyStartToken = nextMeaningfulToken(tokens, arrowToken.index)
|
|
|
|
const { params } = makeParams(tokens, index)
|
2022-11-17 20:17:00 +11:00
|
|
|
const { block, lastIndex: bodyLastIndex } = makeBlockStatement(
|
|
|
|
tokens,
|
|
|
|
bodyStartToken.index
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
2022-11-17 20:17:00 +11:00
|
|
|
return {
|
|
|
|
expression: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'FunctionExpression',
|
2022-11-17 20:17:00 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: tokens[bodyLastIndex].end,
|
|
|
|
id: null,
|
|
|
|
params,
|
|
|
|
body: block,
|
|
|
|
},
|
|
|
|
lastIndex: bodyLastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
|
|
|
|
2022-11-18 08:20:18 +11:00
|
|
|
function makeParams(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
previousParams: Identifier[] = []
|
|
|
|
): { params: Identifier[]; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const braceOrCommaToken = tokens[index]
|
|
|
|
const argumentToken = nextMeaningfulToken(tokens, index)
|
2022-11-18 08:20:18 +11:00
|
|
|
const shouldFinishRecursion =
|
2022-11-26 08:34:23 +11:00
|
|
|
(argumentToken.token.type === 'brace' &&
|
|
|
|
argumentToken.token.value === ')') ||
|
|
|
|
(braceOrCommaToken.type === 'brace' && braceOrCommaToken.value === ')')
|
2022-11-18 08:20:18 +11:00
|
|
|
if (shouldFinishRecursion) {
|
2022-11-26 08:34:23 +11:00
|
|
|
return { params: previousParams, lastIndex: index }
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
const nextBraceOrCommaToken = nextMeaningfulToken(tokens, argumentToken.index)
|
|
|
|
const identifier = makeIdentifier(tokens, argumentToken.index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return makeParams(tokens, nextBraceOrCommaToken.index, [
|
|
|
|
...previousParams,
|
|
|
|
identifier,
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
|
|
|
|
2022-11-17 20:17:00 +11:00
|
|
|
interface BlockStatement extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'BlockStatement'
|
|
|
|
body: Body[]
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeBlockStatement(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { block: BlockStatement; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const openingCurly = tokens[index]
|
|
|
|
const nextToken = nextMeaningfulToken(tokens, index)
|
2022-11-17 20:17:00 +11:00
|
|
|
const { body, lastIndex } =
|
2022-11-26 08:34:23 +11:00
|
|
|
nextToken.token.value === '}'
|
2022-11-17 20:17:00 +11:00
|
|
|
? { body: [], lastIndex: nextToken.index }
|
2022-11-26 08:34:23 +11:00
|
|
|
: makeBody({ tokens, tokenIndex: nextToken.index })
|
2022-11-17 20:17:00 +11:00
|
|
|
return {
|
|
|
|
block: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'BlockStatement',
|
2022-11-17 20:17:00 +11:00
|
|
|
start: openingCurly.start,
|
2022-11-28 09:37:46 +11:00
|
|
|
end: tokens[lastIndex]?.end || 0,
|
2022-11-17 20:17:00 +11:00
|
|
|
body,
|
|
|
|
},
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
|
|
|
|
2022-11-18 08:20:18 +11:00
|
|
|
interface ReturnStatement extends GeneralStatement {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'ReturnStatement'
|
|
|
|
argument: Value
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeReturnStatement(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { statement: ReturnStatement; lastIndex: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const nextToken = nextMeaningfulToken(tokens, index)
|
|
|
|
const { value, lastIndex } = makeValue(tokens, nextToken.index)
|
2022-11-18 08:20:18 +11:00
|
|
|
return {
|
|
|
|
statement: {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'ReturnStatement',
|
2022-11-18 08:20:18 +11:00
|
|
|
start: currentToken.start,
|
|
|
|
end: tokens[lastIndex].end,
|
|
|
|
argument: value,
|
|
|
|
},
|
|
|
|
lastIndex,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
export type All = Program | ExpressionStatement[] | BinaryExpression | Literal
|
2022-11-13 11:14:30 +11:00
|
|
|
|
|
|
|
function nextMeaningfulToken(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
offset: number = 1
|
|
|
|
): { token: Token; index: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const newIndex = index + offset
|
|
|
|
const token = tokens[newIndex]
|
2022-11-13 11:14:30 +11:00
|
|
|
if (!token) {
|
2022-11-26 08:34:23 +11:00
|
|
|
return { token, index: tokens.length }
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (token.type === 'whitespace') {
|
|
|
|
return nextMeaningfulToken(tokens, index, offset + 1)
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
return { token, index: newIndex }
|
2022-11-13 11:14:30 +11:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:43:21 +11:00
|
|
|
function previousMeaningfulToken(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
offset: number = 1
|
|
|
|
): { token: Token; index: number } {
|
2022-11-26 08:34:23 +11:00
|
|
|
const newIndex = index - offset
|
|
|
|
const token = tokens[newIndex]
|
2022-11-20 17:43:21 +11:00
|
|
|
if (!token) {
|
2022-11-26 08:34:23 +11:00
|
|
|
return { token, index: 0 }
|
2022-11-20 17:43:21 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (token.type === 'whitespace') {
|
|
|
|
return previousMeaningfulToken(tokens, index, offset + 1)
|
2022-11-20 17:43:21 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
return { token, index: newIndex }
|
2022-11-20 17:43:21 +11:00
|
|
|
}
|
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
type Body = ExpressionStatement | VariableDeclaration | ReturnStatement
|
2022-11-13 11:14:30 +11:00
|
|
|
|
2022-11-17 20:17:00 +11:00
|
|
|
function makeBody(
|
2022-11-20 17:43:21 +11:00
|
|
|
{
|
|
|
|
tokens,
|
|
|
|
tokenIndex = 0,
|
|
|
|
}: {
|
2022-11-26 08:34:23 +11:00
|
|
|
tokens: Token[]
|
|
|
|
tokenIndex?: number
|
2022-11-20 17:43:21 +11:00
|
|
|
},
|
2022-11-17 20:17:00 +11:00
|
|
|
previousBody: Body[] = []
|
|
|
|
): { body: Body[]; lastIndex: number } {
|
|
|
|
if (tokenIndex >= tokens.length) {
|
2022-11-26 08:34:23 +11:00
|
|
|
return { body: previousBody, lastIndex: tokenIndex }
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-18 08:20:18 +11:00
|
|
|
|
2022-11-26 08:34:23 +11:00
|
|
|
const token = tokens[tokenIndex]
|
|
|
|
if (token.type === 'brace' && token.value === '}') {
|
|
|
|
return { body: previousBody, lastIndex: tokenIndex }
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (typeof token === 'undefined') {
|
|
|
|
console.log('probably should throw')
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (token.type === 'whitespace') {
|
|
|
|
return makeBody({ tokens, tokenIndex: tokenIndex + 1 }, previousBody)
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
const nextToken = nextMeaningfulToken(tokens, tokenIndex)
|
2022-11-17 20:17:00 +11:00
|
|
|
if (
|
2022-11-26 08:34:23 +11:00
|
|
|
token.type === 'word' &&
|
|
|
|
(token.value === 'const' ||
|
|
|
|
token.value === 'fn' ||
|
|
|
|
token.value === 'sketch' ||
|
|
|
|
token.value === 'path')
|
2022-11-17 20:17:00 +11:00
|
|
|
) {
|
|
|
|
const { declaration, lastIndex } = makeVariableDeclaration(
|
|
|
|
tokens,
|
|
|
|
tokenIndex
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
|
|
|
const nextThing = nextMeaningfulToken(tokens, lastIndex)
|
2022-11-20 17:43:21 +11:00
|
|
|
return makeBody({ tokens, tokenIndex: nextThing.index }, [
|
|
|
|
...previousBody,
|
|
|
|
declaration,
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
if (token.type === 'word' && token.value === 'return') {
|
|
|
|
const { statement, lastIndex } = makeReturnStatement(tokens, tokenIndex)
|
|
|
|
const nextThing = nextMeaningfulToken(tokens, lastIndex)
|
2022-11-20 17:43:21 +11:00
|
|
|
return makeBody({ tokens, tokenIndex: nextThing.index }, [
|
|
|
|
...previousBody,
|
|
|
|
statement,
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
2022-11-18 08:20:18 +11:00
|
|
|
}
|
2022-11-20 17:43:21 +11:00
|
|
|
if (
|
2022-11-26 08:34:23 +11:00
|
|
|
token.type === 'word' &&
|
|
|
|
nextToken.token.type === 'brace' &&
|
|
|
|
nextToken.token.value === '('
|
2022-11-20 17:43:21 +11:00
|
|
|
) {
|
2022-11-17 20:17:00 +11:00
|
|
|
const { expression, lastIndex } = makeExpressionStatement(
|
|
|
|
tokens,
|
|
|
|
tokenIndex
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
|
|
|
const nextThing = nextMeaningfulToken(tokens, lastIndex)
|
2022-11-20 17:43:21 +11:00
|
|
|
return makeBody({ tokens, tokenIndex: nextThing.index }, [
|
|
|
|
...previousBody,
|
|
|
|
expression,
|
2022-11-26 08:34:23 +11:00
|
|
|
])
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
|
|
|
if (
|
2022-11-26 08:34:23 +11:00
|
|
|
(token.type === 'number' || token.type === 'word') &&
|
|
|
|
nextMeaningfulToken(tokens, tokenIndex).token.type === 'operator'
|
2022-11-17 20:17:00 +11:00
|
|
|
) {
|
|
|
|
const { expression, lastIndex } = makeExpressionStatement(
|
|
|
|
tokens,
|
|
|
|
tokenIndex
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
2022-11-17 20:17:00 +11:00
|
|
|
// return startTree(tokens, tokenIndex, [...previousBody, makeExpressionStatement(tokens, tokenIndex)]);
|
2022-11-26 08:34:23 +11:00
|
|
|
return { body: [...previousBody, expression], lastIndex }
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-26 08:34:23 +11:00
|
|
|
throw new Error('Unexpected token')
|
2022-11-17 20:17:00 +11:00
|
|
|
}
|
2022-11-13 11:14:30 +11:00
|
|
|
export const abstractSyntaxTree = (tokens: Token[]): Program => {
|
2022-11-26 08:34:23 +11:00
|
|
|
const { body } = makeBody({ tokens })
|
2022-11-13 11:14:30 +11:00
|
|
|
const program: Program = {
|
2022-11-26 08:34:23 +11:00
|
|
|
type: 'Program',
|
2022-11-13 11:14:30 +11:00
|
|
|
start: 0,
|
|
|
|
end: body[body.length - 1].end,
|
|
|
|
body: body,
|
2022-11-26 08:34:23 +11:00
|
|
|
}
|
|
|
|
return program
|
|
|
|
}
|
2022-11-17 16:06:38 +11:00
|
|
|
|
2022-12-02 21:00:57 +11:00
|
|
|
export function findNextDeclarationKeyword(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { token: Token | null; index: number } {
|
|
|
|
const nextToken = nextMeaningfulToken(tokens, index)
|
|
|
|
if (nextToken.index >= tokens.length) {
|
|
|
|
return { token: null, index: tokens.length - 1 }
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
nextToken.token.type === 'word' &&
|
|
|
|
(nextToken.token.value === 'const' ||
|
|
|
|
nextToken.token.value === 'fn' ||
|
|
|
|
nextToken.token.value === 'sketch' ||
|
|
|
|
nextToken.token.value === 'path')
|
|
|
|
) {
|
|
|
|
return nextToken
|
|
|
|
}
|
2022-12-03 22:50:46 +11:00
|
|
|
if (nextToken.token.type === 'brace' && nextToken.token.value === '(') {
|
|
|
|
const closingBraceIndex = findClosingBrace(tokens, nextToken.index)
|
|
|
|
const arrowToken = nextMeaningfulToken(tokens, closingBraceIndex)
|
|
|
|
if (
|
|
|
|
arrowToken?.token?.type === 'operator' &&
|
|
|
|
arrowToken.token.value === '=>'
|
|
|
|
) {
|
|
|
|
return nextToken
|
|
|
|
}
|
|
|
|
// return findNextDeclarationKeyword(tokens, nextToken.index)
|
|
|
|
// probably should do something else here
|
|
|
|
// throw new Error('Unexpected token')
|
|
|
|
}
|
2022-12-02 21:00:57 +11:00
|
|
|
return findNextDeclarationKeyword(tokens, nextToken.index)
|
|
|
|
}
|
|
|
|
|
2022-12-03 22:50:46 +11:00
|
|
|
export function findNextCallExpression(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { token: Token | null; index: number } {
|
|
|
|
const nextToken = nextMeaningfulToken(tokens, index)
|
|
|
|
const veryNextToken = tokens[nextToken.index + 1] // i.e. without whitespace
|
|
|
|
if (nextToken.index >= tokens.length) {
|
|
|
|
return { token: null, index: tokens.length - 1 }
|
|
|
|
}
|
2022-12-04 15:50:52 +11:00
|
|
|
if (
|
|
|
|
nextToken.token.type === 'word' &&
|
|
|
|
veryNextToken?.type === 'brace' &&
|
|
|
|
veryNextToken?.value === '('
|
|
|
|
) {
|
2022-12-03 22:50:46 +11:00
|
|
|
return nextToken
|
|
|
|
}
|
|
|
|
return findNextCallExpression(tokens, nextToken.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findNextClosingCurlyBrace(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number
|
|
|
|
): { token: Token | null; index: number } {
|
|
|
|
const nextToken = nextMeaningfulToken(tokens, index)
|
|
|
|
if (nextToken.index >= tokens.length) {
|
|
|
|
return { token: null, index: tokens.length - 1 }
|
|
|
|
}
|
|
|
|
if (nextToken.token.type === 'brace' && nextToken.token.value === '}') {
|
|
|
|
return nextToken
|
|
|
|
}
|
|
|
|
if (nextToken.token.type === 'brace' && nextToken.token.value === '{') {
|
|
|
|
const closingBraceIndex = findClosingBrace(tokens, nextToken.index)
|
|
|
|
const tokenAfterClosingBrace = nextMeaningfulToken(
|
|
|
|
tokens,
|
|
|
|
closingBraceIndex
|
|
|
|
)
|
|
|
|
return findNextClosingCurlyBrace(tokens, tokenAfterClosingBrace.index)
|
|
|
|
}
|
|
|
|
return findNextClosingCurlyBrace(tokens, nextToken.index)
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:00:57 +11:00
|
|
|
export function hasPipeOperator(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
_limitIndex = -1
|
|
|
|
): { token: Token; index: number } | false {
|
2022-12-03 22:50:46 +11:00
|
|
|
// this probably still needs some work
|
|
|
|
// should be called on expression statuments (i.e "lineTo" for lineTo(10, 10)) or "{" for sketch declarations
|
2022-12-02 21:00:57 +11:00
|
|
|
let limitIndex = _limitIndex
|
|
|
|
if (limitIndex === -1) {
|
2022-12-03 22:50:46 +11:00
|
|
|
const callExpressionEnd = isCallExpression(tokens, index)
|
|
|
|
if (callExpressionEnd !== -1) {
|
2022-12-04 15:50:52 +11:00
|
|
|
const tokenAfterCallExpression = nextMeaningfulToken(
|
|
|
|
tokens,
|
|
|
|
callExpressionEnd
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
tokenAfterCallExpression?.token?.type === 'operator' &&
|
|
|
|
tokenAfterCallExpression.token.value === '|>'
|
|
|
|
) {
|
2022-12-03 22:50:46 +11:00
|
|
|
return tokenAfterCallExpression
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const currentToken = tokens[index]
|
|
|
|
if (currentToken?.type === 'brace' && currentToken?.value === '{') {
|
|
|
|
const closingBraceIndex = findClosingBrace(tokens, index)
|
2022-12-04 15:50:52 +11:00
|
|
|
const tokenAfterClosingBrace = nextMeaningfulToken(
|
|
|
|
tokens,
|
|
|
|
closingBraceIndex
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
tokenAfterClosingBrace?.token?.type === 'operator' &&
|
|
|
|
tokenAfterClosingBrace.token.value === '|>'
|
|
|
|
) {
|
2022-12-03 22:50:46 +11:00
|
|
|
return tokenAfterClosingBrace
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-12-02 21:00:57 +11:00
|
|
|
const nextDeclaration = findNextDeclarationKeyword(tokens, index)
|
|
|
|
limitIndex = nextDeclaration.index
|
|
|
|
}
|
|
|
|
const nextToken = nextMeaningfulToken(tokens, index)
|
|
|
|
if (nextToken.index >= limitIndex) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (nextToken.token.type === 'operator' && nextToken.token.value === '|>') {
|
|
|
|
return nextToken
|
|
|
|
}
|
|
|
|
return hasPipeOperator(tokens, nextToken.index, limitIndex)
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:06:38 +11:00
|
|
|
export function findClosingBrace(
|
|
|
|
tokens: Token[],
|
|
|
|
index: number,
|
|
|
|
_braceCount: number = 0,
|
2022-11-26 08:34:23 +11:00
|
|
|
_searchOpeningBrace: string = ''
|
2022-11-17 16:06:38 +11:00
|
|
|
): number {
|
2022-12-03 22:50:46 +11:00
|
|
|
// should be called with the index of the opening brace
|
2022-11-17 16:06:38 +11:00
|
|
|
const closingBraceMap: { [key: string]: string } = {
|
2022-11-26 08:34:23 +11:00
|
|
|
'(': ')',
|
|
|
|
'{': '}',
|
|
|
|
'[': ']',
|
|
|
|
}
|
|
|
|
const currentToken = tokens[index]
|
|
|
|
let searchOpeningBrace = _searchOpeningBrace
|
|
|
|
|
|
|
|
const isFirstCall = !searchOpeningBrace && _braceCount === 0
|
2022-11-17 16:06:38 +11:00
|
|
|
if (isFirstCall) {
|
2022-11-26 08:34:23 +11:00
|
|
|
searchOpeningBrace = currentToken.value
|
|
|
|
if (!['(', '{', '['].includes(searchOpeningBrace)) {
|
2022-11-17 16:06:38 +11:00
|
|
|
throw new Error(
|
|
|
|
`expected to be started on a opening brace ( { [, instead found '${searchOpeningBrace}'`
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
2022-11-17 16:06:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const foundClosingBrace =
|
|
|
|
_braceCount === 1 &&
|
2022-11-26 08:34:23 +11:00
|
|
|
currentToken.value === closingBraceMap[searchOpeningBrace]
|
|
|
|
const foundAnotherOpeningBrace = currentToken.value === searchOpeningBrace
|
2022-11-17 16:06:38 +11:00
|
|
|
const foundAnotherClosingBrace =
|
2022-11-26 08:34:23 +11:00
|
|
|
currentToken.value === closingBraceMap[searchOpeningBrace]
|
2022-11-17 16:06:38 +11:00
|
|
|
|
|
|
|
if (foundClosingBrace) {
|
2022-11-26 08:34:23 +11:00
|
|
|
return index
|
2022-11-17 16:06:38 +11:00
|
|
|
}
|
|
|
|
if (foundAnotherOpeningBrace) {
|
|
|
|
return findClosingBrace(
|
|
|
|
tokens,
|
|
|
|
index + 1,
|
|
|
|
_braceCount + 1,
|
|
|
|
searchOpeningBrace
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
2022-11-17 16:06:38 +11:00
|
|
|
}
|
|
|
|
if (foundAnotherClosingBrace) {
|
|
|
|
return findClosingBrace(
|
|
|
|
tokens,
|
|
|
|
index + 1,
|
|
|
|
_braceCount - 1,
|
|
|
|
searchOpeningBrace
|
2022-11-26 08:34:23 +11:00
|
|
|
)
|
2022-11-17 16:06:38 +11:00
|
|
|
}
|
|
|
|
// non-brace token, increment and continue
|
2022-11-26 08:34:23 +11:00
|
|
|
return findClosingBrace(tokens, index + 1, _braceCount, searchOpeningBrace)
|
2022-11-17 16:06:38 +11:00
|
|
|
}
|
2022-11-28 09:37:46 +11:00
|
|
|
|
|
|
|
export function addSketchTo(
|
|
|
|
node: Program,
|
2022-12-04 15:50:52 +11:00
|
|
|
axis: 'xy' | 'xz' | 'yz',
|
2022-11-28 09:37:46 +11:00
|
|
|
name = ''
|
2022-12-04 15:50:52 +11:00
|
|
|
): { modifiedAst: Program; id: string; pathToNode: (string | number)[] } {
|
2022-11-28 09:37:46 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const dumbyStartend = { start: 0, end: 0 }
|
|
|
|
const _name = name || findUniqueName(node, 'mySketch')
|
|
|
|
const sketchBody: BlockStatement = {
|
|
|
|
type: 'BlockStatement',
|
|
|
|
...dumbyStartend,
|
|
|
|
body: [],
|
|
|
|
}
|
|
|
|
const sketch: SketchExpression = {
|
|
|
|
type: 'SketchExpression',
|
|
|
|
...dumbyStartend,
|
|
|
|
body: sketchBody,
|
|
|
|
}
|
2022-12-04 15:50:52 +11:00
|
|
|
|
|
|
|
const rotate: CallExpression = {
|
|
|
|
type: 'CallExpression',
|
|
|
|
...dumbyStartend,
|
|
|
|
callee: {
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name: axis === 'xz' ? 'rx' : 'ry',
|
|
|
|
},
|
|
|
|
arguments: [
|
|
|
|
{
|
|
|
|
type: 'Literal',
|
|
|
|
...dumbyStartend,
|
2022-12-23 07:37:42 +11:00
|
|
|
value: axis === 'yz' ? 90 : 90,
|
|
|
|
raw: axis === 'yz' ? '90' : '90',
|
2022-12-04 15:50:52 +11:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'PipeSubstitution',
|
|
|
|
...dumbyStartend,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
optional: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
const pipChain: PipeExpression = {
|
|
|
|
type: 'PipeExpression',
|
|
|
|
...dumbyStartend,
|
|
|
|
body: [sketch, rotate],
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:37:46 +11:00
|
|
|
const sketchVariableDeclaration: VariableDeclaration = {
|
|
|
|
type: 'VariableDeclaration',
|
|
|
|
...dumbyStartend,
|
|
|
|
kind: 'sketch',
|
|
|
|
declarations: [
|
|
|
|
{
|
|
|
|
type: 'VariableDeclarator',
|
|
|
|
...dumbyStartend,
|
|
|
|
id: {
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name: _name,
|
|
|
|
},
|
2022-12-04 15:50:52 +11:00
|
|
|
init: axis === 'xy' ? sketch : pipChain,
|
2022-11-28 09:37:46 +11:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
const showCallIndex = getShowIndex(_node)
|
2022-12-04 15:50:52 +11:00
|
|
|
let sketchIndex = showCallIndex
|
2022-11-28 09:37:46 +11:00
|
|
|
if (showCallIndex === -1) {
|
|
|
|
_node.body = [...node.body, sketchVariableDeclaration]
|
2022-12-04 15:50:52 +11:00
|
|
|
sketchIndex = _node.body.length - 1
|
2022-11-28 09:37:46 +11:00
|
|
|
} else {
|
|
|
|
const newBody = [...node.body]
|
|
|
|
newBody.splice(showCallIndex, 0, sketchVariableDeclaration)
|
|
|
|
_node.body = newBody
|
|
|
|
}
|
2022-12-04 15:50:52 +11:00
|
|
|
let pathToNode: (string | number)[] = [
|
|
|
|
'body',
|
|
|
|
sketchIndex,
|
|
|
|
'declarations',
|
|
|
|
'0',
|
|
|
|
'init',
|
|
|
|
]
|
|
|
|
if (axis !== 'xy') {
|
|
|
|
pathToNode = [...pathToNode, 'body', '0']
|
|
|
|
}
|
2022-11-28 09:37:46 +11:00
|
|
|
|
|
|
|
return {
|
|
|
|
modifiedAst: addToShow(_node, _name),
|
|
|
|
id: _name,
|
2022-12-04 15:50:52 +11:00
|
|
|
pathToNode,
|
2022-11-28 09:37:46 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findUniqueName(
|
|
|
|
ast: Program | string,
|
|
|
|
name: string,
|
|
|
|
index = 1
|
|
|
|
): string {
|
|
|
|
let searchStr = ''
|
|
|
|
if (typeof ast === 'string') {
|
|
|
|
searchStr = ast
|
|
|
|
} else {
|
|
|
|
searchStr = JSON.stringify(ast)
|
|
|
|
}
|
|
|
|
const indexStr = `${index}`.padStart(3, '0')
|
|
|
|
const newName = `${name}${indexStr}`
|
|
|
|
const isInString = searchStr.includes(newName)
|
|
|
|
if (!isInString) {
|
|
|
|
return newName
|
|
|
|
}
|
|
|
|
return findUniqueName(searchStr, name, index + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToShow(node: Program, name: string): Program {
|
|
|
|
const _node = { ...node }
|
|
|
|
const dumbyStartend = { start: 0, end: 0 }
|
|
|
|
const showCallIndex = getShowIndex(_node)
|
|
|
|
if (showCallIndex === -1) {
|
|
|
|
const showCall: CallExpression = {
|
|
|
|
type: 'CallExpression',
|
|
|
|
...dumbyStartend,
|
|
|
|
callee: {
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name: 'show',
|
|
|
|
},
|
|
|
|
optional: false,
|
|
|
|
arguments: [
|
|
|
|
{
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
const showExpressionStatement: ExpressionStatement = {
|
|
|
|
type: 'ExpressionStatement',
|
|
|
|
...dumbyStartend,
|
|
|
|
expression: showCall,
|
|
|
|
}
|
|
|
|
_node.body = [..._node.body, showExpressionStatement]
|
|
|
|
return _node
|
|
|
|
}
|
|
|
|
const showCall = { ..._node.body[showCallIndex] } as ExpressionStatement
|
|
|
|
const showCallArgs = (showCall.expression as CallExpression).arguments
|
|
|
|
const newShowCallArgs: Value[] = [
|
|
|
|
...showCallArgs,
|
|
|
|
{
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
const newShowExpression: CallExpression = {
|
|
|
|
type: 'CallExpression',
|
|
|
|
...dumbyStartend,
|
|
|
|
callee: {
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name: 'show',
|
|
|
|
},
|
|
|
|
optional: false,
|
|
|
|
arguments: newShowCallArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
_node.body[showCallIndex] = {
|
|
|
|
...showCall,
|
|
|
|
expression: newShowExpression,
|
|
|
|
}
|
|
|
|
return _node
|
|
|
|
}
|
|
|
|
|
|
|
|
function getShowIndex(node: Program): number {
|
|
|
|
return node.body.findIndex(
|
|
|
|
(statement) =>
|
|
|
|
statement.type === 'ExpressionStatement' &&
|
|
|
|
statement.expression.type === 'CallExpression' &&
|
|
|
|
statement.expression.callee.type === 'Identifier' &&
|
|
|
|
statement.expression.callee.name === 'show'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addLine(
|
|
|
|
node: Program,
|
2022-12-04 15:50:52 +11:00
|
|
|
pathToNode: (string | number)[],
|
2022-11-28 09:37:46 +11:00
|
|
|
to: [number, number]
|
2022-12-04 15:50:52 +11:00
|
|
|
): { modifiedAst: Program; pathToNode: (string | number)[] } {
|
2022-11-28 09:37:46 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const dumbyStartend = { start: 0, end: 0 }
|
2022-12-04 15:50:52 +11:00
|
|
|
const sketchExpression = getNodeFromPath(
|
2022-11-28 19:44:08 +11:00
|
|
|
_node,
|
2022-12-06 05:40:05 +11:00
|
|
|
pathToNode,
|
|
|
|
'SketchExpression'
|
2022-12-04 15:50:52 +11:00
|
|
|
) as SketchExpression
|
2022-11-28 09:37:46 +11:00
|
|
|
const line: ExpressionStatement = {
|
|
|
|
type: 'ExpressionStatement',
|
|
|
|
...dumbyStartend,
|
|
|
|
expression: {
|
|
|
|
type: 'CallExpression',
|
|
|
|
...dumbyStartend,
|
|
|
|
callee: {
|
|
|
|
type: 'Identifier',
|
|
|
|
...dumbyStartend,
|
|
|
|
name: 'lineTo',
|
|
|
|
},
|
|
|
|
optional: false,
|
|
|
|
arguments: [
|
|
|
|
{
|
|
|
|
type: 'Literal',
|
|
|
|
...dumbyStartend,
|
|
|
|
value: to[0],
|
|
|
|
raw: `${to[0]}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'Literal',
|
|
|
|
...dumbyStartend,
|
|
|
|
value: to[1],
|
|
|
|
raw: `${to[1]}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const newBody = [...sketchExpression.body.body, line]
|
2022-12-04 15:50:52 +11:00
|
|
|
sketchExpression.body.body = newBody
|
2022-11-28 09:37:46 +11:00
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
2022-12-04 15:50:52 +11:00
|
|
|
pathToNode,
|
2022-11-28 09:37:46 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:02:21 +11:00
|
|
|
export function changeArguments(
|
|
|
|
node: Program,
|
|
|
|
pathToNode: (string | number)[],
|
|
|
|
args: [number, number]
|
2022-12-23 07:47:22 +11:00
|
|
|
): { modifiedAst: Program; pathToNode: (string | number)[] } {
|
2022-12-07 10:02:21 +11:00
|
|
|
const _node = { ...node }
|
|
|
|
const dumbyStartend = { start: 0, end: 0 }
|
|
|
|
// const thePath = getNodePathFromSourceRange(_node, sourceRange)
|
|
|
|
const callExpression = getNodeFromPath(_node, pathToNode) as CallExpression
|
2022-12-23 07:47:22 +11:00
|
|
|
const newXArg: CallExpression['arguments'][number] =
|
|
|
|
callExpression.arguments[0].type === 'Literal'
|
|
|
|
? {
|
|
|
|
type: 'Literal',
|
|
|
|
...dumbyStartend,
|
|
|
|
value: args[0],
|
|
|
|
raw: `${args[0]}`,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
...callExpression.arguments[0],
|
|
|
|
}
|
|
|
|
const newYArg: CallExpression['arguments'][number] =
|
|
|
|
callExpression.arguments[1].type === 'Literal'
|
|
|
|
? {
|
|
|
|
type: 'Literal',
|
|
|
|
...dumbyStartend,
|
|
|
|
value: args[1],
|
|
|
|
raw: `${args[1]}`,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
...callExpression.arguments[1],
|
|
|
|
}
|
2022-12-07 10:02:21 +11:00
|
|
|
callExpression.arguments = [newXArg, newYArg]
|
|
|
|
return {
|
|
|
|
modifiedAst: _node,
|
|
|
|
pathToNode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:50:52 +11:00
|
|
|
function isCallExpression(tokens: Token[], index: number): number {
|
2022-12-03 22:50:46 +11:00
|
|
|
const currentToken = tokens[index]
|
|
|
|
const veryNextToken = tokens[index + 1] // i.e. no whitespace
|
2022-12-04 15:50:52 +11:00
|
|
|
if (
|
|
|
|
currentToken.type === 'word' &&
|
|
|
|
veryNextToken.type === 'brace' &&
|
|
|
|
veryNextToken.value === '('
|
|
|
|
) {
|
2022-12-03 22:50:46 +11:00
|
|
|
return findClosingBrace(tokens, index + 1)
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:50:52 +11:00
|
|
|
function debuggerr(tokens: Token[], indexes: number[], msg = ''): string {
|
2022-12-03 22:50:46 +11:00
|
|
|
// return ''
|
|
|
|
const sortedIndexes = [...indexes].sort((a, b) => a - b)
|
|
|
|
const min = Math.min(...indexes)
|
|
|
|
const start = Math.min(Math.abs(min - 1), 0)
|
|
|
|
const max = Math.max(...indexes)
|
|
|
|
const end = Math.min(Math.abs(max + 1), tokens.length)
|
|
|
|
const debugTokens = tokens.slice(start, end)
|
|
|
|
const debugIndexes = indexes.map((i) => i - start)
|
|
|
|
const debugStrings: [string, string][] = debugTokens.map((token, index) => {
|
|
|
|
if (debugIndexes.includes(index)) {
|
|
|
|
return [
|
|
|
|
`${token.value.replaceAll('\n', ' ')}`,
|
|
|
|
'^'.padEnd(token.value.length, '_'),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
token.value.replaceAll('\n', ' '),
|
|
|
|
' '.padEnd(token.value.length, ' '),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
let topString = ''
|
|
|
|
let bottomString = ''
|
|
|
|
debugStrings.forEach(([top, bottom]) => {
|
|
|
|
topString += top
|
|
|
|
bottomString += bottom
|
|
|
|
})
|
2022-12-04 15:50:52 +11:00
|
|
|
const debugResult = [
|
|
|
|
`${msg} - debuggerr: ${sortedIndexes}`,
|
|
|
|
topString,
|
|
|
|
bottomString,
|
|
|
|
].join('\n')
|
2022-12-04 08:16:04 +11:00
|
|
|
console.log(debugResult)
|
|
|
|
return debugResult
|
2022-12-03 22:50:46 +11:00
|
|
|
}
|
|
|
|
|
2022-12-06 05:40:05 +11:00
|
|
|
export function getNodeFromPath(
|
|
|
|
node: Program,
|
|
|
|
path: (string | number)[],
|
|
|
|
stopAt: string = ''
|
|
|
|
) {
|
2022-12-04 15:50:52 +11:00
|
|
|
let currentNode = node as any
|
2022-12-06 05:40:05 +11:00
|
|
|
let stopAtNode = null
|
2022-12-04 18:35:32 +11:00
|
|
|
let successfulPaths: (string | number)[] = []
|
2022-12-04 15:50:52 +11:00
|
|
|
for (const pathItem of path) {
|
2022-12-04 18:35:32 +11:00
|
|
|
try {
|
|
|
|
if (typeof currentNode[pathItem] !== 'object')
|
|
|
|
throw new Error('not an object')
|
|
|
|
currentNode = currentNode[pathItem]
|
|
|
|
successfulPaths.push(pathItem)
|
2022-12-06 05:40:05 +11:00
|
|
|
if (currentNode.type === stopAt) {
|
|
|
|
// it will match the deepest node of the type
|
|
|
|
// instead of returning at the first match
|
|
|
|
stopAtNode = currentNode
|
|
|
|
}
|
2022-12-04 18:35:32 +11:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error(
|
|
|
|
`Could not find path ${pathItem} in node ${JSON.stringify(
|
|
|
|
currentNode,
|
|
|
|
null,
|
|
|
|
2
|
|
|
|
)}, successful path was ${successfulPaths}`
|
|
|
|
)
|
|
|
|
}
|
2022-12-04 15:50:52 +11:00
|
|
|
}
|
2022-12-06 05:40:05 +11:00
|
|
|
return stopAtNode || currentNode
|
2022-12-04 15:50:52 +11:00
|
|
|
}
|
2022-12-04 18:35:32 +11:00
|
|
|
|
|
|
|
type Path = (string | number)[]
|
|
|
|
|
|
|
|
export function getNodePathFromSourceRange(
|
|
|
|
node: Program,
|
|
|
|
sourceRange: [number, number],
|
|
|
|
previousPath: Path = []
|
|
|
|
): Path {
|
|
|
|
const [start, end] = sourceRange
|
|
|
|
let path: Path = [...previousPath, 'body']
|
|
|
|
const _node = { ...node }
|
|
|
|
// loop over each statement in body getting the index with a for loop
|
|
|
|
for (
|
|
|
|
let statementIndex = 0;
|
|
|
|
statementIndex < _node.body.length;
|
|
|
|
statementIndex++
|
|
|
|
) {
|
|
|
|
const statement = _node.body[statementIndex]
|
|
|
|
if (statement.start <= start && statement.end >= end) {
|
|
|
|
path.push(statementIndex)
|
|
|
|
if (statement.type === 'ExpressionStatement') {
|
|
|
|
const expression = statement.expression
|
|
|
|
if (expression.start <= start && expression.end >= end) {
|
|
|
|
path.push('expression')
|
|
|
|
if (expression.type === 'CallExpression') {
|
|
|
|
const callee = expression.callee
|
|
|
|
if (callee.start <= start && callee.end >= end) {
|
|
|
|
path.push('callee')
|
|
|
|
if (callee.type === 'Identifier') {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (statement.type === 'VariableDeclaration') {
|
|
|
|
const declarations = statement.declarations
|
|
|
|
|
|
|
|
for (let decIndex = 0; decIndex < declarations.length; decIndex++) {
|
|
|
|
const declaration = declarations[decIndex]
|
|
|
|
|
|
|
|
if (declaration.start <= start && declaration.end >= end) {
|
|
|
|
path.push('declarations')
|
|
|
|
path.push(decIndex)
|
|
|
|
const init = declaration.init
|
|
|
|
if (init.start <= start && init.end >= end) {
|
|
|
|
path.push('init')
|
|
|
|
if (init.type === 'SketchExpression') {
|
|
|
|
const body = init.body
|
|
|
|
if (body.start <= start && body.end >= end) {
|
|
|
|
path.push('body')
|
|
|
|
if (body.type === 'BlockStatement') {
|
|
|
|
path = getNodePathFromSourceRange(body, sourceRange, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (init.type === 'PipeExpression') {
|
|
|
|
const body = init.body
|
|
|
|
for (let pipeIndex = 0; pipeIndex < body.length; pipeIndex++) {
|
|
|
|
const pipe = body[pipeIndex]
|
|
|
|
if (pipe.start <= start && pipe.end >= end) {
|
|
|
|
path.push('body')
|
|
|
|
path.push(pipeIndex)
|
|
|
|
if (pipe.type === 'SketchExpression') {
|
|
|
|
const body = pipe.body
|
|
|
|
if (body.start <= start && body.end >= end) {
|
|
|
|
path.push('body')
|
|
|
|
if (body.type === 'BlockStatement') {
|
|
|
|
path = getNodePathFromSourceRange(
|
|
|
|
body,
|
|
|
|
sourceRange,
|
|
|
|
path
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (init.type === 'CallExpression') {
|
|
|
|
const callee = init.callee
|
|
|
|
if (callee.start <= start && callee.end >= end) {
|
|
|
|
path.push('callee')
|
|
|
|
if (callee.type === 'Identifier') {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|