Skip to content

Commit

Permalink
feat-fix: the stepping slicer does no correctly pass along inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed Aug 28, 2023
1 parent 4110b92 commit cfc5b41
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/benchmark/slicer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class BenchmarkSlicer {

this.decoratedAst = this.commonMeasurements.measure(
'decorate R AST',
() => decorateAst(normalizedAst )
() => decorateAst(normalizedAst)
)

this.dataflow = this.commonMeasurements.measure(
Expand Down
4 changes: 3 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { SteppingSlicer, SteppingSlicerInput } from './slicer'
export { SteppingSlicer } from './slicer'
export * from './steps'
export * from './input'
export * from './output'
4 changes: 2 additions & 2 deletions src/core/intput.ts → src/core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface BaseSteppingSlicerInput<InterestedIn extends SubStepName | undefined>
/** These hooks only make sense if you at least want to normalize the parsed R AST. They can augment the normalization process */
hooks?: DeepPartial<XmlParserHooks>
/** This id generator is only necessary if you want to retrieve a dataflow from the parsed R AST, it determines the id generator to use and if you are unsure, use the {@link deterministicCountingIdGenerator}*/
getId?: IdGenerator<NoInfo>
getId?: IdGenerator<unknown>
/** The slicing criterion is only of interest if you actually want to slice the R code */
criterion?: SlicingCriteria
}
Expand All @@ -34,7 +34,7 @@ interface NormalizeSteppingSlicerInput<InterestedIn extends SubStepName | undefi
}

interface DecorateSteppingSlicerInput<InterestedIn extends SubStepName | undefined> extends NormalizeSteppingSlicerInput<InterestedIn> {
getId: IdGenerator<NoInfo>
getId: IdGenerator<unknown>
}


Expand Down
20 changes: 11 additions & 9 deletions src/core/slicer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
DecoratedAst,
NodeId,
RExpressionList,
DecoratedAst, IdGenerator,
NodeId, RNode,
RParseRequest,
RShell,
TokenMap,
Expand All @@ -19,7 +18,7 @@ import { guard } from '../util/assert'
import { SlicingCriteria } from '../slicing'
import { DataflowGraph } from '../dataflow'
import { DeepPartial } from 'ts-essentials'
import { SteppingSlicerInput } from './intput'
import { SteppingSlicerInput } from './input'
import { StepResults } from './output'

/**
Expand Down Expand Up @@ -91,6 +90,7 @@ export class SteppingSlicer<InterestedIn extends SubStepName> {
private readonly stepOfInterest: InterestedIn
private readonly request: RParseRequest
private readonly hooks?: DeepPartial<XmlParserHooks>
private readonly getId?: IdGenerator<unknown>

private criterion?: SlicingCriteria

Expand All @@ -108,6 +108,7 @@ export class SteppingSlicer<InterestedIn extends SubStepName> {
this.tokenMap = input.tokenMap
this.request = input.request
this.hooks = input.hooks
this.getId = input.getId
this.stepOfInterest = input.stepOfInterest ?? LAST_STEP as InterestedIn
this.criterion = input.criterion
}
Expand Down Expand Up @@ -163,9 +164,9 @@ export class SteppingSlicer<InterestedIn extends SubStepName> {
guard(this.hasNextStep(), 'No more steps to do')

const guardStep = expectedStepName === undefined ?
(name: SubStepName) => name
<K extends SubStepName>(name: K): K => name
:
(name: SubStepName): SubStepName => {
<K extends SubStepName>(name: K): K => {
guard(expectedStepName === name, `Expected step ${expectedStepName} but got ${step}`)
return name
}
Expand All @@ -181,7 +182,7 @@ export class SteppingSlicer<InterestedIn extends SubStepName> {
return { name: step, result: result as Awaited<ReturnType<SubStepProcessor<typeof step>>> }
}

private async doNextStep(guardStep: (name: SubStepName) => SubStepName) {
private async doNextStep(guardStep: <K extends SubStepName>(name: K) => K) {
let step: SubStepName
let result: unknown

Expand All @@ -197,7 +198,8 @@ export class SteppingSlicer<InterestedIn extends SubStepName> {
break
case 2:
step = guardStep('decorate')
result = doSubStep(step, this.results.normalizeAst as RExpressionList)
guard(this.getId !== undefined, 'Cannot decorate ast without an id generator')
result = doSubStep(step, this.results['normalize ast'] as RNode, this.getId)
break
case 3:
step = guardStep('dataflow')
Expand Down Expand Up @@ -251,7 +253,7 @@ export class SteppingSlicer<InterestedIn extends SubStepName> {
while (this.hasNextStep()) {
await this.nextStep()
}
if(switchStage && this.stage === 'once-per-file') {
if(switchStage && !this.reachedWanted && this.stage === 'once-per-file') {
this.switchToSliceStage()
while (this.hasNextStep()) {
await this.nextStep()
Expand Down
2 changes: 1 addition & 1 deletion src/core/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ export type SubStep<name extends SubStepName> = typeof STEPS[name]
export type SubStepProcessor<name extends SubStepName> = SubStep<name>['processor']

export function doSubStep<Name extends SubStepName, Processor extends SubStepProcessor<Name>>(subStep: Name, ...input: Parameters<Processor>): ReturnType<Processor> {
return STEPS[subStep].processor(input as never) as ReturnType<Processor>
return STEPS[subStep].processor(...input as unknown as never[]) as ReturnType<Processor>
}

11 changes: 5 additions & 6 deletions test/helper/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
IdGenerator,
NodeId,
NoInfo,
retrieveAstFromRCode,
RExpressionList,
RNode,
RNodeWithParent, RParseRequest,
Expand All @@ -19,10 +18,9 @@ import {
import { assert } from 'chai'
import { DataflowGraph, diffGraphsToMermaidUrl, graphToMermaidUrl, produceDataFlowGraph } from '../../src/dataflow'
import { reconstructToCode, SlicingCriteria, slicingCriterionToId, staticSlicing } from '../../src/slicing'
import { LocalScope } from '../../src/dataflow/environments/scopes'
import { testRequiresRVersion } from './version'
import { deepMergeObject, MergeableRecord } from '../../src/util/objects'
import { retrieveResultOfStep, SteppingSlicer } from '../../src/core/slicer'
import { SteppingSlicer } from '../../src/core'

let defaultTokenMap: Record<string, string>

Expand Down Expand Up @@ -107,12 +105,13 @@ function requestFromInput(input: `file://${string}` | string): RParseRequest {

export const retrieveAst = async(shell: RShell, input: `file://${string}` | string, hooks?: DeepPartial<XmlParserHooks>): Promise<RExpressionList> => {
const request = requestFromInput(input)
return (await retrieveResultOfStep('normalize ast', {
return (await new SteppingSlicer({
stepOfInterest: 'normalize ast',
shell,
request,
tokenMap: defaultTokenMap,
tokenMap: defaultTokenMap,
hooks
}))['normalize ast']
}).allRemainingSteps())['normalize ast']
}

export interface TestConfiguration extends MergeableRecord {
Expand Down

0 comments on commit cfc5b41

Please sign in to comment.