Repository navigation
Expand file tree
/
Copy pathconverter.js
More file actions
81 lines (74 loc) · 3.33 KB
/
Copy pathconverter.js
File metadata and controls
81 lines (74 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const espree = require("espree");
const estraverse = require("estraverse");
function translateJSExpressionToPython(node) {
switch (node.type) {
case "Literal":
return JSON.stringify(node.value);
case "Identifier":
return node.name;
case "BinaryExpression":
return `(${translateJSExpressionToPython(node.left)} ${node.operator} ${translateJSExpressionToPython(node.right)})`;
case "CallExpression":
if (
node.callee.type === "MemberExpression" &&
node.callee.object.name === "console" &&
node.callee.property.name === "log"
) {
// Convert console.log to Python print statement
const args = node.arguments.map((arg) => translateJSExpressionToPython(arg));
return `print(${args.join(", ")})`;
} else if (
node.callee.type === "MemberExpression" &&
node.callee.object.name === "console" &&
node.callee.property.name === "error"
) {
// Convert console.error to Python print statement to stderr
const args = node.arguments.map((arg) => translateJSExpressionToPython(arg));
return `print(${args.join(", ")}, file=sys.stderr)`;
} else {
// Handle other function calls
const callee = translateJSExpressionToPython(node.callee);
const args = node.arguments.map((arg) => translateJSExpressionToPython(arg));
return `${callee}(${args.join(", ")})`;
}
case "MemberExpression":
const object = translateJSExpressionToPython(node.object);
const property = translateJSExpressionToPython(node.property);
return `${object}.${property}`;
case "TemplateLiteral":
// Handle template literals by joining the cooked values
return `"${node.quasis.map((quasi) => quasi.value.cooked).join("")}"`;
case "AssignmentExpression":
const left = translateJSExpressionToPython(node.left);
const operator = node.operator;
const right = translateJSExpressionToPython(node.right);
return `${left} ${operator} ${right}`;
case "ThisExpression":
// Omit or replace this as needed
return "";
case "FunctionDeclaration":
case "FunctionExpression":
const functionName = node.id ? node.id.name : "";
const params = node.params.map(param => param.name).join(", ");
const functionBody = translateJSExpressionToPython(node.body);
return `def ${functionName}(${params}):\n${functionBody}`;
default:
throw new Error(`Unsupported JavaScript expression: ${node.type}`);
}
}
function translateJSToPython(jsCode) {
const jsAST = espree.parse(jsCode, {
ecmaVersion: 2020, // Adjust this as needed
});
let pythonCode = "";
estraverse.traverse(jsAST, {
enter(node) {
if (node.type === "ExpressionStatement") {
const translated = translateJSExpressionToPython(node.expression);
pythonCode += translated + '\n'; // Append a line break
}
},
});
return pythonCode;
}
module.exports = { translateJSToPython };

