Skip to content

Commit f2acc94

Browse files
committed
feat:Implemented wrapping and unwrapping of symbolic subs.
1 parent a657731 commit f2acc94

1 file changed

Lines changed: 46 additions & 10 deletions

File tree

lib/CortexJS.rakumod

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use v6.d;
22
unit module CortexJS;
33

44
use CortexJS::ComputeEngine;
5+
use LaTeX::Grammar;
56

67
our sub resources($key) {
78
%?RESOURCES{$key}
@@ -55,44 +56,44 @@ sub to-latex($expr,
5556
# Free symbolic functions
5657
#==========================================================
5758

58-
sub simplify($expr) is export {
59+
our sub simplify($expr) is export {
5960
start-ce() without $ce;
6061
return $ce.simplify($expr);
6162
}
6263

63-
sub assign($id, $expr) is export {
64+
our sub assign($id, $expr) is export {
6465
start-ce() without $ce;
6566
return $ce.simplify(:$id, $expr);
6667
}
6768

68-
sub evaluate($expr) is export {
69+
our sub evaluate($expr) is export {
6970
start-ce() without $ce;
7071
return $ce.evaluate($expr);
7172
}
7273

73-
sub N($expr) is export {
74+
our sub N($expr) is export {
7475
start-ce() without $ce;
7576
return $ce.N($expr);
7677
}
7778

78-
sub expand($expr) is export {
79+
our sub expand($expr) is export {
7980
start-ce() without $ce;
8081
return $ce.expand($expr);
8182
}
8283

83-
sub expandAll($expr) is export {
84+
our sub expandAll($expr) is export {
8485
start-ce() without $ce;
8586
return $ce.expandAll($expr);
8687
}
8788

8889
our &expand-all is export = &expandAll;
8990

90-
sub factor($expr) is export {
91+
our sub factor($expr) is export {
9192
start-ce() without $ce;
9293
return $ce.factor($expr);
9394
}
9495

95-
proto sub solve($expr, |) is export {*}
96+
our proto sub solve($expr, |) is export {*}
9697

9798
multi sub solve($expr, $vars) {
9899
return solve($expr, :$vars);
@@ -108,7 +109,7 @@ multi sub solve($expr) {
108109
return $ce.solve($expr);
109110
}
110111

111-
sub cortex-js-call($func, $expr) is export {
112+
our sub cortex-js-call($func, $expr) is export {
112113
start-ce() without $ce;
113114
return $ce.call($func, $expr);
114115
}
@@ -117,7 +118,42 @@ sub cortex-js-call($func, $expr) is export {
117118
# Wrappers
118119
#==========================================================
119120

120-
# TBD...
121+
# Using LaTeX::Grammar function &latex-parse to detect LaTeX input
122+
my &is-latex-spec = { so latex-parse($_) }
123+
124+
our @wrappers;
125+
126+
our sub wrap-symbolic-subs() {
127+
return if @wrappers;
128+
129+
@wrappers = [&simplify, &evaluate, &N, &expand, &expand-all, &expandAll, &factor].map({
130+
$_.wrap(-> $expr {
131+
my $is-latex = &is-latex-spec($expr);
132+
my $expr2 = $is-latex ?? parse-latex($expr) !! $expr;
133+
134+
my $res = callwith($expr2);
135+
136+
$is-latex ?? to-latex($res) !! $res;
137+
})
138+
});
139+
140+
@wrappers.push(
141+
&solve.wrap(-> $expr, |c {
142+
my $is-latex = &is-latex-spec($expr);
143+
my $expr2 = $is-latex ?? parse-latex($expr) !! $expr;
144+
145+
my $res = callwith($expr2, |c);
146+
147+
$is-latex ?? to-latex($res) !! $res;
148+
})
149+
)
150+
}
151+
152+
our sub unwrap-symbolic-subs() {
153+
return unless @wrappers;
154+
([&simplify, &evaluate, &N, &expand, &expand-all, &expandAll, &factor, &solve] Z @wrappers).map({ $_.head.unwrap($_.tail) });
155+
@wrappers = Empty
156+
}
121157

122158
#==========================================================
123159
# Cleanup

0 commit comments

Comments
 (0)