Skip to content

Commit 5a3f1ac

Browse files
committed
Setup new execution path for Python AST [Refs #51][1]
* Updated index.ts with runPyAST (Python AST -> CSE) * py_interpreter.ts in CSE (to replace interpreter.ts in the future) * PyRunCSEMachine in pyRunner.ts * added local.test.ts to gitignore * naming convention for files/functions to replace old logic will start with "Py" -- to be renamed after full migration
1 parent 7672054 commit 5a3f1ac

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,7 @@ dist
107107
.idea
108108

109109
# TypeScript build files
110-
build/
110+
build/
111+
112+
# Ignore personal test files
113+
*.local.test.ts

src/cse-machine/py_interpreter.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Context } from "./context";
2+
import { CSEBreak, Representation, Result, Finished } from "../types";
3+
import { StmtNS } from "../ast-types";
4+
import { Value, ErrorValue } from "./stash";
5+
6+
type Stmt = StmtNS.Stmt;
7+
8+
export function PyCSEResultPromise(context: Context, value: Value): Promise<Result> {
9+
return new Promise((resolve, reject) => {
10+
if (value instanceof CSEBreak) {
11+
resolve({ status: 'suspended-cse-eval', context });
12+
} else if (value.type === 'error') {
13+
const errorValue = value as ErrorValue;
14+
const representation = new Representation(errorValue.message);
15+
resolve({ status: 'finished', context, value, representation } as Finished);
16+
} else {
17+
const representation = new Representation(value);
18+
resolve({ status: 'finished', context, value, representation } as Finished);
19+
}
20+
});
21+
}
22+
23+
export function PyEvaluate(code: string, program: Stmt, context: Context): Promise<Result> {
24+
// dummy for now, just to test getting AST from parser
25+
const dummyValue = { type: 'NoneType', value: undefined };
26+
return PyCSEResultPromise(context, dummyValue);
27+
}

src/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ import { runCSEMachine } from "./runner/pyRunner";
141141
import { initialise } from "./conductor/runner/util/initialise";
142142
import { PyEvaluator } from "./conductor/runner/types/PyEvaluator";
143143
export * from './errors';
144+
import { PyRunCSEMachine } from "./runner/pyRunner";
145+
import { StmtNS } from "./ast-types";
146+
147+
type Stmt = StmtNS.Stmt;
144148

145149
export function parsePythonToEstreeAst(code: string,
146150
variant: number = 1,
@@ -205,4 +209,20 @@ export async function runInContext(
205209
return result;
206210
}
207211

208-
const {runnerPlugin, conduit} = initialise(PyEvaluator);
212+
213+
214+
export async function runPyAST(
215+
code: string,
216+
context: Context,
217+
options: RecursivePartial<IOptions> = {}
218+
): Promise<Result> {
219+
const script = code + "\n";
220+
const tokenizer = new Tokenizer(script);
221+
const tokens = tokenizer.scanEverything();
222+
const pyParser = new Parser(script, tokens);
223+
const ast = pyParser.parse();
224+
const result = PyRunCSEMachine(code, ast, context, options);
225+
return result;
226+
};
227+
228+
const {runnerPlugin, conduit} = initialise(PyEvaluator);

src/runner/pyRunner.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ import { Context } from "../cse-machine/context"
33
import { CSEResultPromise, evaluate } from "../cse-machine/interpreter"
44
import { RecursivePartial, Result } from "../types"
55
import * as es from 'estree'
6+
import { PyEvaluate } from "../cse-machine/py_interpreter"
7+
import { StmtNS } from "../ast-types";
68

79
export function runCSEMachine(code: string, program: es.Program, context: Context, options: RecursivePartial<IOptions> = {}): Promise<Result> {
810
const result = evaluate(code, program, context, options);
911
return CSEResultPromise(context, result);
10-
}
12+
}
13+
14+
type Stmt = StmtNS.Stmt;
15+
16+
export function PyRunCSEMachine(code: string, program: Stmt, context: Context, options: RecursivePartial<IOptions> = {}): Promise<Result> {
17+
return PyEvaluate(code, program, context);
18+
}

0 commit comments

Comments
 (0)