generated from lambda-feedback/evaluation-function-boilerplate-wolfram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.m
More file actions
102 lines (82 loc) · 3.44 KB
/
preview.m
File metadata and controls
102 lines (82 loc) · 3.44 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(* ::Package:: *)
(* Wolfram Language Package *)
(* Created by the Wolfram Language Plugin for IntelliJ, see http://wlplugin.halirutan.de/ *)
(* :Title: preview *)
(* :Context: preview` *)
(* :Author: marcus *)
(* :Date: 2025-09-26 *)
(* :Package Version: 0.1 *)
(* :Mathematica Version: 14.0 *)
(* :Copyright: (c) 2025 Lambda Feedback *)
(* :Keywords: *)
(* :Discussion: *)
(* For new style packages see: https://mathematica.stackexchange.com/a/176489) *)
(* Declare package context *)
BeginPackage["preview`"];
PreviewFunction[response_] := Module[{latexString, wolframString, parsedResponse},
Print["Running Preview Function"];
Print["Preview Input:", response];
parsedResponse = SafeToExpression[response];
If[StringQ[parsedResponse] && StringStartsQ[parsedResponse, "Error:"],
Return[
<|
"error" -> <|
"message" -> parsedResponse
|>
|>
]
];
latexString = ToString[parsedResponse/.activeFunctionRules, TeXForm];
wolframString = ToString[parsedResponse/.activeFunctionRules, InputForm];
<|
"latex" -> latexString,
"sympy" -> wolframString
|>
];
activeFunctionRules = {
sin -> Sin, cos -> Cos, tan -> Tan, sec -> Sec, Cosec -> Csc, csc -> Csc, cosec -> Csc, cot -> Cot,
arcsin -> ArcSin, asin -> ArcSin, arccos -> ArcCos, acos -> ArcCos, arctan -> ArcTan, atan -> ArcTan,
arcsec -> ArcSec, asec -> ArcSec, ArcCosec -> ArcCsc, arccsc -> ArcCsc, acsc -> ArcCsc, acosec -> ArcCsc,
arccot -> ArcCot,acot -> ArcCot,
sinh -> Sinh, cosh -> Cosh, tanh -> Tanh, sech -> Sech, Cosech -> Csch, csch -> Csch, cosech -> Csch, coth -> Coth,
arcsinh -> ArcSinh, asinh -> ArcSinh, arccosh -> ArcCosh, acosh -> ArcCosh, arctanh -> ArcTanh, atanh -> ArcTanh,
arcsech -> ArcSech, asech -> ArcSech,
ArcCsch -> ArcCsch, ArcCosech -> ArcCsch, arccsch->ArcCsch, acsch -> ArcCsch, acosech -> ArcCsch,
arccoth -> ArcCoth, acoth -> ArcCoth,
exp -> Exp, log -> Log, ln -> Log};
Begin["`Private`"];
SafeToExpression[str_String] :=
Module[{expr, result},
(* First check for obviously dangerous patterns in the raw string *)
If[StringContainsQ[str,
RegularExpression["\\b(Set|SetDelayed|Module|Block|Function|With|Do|For|While|RunProcess|Import|Export|DeleteFile|CreateFile|Get|Put|Install|Uninstall)\\b"]],
Return["Error: Expression contains unsafe constructs"]
];
(* Try to parse the expression safely *)
result = Quiet @ Check[
ToExpression[StandardizeEquation[str], TraditionalForm, Hold],
Return["Error: Failed to parse expression"]
];
(* If parsing succeeded, check the parsed structure *)
If[MatchQ[result, Hold[_]],
expr = First[result];
(* Check for unsafe constructs in the parsed expression *)
If[!FreeQ[expr,
Alternatives[
Set, SetDelayed, Module, Block, Function, With,
Do, For, While,
RunProcess, Import, Export, DeleteFile, CreateFile,
Get, Put, Install, Uninstall
]],
"Error: Expression contains unsafe constructs",
expr (* safe expression *)
],
"Error: Unexpected parsing result"
]
]
(*StandardizeEquation: a function that automatically converts all instances
of the equals sign in a string to the repeated equals sign, so that anything WL
would parse as an assignment gets parsed instead as an equation*)
StandardizeEquation[str_String]:=FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]]
End[];
EndPackage[];