• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / js-slang / 11929582114

20 Nov 2024 08:33AM UTC coverage: 81.654% (+0.04%) from 81.61%
11929582114

Pull #1728

github

web-flow
Merge c3f0269a5 into 665233634
Pull Request #1728: Implement the CSET machine, along with macros

3654 of 4864 branches covered (75.12%)

Branch coverage included in aggregate %.

491 of 596 new or added lines in 11 files covered. (82.38%)

1 existing line in 1 file now uncovered.

11474 of 13663 relevant lines covered (83.98%)

143610.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/cse-machine/continuations.ts
1
import * as es from 'estree'
2

3
import { Context, Environment } from '../types'
4
import { Control, Stash, Transformers } from './interpreter'
5
import { uniqueId } from './utils'
79✔
6

7
/**
8
 * A dummy function used to detect for the apply function object.
9
 * If the interpreter sees this specific function, it applies the function
10
 * with the given arguments to apply.
11
 *
12
 * We need this to be a metaprocedure so that it can properly handle
13
 * the arguments passed to it, even if they are continuations.
14
 */
15
export class Apply extends Function {
79✔
16
  private static instance: Apply = new Apply()
79✔
17

18
  private constructor() {
19
    super()
79✔
20
  }
21

22
  public static get(): Apply {
23
    return Apply.instance
79✔
24
  }
25

26
  public toString(): string {
NEW
27
    return 'apply'
×
28
  }
29
}
30

31
export const apply = Apply.get()
79✔
32

33
export function isApply(value: any): boolean {
79✔
NEW
34
  return value === apply
×
35
}
36

37
/**
38
 * A dummy function used to detect for the call/cc function object.
39
 * If the interpreter sees this specific function, a continuation at the current
40
 * point of evaluation is executed instead of a regular function call.
41
 */
42
export class Call_cc extends Function {
79✔
43
  private static instance: Call_cc = new Call_cc()
79✔
44

45
  private constructor() {
46
    super()
79✔
47
  }
48

49
  public static get(): Call_cc {
50
    return Call_cc.instance
83✔
51
  }
52

53
  public toString(): string {
54
    return 'call/cc'
1✔
55
  }
56
}
57

58
export const call_with_current_continuation = Call_cc.get()
79✔
59

60
export function isCallWithCurrentContinuation(value: any): boolean {
79✔
61
  return value === call_with_current_continuation
656,656✔
62
}
63

64
/**
65
 * An object representing a continuation of the CSE machine.
66
 * When instantiated, it copies the control stack, and
67
 * current environment at the point of capture.
68
 *
69
 * Continuations and functions are treated as the same by
70
 * the typechecker so that they can be first-class values.
71
 */
72
export class Continuation extends Function {
79✔
73
  private control: Control
74
  private stash: Stash
75
  private env: Environment[]
76
  private transformers: Transformers
77

78
  /** Unique ID defined for continuation */
79
  public readonly id: string
80

81
  constructor(
82
    context: Context,
83
    control: Control,
84
    stash: Stash,
85
    env: Environment[],
86
    transformers: Transformers
87
  ) {
88
    super()
7✔
89
    this.control = control.copy()
7✔
90
    this.stash = stash.copy()
7✔
91
    this.env = [...env]
7✔
92
    this.transformers = transformers
7✔
93
    this.id = uniqueId(context)
7✔
94
  }
95

96
  // As the continuation needs to be immutable (we can call it several times)
97
  // we need to copy its elements whenever we access them
98
  public getControl(): Control {
99
    return this.control.copy()
3✔
100
  }
101

102
  public getStash(): Stash {
103
    return this.stash.copy()
3✔
104
  }
105

106
  public getEnv(): Environment[] {
107
    return [...this.env]
3✔
108
  }
109

110
  public getTransformers(): Transformers {
111
    return this.transformers
3✔
112
  }
113

114
  public toString(): string {
115
    return 'continuation'
1✔
116
  }
117

118
  public equals(other: Continuation): boolean {
NEW
119
    return this === other
×
120
  }
121
}
122

123
/**
124
 * Provides an adequate representation of what calling
125
 * call/cc or continuations looks like, to give to the
126
 * APPLICATION instruction.
127
 */
128
export function makeDummyContCallExpression(callee: string, argument: string): es.CallExpression {
79✔
129
  return {
6✔
130
    type: 'CallExpression',
131
    optional: false,
132
    callee: {
133
      type: 'Identifier',
134
      name: callee
135
    },
136
    arguments: [
137
      {
138
        type: 'Identifier',
139
        name: argument
140
      }
141
    ]
142
  }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc