Skip to content

Commit f26c68c

Browse files
WangYuyang1013Wangdahai
andauthored
Integrate Conductor Interface for py-slang Runner (#56)
* conductor interface * acknowledgement * acknowledgement --------- Co-authored-by: Wangdahai <wangdahai@WangdahaideMacBook-Air.local>
1 parent bc6a7fb commit f26c68c

File tree

125 files changed

+2580
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+2580
-5
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ The AST types need to be regenerated after changing
3838
the AST type definitions in `generate-ast.ts`.
3939
```shell
4040
npm run regen
41-
```
41+
```
42+
43+
## Acknowledgements
44+
45+
This project adapts the `Conductor Interface` from [source-academy/conductor](https://github.com/source-academy/conductor), which is part of the Source Academy ecosystem.
46+
47+
Specifically, all files under the following folders are derived from the conductor repository:
48+
49+
- `src/conductor/`
50+
- `src/common/`
51+
- `src/conduit/`
52+
53+
All credits go to the original authors of the Source Academy Conductor Interface.

src/common/Constant.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
export const enum Constant {
6+
PROTOCOL_VERSION = 0,
7+
PROTOCOL_MIN_VERSION = 0,
8+
}

src/common/ds/MessageQueue.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
import { Queue } from "./Queue";
6+
7+
export class MessageQueue<T> {
8+
private readonly __inputQueue: Queue<T> = new Queue();
9+
private readonly __promiseQueue: Queue<Function> = new Queue();
10+
11+
push(item: T) {
12+
if (this.__promiseQueue.length !== 0) this.__promiseQueue.pop()(item);
13+
else this.__inputQueue.push(item);
14+
}
15+
16+
async pop(): Promise<T> {
17+
if (this.__inputQueue.length !== 0) return this.__inputQueue.pop();
18+
return new Promise((resolve, _reject) => {
19+
this.__promiseQueue.push(resolve);
20+
});
21+
}
22+
23+
tryPop(): T | undefined {
24+
if (this.__inputQueue.length !== 0) return this.__inputQueue.pop();
25+
return undefined;
26+
}
27+
28+
constructor() {
29+
this.push = this.push.bind(this);
30+
}
31+
}

src/common/ds/Queue.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
/**
6+
* A stack-based queue implementation.
7+
* `push` and `pop` run in amortized constant time.
8+
*/
9+
export class Queue<T> {
10+
/** The output stack. */
11+
private __s1: T[] = [];
12+
/** The input stack. */
13+
private __s2: T[] = [];
14+
15+
/**
16+
* Adds an item to the queue.
17+
* @param item The item to be added to the queue.
18+
*/
19+
push(item: T) {
20+
this.__s2.push(item);
21+
}
22+
23+
/**
24+
* Removes an item from the queue.
25+
* @returns The item removed from the queue.
26+
* @throws If the queue is empty.
27+
*/
28+
pop(): T {
29+
if (this.__s1.length === 0) {
30+
if (this.__s2.length === 0) throw new Error("queue is empty");
31+
let temp = this.__s1;
32+
this.__s1 = this.__s2.reverse();
33+
this.__s2 = temp;
34+
}
35+
return this.__s1.pop()!; // as the length is nonzero
36+
}
37+
38+
/**
39+
* The length of the queue.
40+
*/
41+
get length() {
42+
return this.__s1.length + this.__s2.length;
43+
}
44+
45+
/**
46+
* Makes a copy of the queue.
47+
* @returns A copy of the queue.
48+
*/
49+
clone(): Queue<T> {
50+
const newQueue = new Queue<T>();
51+
newQueue.__s1 = [...this.__s1];
52+
newQueue.__s2 = [...this.__s2];
53+
return newQueue;
54+
}
55+
}

src/common/ds/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
export { MessageQueue } from "./MessageQueue";
6+
export { Queue } from "./Queue";

src/common/errors/ConductorError.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
import { ErrorType } from "./ErrorType";
6+
7+
/**
8+
* Generic Conductor Error.
9+
*/
10+
export class ConductorError extends Error {
11+
override name = "ConductorError";
12+
readonly errorType: ErrorType | string = ErrorType.UNKNOWN;
13+
14+
constructor(message: string) {
15+
super(message);
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
import { ConductorError } from "./ConductorError";
6+
import { ErrorType } from "./ErrorType";
7+
8+
/**
9+
* Conductor internal error, probably caused by developer oversight.
10+
*/
11+
export class ConductorInternalError extends ConductorError {
12+
override name = "ConductorInternalError";
13+
override readonly errorType: ErrorType | string = ErrorType.INTERNAL;
14+
15+
constructor(message: string) {
16+
super(message);
17+
}
18+
}

src/common/errors/ErrorType.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
export const enum ErrorType {
6+
UNKNOWN = "__unknown",
7+
INTERNAL = "__internal",
8+
EVALUATOR = "__evaluator",
9+
EVALUATOR_SYNTAX = "__evaluator_syntax",
10+
EVALUATOR_TYPE = "__evaluator_type",
11+
EVALUATOR_RUNTIME = "__evaluator_runtime",
12+
}

src/common/errors/EvaluatorError.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
import { ConductorError } from "./ConductorError";
6+
import { ErrorType } from "./ErrorType";
7+
8+
/**
9+
* Generic evaluation error, caused by a problem in user code.
10+
*/
11+
export class EvaluatorError extends ConductorError {
12+
override name = "EvaluatorError";
13+
override readonly errorType: ErrorType | string = ErrorType.EVALUATOR;
14+
15+
readonly rawMessage: string;
16+
readonly line?: number;
17+
readonly column?: number;
18+
readonly fileName?: string
19+
20+
constructor(message: string, line?: number, column?: number, fileName?: string) {
21+
const location = line !== undefined
22+
? `${fileName ? fileName + ":" : ""}${line}${column !== undefined ? ":" + column : ""}: `
23+
: "";
24+
super(`${location}${message}`);
25+
this.rawMessage = message;
26+
this.line = line;
27+
this.column = column;
28+
this.fileName = fileName
29+
}
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file is adapted from:
2+
// https://github.com/source-academy/conductor
3+
// Original author(s): Source Academy Team
4+
5+
import { ErrorType } from "./ErrorType";
6+
import { EvaluatorError } from "./EvaluatorError";
7+
8+
/**
9+
* Evaluator runtime error - some problem occurred while running the user code.
10+
*/
11+
export class EvaluatorRuntimeError extends EvaluatorError {
12+
override name = "EvaluatorRuntimeError";
13+
override readonly errorType: ErrorType | string = ErrorType.EVALUATOR_RUNTIME;
14+
}

0 commit comments

Comments
 (0)