Add math stdlib with formulas/plots and polish the docs view.
Ship Num/Formula runtime, bilingual math libs, SVG plots with axes/grid, and view UX for collapsible folders plus centered plots. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a53ebb65ee
commit
dcd0cdb8f7
40 files changed
+3553
-276
No files matched your search
+243
-3
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::host::{fs, json, net, sys, time, HostContext};
|
||||
use crate::host::{fs, json, math, net, sys, time, HostContext};
|
||||
use crate::value::Value;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -33,6 +33,43 @@ pub enum HostFn {
|
||||
HttpGet = 23,
|
||||
HttpPost = 24,
|
||||
UrlEncode = 25,
|
||||
// math
|
||||
Pi = 26,
|
||||
EConst = 27,
|
||||
Num = 28,
|
||||
MathAdd = 29,
|
||||
MathSub = 30,
|
||||
MathMul = 31,
|
||||
MathDiv = 32,
|
||||
MathPow = 33,
|
||||
MathNeg = 34,
|
||||
MathSin = 35,
|
||||
MathCos = 36,
|
||||
MathTan = 37,
|
||||
MathAsin = 38,
|
||||
MathAcos = 39,
|
||||
MathAtan = 40,
|
||||
MathSqrt = 41,
|
||||
MathAbs = 42,
|
||||
MathLn = 43,
|
||||
MathExp = 44,
|
||||
MathFloor = 45,
|
||||
MathCeil = 46,
|
||||
MathMin = 47,
|
||||
MathMax = 48,
|
||||
Random = 49,
|
||||
RandomInt = 50,
|
||||
Seed = 51,
|
||||
Formula = 52,
|
||||
Simplify = 53,
|
||||
Expand = 54,
|
||||
Diff = 55,
|
||||
Subs = 56,
|
||||
Eval = 57,
|
||||
Solve = 58,
|
||||
Plot = 59,
|
||||
PlotPoints = 60,
|
||||
PlotConic = 61,
|
||||
}
|
||||
|
||||
impl HostFn {
|
||||
@@ -63,6 +100,42 @@ impl HostFn {
|
||||
23 => Self::HttpGet,
|
||||
24 => Self::HttpPost,
|
||||
25 => Self::UrlEncode,
|
||||
26 => Self::Pi,
|
||||
27 => Self::EConst,
|
||||
28 => Self::Num,
|
||||
29 => Self::MathAdd,
|
||||
30 => Self::MathSub,
|
||||
31 => Self::MathMul,
|
||||
32 => Self::MathDiv,
|
||||
33 => Self::MathPow,
|
||||
34 => Self::MathNeg,
|
||||
35 => Self::MathSin,
|
||||
36 => Self::MathCos,
|
||||
37 => Self::MathTan,
|
||||
38 => Self::MathAsin,
|
||||
39 => Self::MathAcos,
|
||||
40 => Self::MathAtan,
|
||||
41 => Self::MathSqrt,
|
||||
42 => Self::MathAbs,
|
||||
43 => Self::MathLn,
|
||||
44 => Self::MathExp,
|
||||
45 => Self::MathFloor,
|
||||
46 => Self::MathCeil,
|
||||
47 => Self::MathMin,
|
||||
48 => Self::MathMax,
|
||||
49 => Self::Random,
|
||||
50 => Self::RandomInt,
|
||||
51 => Self::Seed,
|
||||
52 => Self::Formula,
|
||||
53 => Self::Simplify,
|
||||
54 => Self::Expand,
|
||||
55 => Self::Diff,
|
||||
56 => Self::Subs,
|
||||
57 => Self::Eval,
|
||||
58 => Self::Solve,
|
||||
59 => Self::Plot,
|
||||
60 => Self::PlotPoints,
|
||||
61 => Self::PlotConic,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -94,6 +167,44 @@ impl HostFn {
|
||||
"host_http_get" => Self::HttpGet,
|
||||
"host_http_post" => Self::HttpPost,
|
||||
"host_url_encode" => Self::UrlEncode,
|
||||
"host_pi" => Self::Pi,
|
||||
"host_e" => Self::EConst,
|
||||
"host_num" => Self::Num,
|
||||
"host_math_add" => Self::MathAdd,
|
||||
"host_math_sub" => Self::MathSub,
|
||||
"host_math_mul" => Self::MathMul,
|
||||
"host_math_div" => Self::MathDiv,
|
||||
"host_math_pow" => Self::MathPow,
|
||||
"host_math_neg" => Self::MathNeg,
|
||||
"host_math_sin" => Self::MathSin,
|
||||
"host_math_cos" => Self::MathCos,
|
||||
"host_math_tan" => Self::MathTan,
|
||||
"host_math_asin" => Self::MathAsin,
|
||||
"host_math_acos" => Self::MathAcos,
|
||||
"host_math_atan" => Self::MathAtan,
|
||||
"host_math_sqrt" => Self::MathSqrt,
|
||||
"host_math_abs" => Self::MathAbs,
|
||||
"host_math_ln" => Self::MathLn,
|
||||
"host_math_exp" => Self::MathExp,
|
||||
"host_math_floor" => Self::MathFloor,
|
||||
"host_math_ceil" => Self::MathCeil,
|
||||
"host_math_min" => Self::MathMin,
|
||||
"host_math_max" => Self::MathMax,
|
||||
"host_random" => Self::Random,
|
||||
"host_random_int" => Self::RandomInt,
|
||||
"host_seed" => Self::Seed,
|
||||
"host_formula" => Self::Formula,
|
||||
"host_simplify" => Self::Simplify,
|
||||
"host_expand" => Self::Expand,
|
||||
"host_diff" => Self::Diff,
|
||||
"host_subs" => Self::Subs,
|
||||
"host_eval" => Self::Eval,
|
||||
"host_solve" => Self::Solve,
|
||||
// Friendly aliases so L1 `plot` / `绘图` get host optionals (`grid`, `path`, …)
|
||||
// instead of thin wrappers that only forward required params.
|
||||
"host_plot" | "plot" | "绘图" => Self::Plot,
|
||||
"host_plot_points" | "plot_points" | "绘点列" => Self::PlotPoints,
|
||||
"host_plot_conic" | "plot_conic" | "绘圆锥" => Self::PlotConic,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -129,10 +240,45 @@ impl HostFn {
|
||||
Self::HttpGet => "host_http_get",
|
||||
Self::HttpPost => "host_http_post",
|
||||
Self::UrlEncode => "host_url_encode",
|
||||
Self::Pi => "host_pi",
|
||||
Self::EConst => "host_e",
|
||||
Self::Num => "host_num",
|
||||
Self::MathAdd => "host_math_add",
|
||||
Self::MathSub => "host_math_sub",
|
||||
Self::MathMul => "host_math_mul",
|
||||
Self::MathDiv => "host_math_div",
|
||||
Self::MathPow => "host_math_pow",
|
||||
Self::MathNeg => "host_math_neg",
|
||||
Self::MathSin => "host_math_sin",
|
||||
Self::MathCos => "host_math_cos",
|
||||
Self::MathTan => "host_math_tan",
|
||||
Self::MathAsin => "host_math_asin",
|
||||
Self::MathAcos => "host_math_acos",
|
||||
Self::MathAtan => "host_math_atan",
|
||||
Self::MathSqrt => "host_math_sqrt",
|
||||
Self::MathAbs => "host_math_abs",
|
||||
Self::MathLn => "host_math_ln",
|
||||
Self::MathExp => "host_math_exp",
|
||||
Self::MathFloor => "host_math_floor",
|
||||
Self::MathCeil => "host_math_ceil",
|
||||
Self::MathMin => "host_math_min",
|
||||
Self::MathMax => "host_math_max",
|
||||
Self::Random => "host_random",
|
||||
Self::RandomInt => "host_random_int",
|
||||
Self::Seed => "host_seed",
|
||||
Self::Formula => "host_formula",
|
||||
Self::Simplify => "host_simplify",
|
||||
Self::Expand => "host_expand",
|
||||
Self::Diff => "host_diff",
|
||||
Self::Subs => "host_subs",
|
||||
Self::Eval => "host_eval",
|
||||
Self::Solve => "host_solve",
|
||||
Self::Plot => "host_plot",
|
||||
Self::PlotPoints => "host_plot_points",
|
||||
Self::PlotConic => "host_plot_conic",
|
||||
}
|
||||
}
|
||||
|
||||
/// Required params first; see `optional_params` for trailing optionals.
|
||||
pub fn required_params(self) -> &'static [&'static str] {
|
||||
match self {
|
||||
Self::ReadText | Self::Exists | Self::ListDir | Self::MakeDir | Self::Remove => {
|
||||
@@ -152,6 +298,20 @@ impl HostFn {
|
||||
Self::JsonGet => &["value", "key"],
|
||||
Self::HttpGet => &["url"],
|
||||
Self::HttpPost => &["url", "body"],
|
||||
Self::Pi | Self::EConst | Self::Random => &[],
|
||||
Self::Num | Self::MathNeg | Self::MathSin | Self::MathCos | Self::MathTan
|
||||
| Self::MathAsin | Self::MathAcos | Self::MathAtan | Self::MathSqrt
|
||||
| Self::MathAbs | Self::MathLn | Self::MathExp | Self::MathFloor | Self::MathCeil
|
||||
| Self::Seed | Self::Formula | Self::Simplify | Self::Expand => &["value"],
|
||||
Self::MathAdd | Self::MathSub | Self::MathMul | Self::MathDiv | Self::MathPow
|
||||
| Self::MathMin | Self::MathMax => &["a", "b"],
|
||||
Self::RandomInt => &["min", "max"],
|
||||
Self::Diff => &["formula", "var"],
|
||||
Self::Subs | Self::Eval => &["formula", "var", "value"],
|
||||
Self::Solve => &["formula", "var"],
|
||||
Self::Plot => &["formula", "var", "min", "max"],
|
||||
Self::PlotPoints => &["xs", "ys"],
|
||||
Self::PlotConic => &["kind", "a"],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,6 +320,10 @@ impl HostFn {
|
||||
Self::JsonStringify => &["indent"],
|
||||
Self::HttpPost => &["content_type"],
|
||||
Self::Exec => &["args"],
|
||||
Self::Solve => &["min", "max"],
|
||||
Self::Plot => &["steps", "path", "derivative", "grid"],
|
||||
Self::PlotPoints => &["path", "grid"],
|
||||
Self::PlotConic => &["b", "h", "k", "path", "grid"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -172,7 +336,7 @@ impl HostFn {
|
||||
}
|
||||
|
||||
pub fn call_host(
|
||||
ctx: &HostContext,
|
||||
ctx: &mut HostContext,
|
||||
fn_id: HostFn,
|
||||
bound: &HashMap<String, Value>,
|
||||
) -> Result<Value, String> {
|
||||
@@ -217,6 +381,82 @@ pub fn call_host(
|
||||
bound.get("content_type"),
|
||||
),
|
||||
HostFn::UrlEncode => net::url_encode(require(bound, "text")?),
|
||||
HostFn::Pi => math::pi(),
|
||||
HostFn::EConst => math::e_const(),
|
||||
HostFn::Num => math::to_num(require(bound, "value")?),
|
||||
HostFn::MathAdd => math::binary_num("add", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathSub => math::binary_num("sub", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathMul => math::binary_num("mul", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathDiv => math::binary_num("div", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathPow => math::binary_num("pow", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathMin => math::binary_num("min", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathMax => math::binary_num("max", require(bound, "a")?, require(bound, "b")?),
|
||||
HostFn::MathNeg => math::unary_num("neg", require(bound, "value")?),
|
||||
HostFn::MathSin => math::unary_num("sin", require(bound, "value")?),
|
||||
HostFn::MathCos => math::unary_num("cos", require(bound, "value")?),
|
||||
HostFn::MathTan => math::unary_num("tan", require(bound, "value")?),
|
||||
HostFn::MathAsin => math::unary_num("asin", require(bound, "value")?),
|
||||
HostFn::MathAcos => math::unary_num("acos", require(bound, "value")?),
|
||||
HostFn::MathAtan => math::unary_num("atan", require(bound, "value")?),
|
||||
HostFn::MathSqrt => math::unary_num("sqrt", require(bound, "value")?),
|
||||
HostFn::MathAbs => math::unary_num("abs", require(bound, "value")?),
|
||||
HostFn::MathLn => math::unary_num("ln", require(bound, "value")?),
|
||||
HostFn::MathExp => math::unary_num("exp", require(bound, "value")?),
|
||||
HostFn::MathFloor => math::unary_num("floor", require(bound, "value")?),
|
||||
HostFn::MathCeil => math::unary_num("ceil", require(bound, "value")?),
|
||||
HostFn::Random => math::random(ctx),
|
||||
HostFn::RandomInt => {
|
||||
math::random_int(ctx, require(bound, "min")?, require(bound, "max")?)
|
||||
}
|
||||
HostFn::Seed => math::seed(ctx, require(bound, "value")?),
|
||||
HostFn::Formula => math::formula_parse(require(bound, "value")?),
|
||||
HostFn::Simplify => math::simplify(require(bound, "value")?),
|
||||
HostFn::Expand => math::expand(require(bound, "value")?),
|
||||
HostFn::Diff => math::diff(require(bound, "formula")?, require(bound, "var")?),
|
||||
HostFn::Subs => math::subs(
|
||||
require(bound, "formula")?,
|
||||
require(bound, "var")?,
|
||||
require(bound, "value")?,
|
||||
),
|
||||
HostFn::Eval => math::eval_at(
|
||||
require(bound, "formula")?,
|
||||
require(bound, "var")?,
|
||||
require(bound, "value")?,
|
||||
),
|
||||
HostFn::Solve => math::solve(
|
||||
require(bound, "formula")?,
|
||||
require(bound, "var")?,
|
||||
bound.get("min"),
|
||||
bound.get("max"),
|
||||
),
|
||||
HostFn::Plot => math::plot(
|
||||
ctx,
|
||||
require(bound, "formula")?,
|
||||
require(bound, "var")?,
|
||||
require(bound, "min")?,
|
||||
require(bound, "max")?,
|
||||
bound.get("steps"),
|
||||
bound.get("path"),
|
||||
bound.get("derivative"),
|
||||
bound.get("grid"),
|
||||
),
|
||||
HostFn::PlotPoints => math::plot_points(
|
||||
ctx,
|
||||
require(bound, "xs")?,
|
||||
require(bound, "ys")?,
|
||||
bound.get("path"),
|
||||
bound.get("grid"),
|
||||
),
|
||||
HostFn::PlotConic => math::plot_conic(
|
||||
ctx,
|
||||
require(bound, "kind")?,
|
||||
require(bound, "a")?,
|
||||
bound.get("b"),
|
||||
bound.get("h"),
|
||||
bound.get("k"),
|
||||
bound.get("path"),
|
||||
bound.get("grid"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user