Skip to content

Commit 7dcd393

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

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

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: 53 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,52 @@ 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);
229+
export * from "./errors";
230+
import * as fs from "fs";
231+
232+
233+
if (require.main === module) {
234+
(async () => {
235+
if (process.argv.length < 3) {
236+
console.error("Usage: npm run start:dev -- <python-file>");
237+
process.exit(1);
238+
}
239+
const options = {};
240+
const context = new Context();
241+
242+
const filePath = process.argv[2];
243+
244+
try {
245+
//await loadModulesFromServer(context, "http://localhost:8022");
246+
247+
const code = fs.readFileSync(filePath, "utf8") + "\n";
248+
console.log(`Parsing Python file: ${filePath}`);
249+
250+
const result = await runInContext(code, context, options);
251+
console.info(result);
252+
console.info((result as Finished).value);
253+
console.info((result as Finished).representation.toString((result as Finished).value));
254+
255+
} catch (e) {
256+
console.error("Error:", e);
257+
}
258+
259+
})();
260+
}

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)