Add walks to determine symbols used by and defined by various expressions

This commit is contained in:
Garrett Mills 2022-04-09 00:58:19 -05:00
parent 83c3c15cba
commit 189439227f
4 changed files with 4041 additions and 10 deletions

3802
pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,8 @@
// This starter template is using Vue 3 <script setup> SFCs // This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup // Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HelloWorld from './components/HelloWorld.vue' import HelloWorld from './components/HelloWorld.vue'
import { MathStatement } from './support/parse'
console.log('math statement', MathStatement)
</script> </script>
<template> <template>
@ -11,7 +13,7 @@ import HelloWorld from './components/HelloWorld.vue'
<div id = "main"> <div id = "main">
<img alt="Vue logo" src="./assets/logo.png" /> <img alt="Vue logo" src="./assets/logo.png" />
<git msg="Hello Vue 3 + TypeScript + Vite" /> <!-- <git msg="Hello Vue 3 + TypeScript + Vite" />-->
</div> </div>

View File

@ -1,4 +1,4 @@
<script setup></script> <script setup lang="ts"></script>
<template> <template>
<div <div

View File

@ -1,7 +1,44 @@
import math from 'mathjs' import * as math from 'mathjs'
import {hasOwnProperty} from '../types'
/** Base class for walks over MathNode trees. */
export abstract class MathNodeWalk<TReturn> { export abstract class MathNodeWalk<TReturn> {
walk(node: math.MathNode): TReturn {
if ( math.isAccessorNode(node) ) {
return this.walkAccessorNode(node)
} else if ( math.isArrayNode(node) ) {
return this.walkArrayNode(node)
} else if ( math.isAssignmentNode(node) ) {
return this.walkAssignmentNode(node)
} else if ( math.isBlockNode(node) ) {
return this.walkBlockNode(node)
} else if ( math.isConditionalNode(node) ) {
return this.walkConditionalNode(node)
} else if ( math.isConstantNode(node) ) {
return this.walkConstantNode(node)
} else if ( math.isFunctionAssignmentNode(node) ) {
return this.walkFunctionAssignmentNode(node)
} else if ( math.isFunctionNode(node) ) {
return this.walkFunctionNode(node)
} else if ( math.isIndexNode(node) ) {
return this.walkIndexNode(node)
} else if ( math.isObjectNode(node) ) {
return this.walkObjectNode(node)
} else if ( math.isOperatorNode(node) ) {
return this.walkOperatorNode(node)
} else if ( math.isParenthesisNode(node) ) {
return this.walkParenthesisNode(node)
} else if ( math.isRangeNode(node) ) {
return this.walkRangeNode(node)
} else if ( (node as unknown as any).isRelationalNode ) {
return this.walkRelationalNode(node as unknown as any)
} else if ( math.isSymbolNode(node) ) {
return this.walkSymbolNode(node)
}
throw new TypeError('Invalid MathNode: ' + node)
}
abstract walkAccessorNode(node: math.AccessorNode): TReturn abstract walkAccessorNode(node: math.AccessorNode): TReturn
abstract walkArrayNode(node: math.ArrayNode): TReturn abstract walkArrayNode(node: math.ArrayNode): TReturn
@ -33,15 +70,205 @@ export abstract class MathNodeWalk<TReturn> {
abstract walkSymbolNode(node: math.SymbolNode): TReturn abstract walkSymbolNode(node: math.SymbolNode): TReturn
} }
export class SymbolWalk extends MathNodeWalk<math.SymbolNode[]> {
walkAccessorNode(node: math.AccessorNode): math.SymbolNode[] {
return [
...this.walk(node.object),
...this.walk(node.index),
]
}
walkArrayNode(node: math.ArrayNode): math.SymbolNode[] {
return node.items
.map(x => this.walk(x))
.reduce((carry, current) => current.concat(carry), [])
}
walkAssignmentNode(node: math.AssignmentNode): math.SymbolNode[] {
return [
...this.walk(node.object),
...this.walk(node.value),
...(node.index ? this.walk(node.index) : []),
]
}
walkBlockNode(node: math.BlockNode): math.SymbolNode[] {
return node.blocks
.map(x => x.node)
.map(x => this.walk(x))
.reduce((carry, current) => current.concat(carry), [])
}
walkConditionalNode(node: math.ConditionalNode): math.SymbolNode[] {
return [
...this.walk(node.condition),
...this.walk(node.trueExpr),
...this.walk(node.falseExpr),
]
}
walkConstantNode(): math.SymbolNode[] {
return []
}
walkFunctionAssignmentNode(node: math.FunctionAssignmentNode): math.SymbolNode[] {
return this.walk(node.expr)
}
walkFunctionNode(node: math.FunctionNode): math.SymbolNode[] {
return [
...this.walk(node.fn),
...node.args
.map(x => this.walk(x))
.reduce((carry, current) => current.concat(carry), []),
]
}
walkIndexNode(node: math.IndexNode): math.SymbolNode[] {
return node.dimensions
.map(x => this.walk(x))
.reduce((carry, current) => current.concat(carry), [])
}
walkObjectNode(node: math.ObjectNode): math.SymbolNode[] {
return Object.values(node.properties)
.map(x => this.walk(x))
.reduce((carry, current) => current.concat(carry), [])
}
walkOperatorNode(node: math.OperatorNode): math.SymbolNode[] {
return node.args
.map(x => this.walk(x))
.reduce((carry, current) => carry.concat(current), [])
}
walkParenthesisNode(node: math.ParenthesisNode): math.SymbolNode[] {
return this.walk(node.content)
}
walkRangeNode(node: math.RangeNode): math.SymbolNode[] {
return [
...this.walk(node.start),
...this.walk(node.end),
...(node.step ? this.walk(node.step) : []),
]
}
walkRelationalNode(node: math.RelationalNode): math.SymbolNode[] {
return node.params
.map(x => this.walk(x))
.reduce((carry, current) => carry.concat(current), [])
}
walkSymbolNode(node: math.SymbolNode): math.SymbolNode[] {
return [node]
}
}
export class RValSymbolWalk extends SymbolWalk {
walkAssignmentNode(node: math.AssignmentNode): math.SymbolNode[] {
return this.walk(node.value)
}
walkFunctionNode(node: math.FunctionNode): math.SymbolNode[] {
return [
...this.walk(node.fn), // FIXME should this be removed? Not sure if this is rval or lval
...node.args
.map(x => this.walk(x))
.reduce((carry, current) => current.concat(carry), []),
]
}
}
export class LValSymbolWalk extends SymbolWalk {
walkAccessorNode(): math.SymbolNode[] {
return []
}
walkArrayNode(): math.SymbolNode[] {
return []
}
walkAssignmentNode(node: math.AssignmentNode): math.SymbolNode[] {
if ( math.isSymbolNode(node.object) ) {
return super.walkSymbolNode(node.object)
}
return super.walkAccessorNode(node.object)
}
walkBlockNode(node: math.BlockNode): math.SymbolNode[] {
return node.blocks
.map(x => this.walk(x.node))
.reduce((carry, current) => current.concat(carry), [])
}
walkConditionalNode(): math.SymbolNode[] {
return []
}
walkConstantNode(): math.SymbolNode[] {
return []
}
walkFunctionAssignmentNode(): math.SymbolNode[] {
return []
}
walkFunctionNode(): math.SymbolNode[] {
return []
}
walkIndexNode(): math.SymbolNode[] {
return []
}
walkObjectNode(): math.SymbolNode[] {
return []
}
walkOperatorNode(): math.SymbolNode[] {
return []
}
walkParenthesisNode(node: math.ParenthesisNode): math.SymbolNode[] {
return this.walk(node.content)
}
walkRangeNode(): math.SymbolNode[] {
return []
}
walkRelationalNode(): math.SymbolNode[] {
return []
}
walkSymbolNode(): math.SymbolNode[] {
return []
}
}
export class MathStatement { export class MathStatement {
constructor( constructor(
public readonly raw: string, public readonly raw: string,
) {} ) {}
// parse(): MathNode { parse(): math.MathNode {
// return parse(this.raw) return math.parse(this.raw)
// }
} }
export class MathExpression extends MathStatement {} symbols(): math.SymbolNode[] {
return (new SymbolWalk()).walk(this.parse())
}
defines(): math.SymbolNode[] {
return (new LValSymbolWalk()).walk(this.parse())
}
uses(): math.SymbolNode[] {
return (new RValSymbolWalk()).walk(this.parse())
}
}