From f26fd2e6deccc5a7172d55468d6af87d9bdad332 Mon Sep 17 00:00:00 2001 From: mcDandy Date: Wed, 3 Jun 2026 12:02:07 +0200 Subject: [PATCH] AI Auto-generate functions for frontend and future use --- more_math/Parser/compile.bat | 3 -- more_math/Parser/compile.ps1 | 84 +++++++++++++++++++++++++++++ more_math/Parser/inbuilt_symbols.py | 10 ++++ web/inbuilt_symbols.js | 13 +++++ web/script_text_input.js | 32 +---------- 5 files changed, 108 insertions(+), 34 deletions(-) delete mode 100644 more_math/Parser/compile.bat create mode 100644 more_math/Parser/compile.ps1 create mode 100644 more_math/Parser/inbuilt_symbols.py create mode 100644 web/inbuilt_symbols.js diff --git a/more_math/Parser/compile.bat b/more_math/Parser/compile.bat deleted file mode 100644 index cd0f845..0000000 --- a/more_math/Parser/compile.bat +++ /dev/null @@ -1,3 +0,0 @@ -antlr4 -v 4.9.3 -Dlanguage=Python3 -visitor -o ./legacy MathExpr.g4 - -antlr4 -v 4.13.2 -Dlanguage=Python3 -visitor -o ./grammer MathExpr.g4 \ No newline at end of file diff --git a/more_math/Parser/compile.ps1 b/more_math/Parser/compile.ps1 new file mode 100644 index 0000000..a20b649 --- /dev/null +++ b/more_math/Parser/compile.ps1 @@ -0,0 +1,84 @@ +Write-Host "===================================================" -ForegroundColor Cyan +Write-Host "1/2 Compiling ANTLR Grammars" -ForegroundColor Cyan +Write-Host "===================================================" -ForegroundColor Cyan + +Write-Host "Compiling legacy version (ANTLR 4.9.3)..." -ForegroundColor Yellow +antlr4 -v 4.9.3 -Dlanguage=Python3 -visitor -o ./legacy MathExpr.g4 + +Write-Host "Compiling modern version (ANTLR 4.13.2)..." -ForegroundColor Yellow +antlr4 -v 4.13.2 -Dlanguage=Python3 -visitor -o ./grammer MathExpr.g4 + +Write-Host "" +Write-Host "===================================================" -ForegroundColor Cyan +Write-Host "2/2 Generating Python and JS Token Sets" -ForegroundColor Cyan +Write-Host "===================================================" -ForegroundColor Cyan + +$g4Path = "MathExpr.g4" +if (-not (Test-Path $g4Path)) { + Write-Error "MathExpr.g4 not found!" + exit 1 +} + +# 1. Načtení a očištění gramatiky od komentářů +$content = [System.IO.File]::ReadAllText($g4Path, [System.Text.Encoding]::UTF8) +$content = $content -replace '//.*', '' +$content = $content -replace '/\*[\s\S]*?\*/', '' + +# 2. Vytažení všech alfabetických řetězců v apostrofech +$matches = [regex]::Matches($content, "'([a-zA-Z_][a-zA-Z0-9_]*)'") +$allSymbols = @() +foreach ($m in $matches) { + $allSymbols += $m.Groups[1].Value +} +$allSymbols = $allSymbols | Sort-Object -Unique + +# 3. Definice pevných množin +$keywords = @('if', 'else', 'while', 'for', 'in', 'break', 'continue', 'return', 'none', 'None', 'null', 'NULL') | Sort-Object -Unique +$constants = @('pi', 'PI', 'e', 'E') | Sort-Object -Unique + +# Vše ostatní, co jsou slova a nejsou klíčová slova ani konstanty, jsou funkce +$functions = @() +foreach ($sym in $allSymbols) { + if ($sym -notin $keywords -and $sym -notin $constants) { + $functions += $sym + } +} +$functions = $functions | Sort-Object -Unique + +# 4. Formátování a zápis do PYTHON souboru (do kořene/aktuální složky vedle .g4) +$pyPath = "inbuilt_symbols.py" +$pyContent = @( + "# Generated automatically by compile.ps1. Do not edit.", + "INBUILT_KEYWORDS = {" + (($keywords | foreach { "'$_'" }) -join ", ") + "}", + "INBUILT_CONSTANTS = {" + (($constants | foreach { "'$_'" }) -join ", ") + "}", + "INBUILT_FUNCTIONS = {" + (($functions | foreach { "'$_'" }) -join ", ") + "}" +) -join [Environment]::NewLine + +[System.IO.File]::WriteAllText($pyPath, $pyContent, [System.Text.Encoding]::UTF8) +Write-Host "-> Exported Python symbols to root: $pyPath" -ForegroundColor Green + +# 5. Formátování a zápis do JAVASCRIPT souboru (odskok do složky web) +# Pokud máš složku web hned vedle .g4, změň '..\web' na 'web' +$jsDir = "..\..\web" +if (-not (Test-Path $jsDir)) { + New-Item -ItemType Directory -Force -Path $jsDir | Out-Null +} +$jsPath = Join-Path $jsDir "inbuilt_symbols.js" + +$jsContent = @( + "// Generated automatically by compile.ps1. Do not edit.", + "", + "export const KEYWORDS = new Set([" + (($keywords | foreach { "`"$_`"" }) -join ", ") + "]);", + "", + "export const CONSTANTS = new Set([" + (($constants | foreach { "`"$_`"" }) -join ", ") + "]);", + "", + "export const FUNCTIONS = new Set([" + (($functions | foreach { "`"$_`"" }) -join ", ") + "]);" +) -join [Environment]::NewLine + +[System.IO.File]::WriteAllText($jsPath, $jsContent, [System.Text.Encoding]::UTF8) +Write-Host "-> Exported JS symbols to: $jsPath" -ForegroundColor Green + +Write-Host "" +Write-Host "===================================================" -ForegroundColor Cyan +Write-Host "Compilation and Generation Successful!" -ForegroundColor Cyan +Write-Host "===================================================" -ForegroundColor Cyan diff --git a/more_math/Parser/inbuilt_symbols.py b/more_math/Parser/inbuilt_symbols.py new file mode 100644 index 0000000..dcabbb1 --- /dev/null +++ b/more_math/Parser/inbuilt_symbols.py @@ -0,0 +1,10 @@ +# Generated automatically by compile.ps1. Do not edit. +INBUILT_KEYWORDS = { +'break', 'continue', 'else', 'for', 'if', 'in', 'none', 'null', 'return', 'while' +} +INBUILT_CONSTANTS = { +'e', 'pi' +} +INBUILT_FUNCTIONS = { +'abs', 'acos', 'acosh', 'all', 'angle', 'any', 'append', 'argmax', 'argmin', 'argsort', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'band', 'batch_shuffle', 'bitcount', 'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blur', 'bnot', 'bor', 'botk', 'botk_ind', 'botk_indices', 'bxor', 'cat', 'ceil', 'cellular', 'cellular_noise', 'cielab_to_rgb', 'clamp', 'cnt', 'concat', 'concatenate', 'conv', 'convolution', 'corr', 'correlation', 'cos', 'cosh', 'cosine_similarity', 'cossim', 'count', 'cov', 'crop', 'cross', 'cubic', 'cubic_ease', 'cumprod', 'cumsum', 'dilate', 'dist', 'distance', 'domain_warp', 'domain_warp_noise', 'dot', 'edge', 'elastic', 'elastic_ease', 'entropy', 'erf', 'erfinv', 'erode', 'exp', 'ezconv', 'ezconvolution', 'fft', 'find', 'flatten', 'flip', 'float', 'floor', 'flow_ang', 'flow_angle', 'flow_apply', 'flow_mag', 'flow_magnitude', 'flow_to_image', 'fract', 'gamma', 'gaussian', 'gelu', 'get_value', 'hist', 'histogram', 'hsv_to_rgb', 'ifft', 'int', 'int_to_rgb', 'interpolate_area', 'interpolate_linear', 'interpolate_nearest', 'interpolate_nearest_exact', 'join', 'length', 'lerp', 'linspace', 'ln', 'log', 'logspace', 'lower', 'map', 'matmul', 'mean', 'median', 'mode', 'moment', 'morph_close', 'morph_open', 'motion_mask', 'nan_to_num', 'noise', 'now', 'nvl', 'oklab_to_rgb', 'overlay', 'pad', 'percentile', 'perlin', 'perlin_noise', 'perm', 'permute', 'pinv', 'plasma', 'plasma_noise', 'popcnt', 'popcount', 'pow', 'prcnt', 'print', 'print_shape', 'pshp', 'quantile', 'quartil', 'quartile', 'rand', 'randb', 'randbeta', 'randc', 'rande', 'randg', 'randgumbel', 'randchi2', 'randl', 'randln', 'randn', 'random_bernoulli', 'random_beta', 'random_cauchy', 'random_exponential', 'random_gamma', 'random_gumbel', 'random_chi2', 'random_laplace', 'random_log_normal', 'random_normal', 'random_poisson', 'random_studentt', 'random_uniform', 'random_weibull', 'randp', 'randt', 'randu', 'randw', 'range', 'relu', 'remap', 'replace', 'reshape', 'rgb_to_cielab', 'rgb_to_hsv', 'rgb_to_int', 'rgb_to_oklab', 'ridged', 'ridged_noise', 'rife', 'roll', 'round', 'rshp', 'select', 'shape', 'shuffle', 'sigm', 'sign', 'sin', 'sine', 'sine_ease', 'sinh', 'smax', 'smin', 'smootherstep', 'smoothstep', 'snorm', 'softmax', 'softmin', 'softplus', 'sort', 'split', 'sqrt', 'stack_clear', 'stack_get', 'stack_has', 'stack_pop', 'stack_push', 'std', 'step', 'substr', 'substring', 'sum', 'swap', 'tan', 'tanh', 'tensor', 'timestamp', 'tmax', 'tmin', 'tnorm', 'topk', 'topk_ind', 'topk_indices', 'trim', 'turbulence', 'unique', 'upper', 'var', 'voronoi', 'voronoi_noise', 'where', 'worley' +} \ No newline at end of file diff --git a/web/inbuilt_symbols.js b/web/inbuilt_symbols.js new file mode 100644 index 0000000..c4679b3 --- /dev/null +++ b/web/inbuilt_symbols.js @@ -0,0 +1,13 @@ +// Generated automatically by compile.ps1. Do not edit. + +export const KEYWORDS = new Set([ +"break", "continue", "else", "for", "if", "in", "none", "null", "return", "while" +]); + +export const CONSTANTS = new Set([ +"e", "pi" +]); + +export const FUNCTIONS = new Set([ +"abs", "acos", "acosh", "all", "angle", "any", "append", "argmax", "argmin", "argsort", "asin", "asinh", "atan", "atan2", "atanh", "band", "batch_shuffle", "bitcount", "bitwise_and", "bitwise_not", "bitwise_or", "bitwise_xor", "blur", "bnot", "bor", "botk", "botk_ind", "botk_indices", "bxor", "cat", "ceil", "cellular", "cellular_noise", "cielab_to_rgb", "clamp", "cnt", "concat", "concatenate", "conv", "convolution", "corr", "correlation", "cos", "cosh", "cosine_similarity", "cossim", "count", "cov", "crop", "cross", "cubic", "cubic_ease", "cumprod", "cumsum", "dilate", "dist", "distance", "domain_warp", "domain_warp_noise", "dot", "edge", "elastic", "elastic_ease", "entropy", "erf", "erfinv", "erode", "exp", "ezconv", "ezconvolution", "fft", "find", "flatten", "flip", "float", "floor", "flow_ang", "flow_angle", "flow_apply", "flow_mag", "flow_magnitude", "flow_to_image", "fract", "gamma", "gaussian", "gelu", "get_value", "hist", "histogram", "hsv_to_rgb", "ifft", "int", "int_to_rgb", "interpolate_area", "interpolate_linear", "interpolate_nearest", "interpolate_nearest_exact", "join", "length", "lerp", "linspace", "ln", "log", "logspace", "lower", "map", "matmul", "mean", "median", "mode", "moment", "morph_close", "morph_open", "motion_mask", "nan_to_num", "noise", "now", "nvl", "oklab_to_rgb", "overlay", "pad", "percentile", "perlin", "perlin_noise", "perm", "permute", "pinv", "plasma", "plasma_noise", "popcnt", "popcount", "pow", "prcnt", "print", "print_shape", "pshp", "quantile", "quartil", "quartile", "rand", "randb", "randbeta", "randc", "rande", "randg", "randgumbel", "randchi2", "randl", "randln", "randn", "random_bernoulli", "random_beta", "random_cauchy", "random_exponential", "random_gamma", "random_gumbel", "random_chi2", "random_laplace", "random_log_normal", "random_normal", "random_poisson", "random_studentt", "random_uniform", "random_weibull", "randp", "randt", "randu", "randw", "range", "relu", "remap", "replace", "reshape", "rgb_to_cielab", "rgb_to_hsv", "rgb_to_int", "rgb_to_oklab", "ridged", "ridged_noise", "rife", "roll", "round", "rshp", "select", "shape", "shuffle", "sigm", "sign", "sin", "sine", "sine_ease", "sinh", "smax", "smin", "smootherstep", "smoothstep", "snorm", "softmax", "softmin", "softplus", "sort", "split", "sqrt", "stack_clear", "stack_get", "stack_has", "stack_pop", "stack_push", "std", "step", "substr", "substring", "sum", "swap", "tan", "tanh", "tensor", "timestamp", "tmax", "tmin", "tnorm", "topk", "topk_ind", "topk_indices", "trim", "turbulence", "unique", "upper", "var", "voronoi", "voronoi_noise", "where", "worley" +]); \ No newline at end of file diff --git a/web/script_text_input.js b/web/script_text_input.js index 9f17611..b13162d 100644 --- a/web/script_text_input.js +++ b/web/script_text_input.js @@ -1,4 +1,5 @@ import { app } from "/scripts/app.js"; +import { KEYWORDS, CONSTANTS, FUNCTIONS } from "./inbuilt_symbols.js"; const STYLE_ID = "mrmth-script-line-numbers-unified"; @@ -617,37 +618,6 @@ if (!window.__mrmthScriptInputUnifiedInit) { }); } -const KEYWORDS = new Set(["if", "else", "while", "for", "in", "break", "continue", "return", "none", "None", "null", "NULL"]); -const CONSTANTS = new Set(["pi", "PI", "e", "E"]); -const FUNCTIONS = new Set([ - "sin", "cos", "tan", "asin", "acos", "atan", "atan2", "sinh", "cosh", "tanh", "asinh", "acosh", "atanh", - "abs", "sqrt", "ln", "log", "exp", "smin", "smax", "tmin", "tmax", "tnorm", "snorm", "floor", "ceil", - "round", "gamma", "pow", "sigm", "clamp", "fft", "ifft", "angle", "print", "print_shape", "pshp", "nvl", - "nan_to_num", "lerp", "step", "smoothstep", "fract", "relu", "softplus", "gelu", "sign", "map", "ezconvolution", - "ezconv", "convolution", "conv", "swap", "permute", "perm", "reshape", "rshp", "range", "topk", "botk", "pinv", - "sum", "mean", "std", "var", "quartile", "quartil", "percentile", "prcnt", "quantile", "dot", "moment", "erf", "erfinv", "any", - "all", "edge", "blur", "gaussian", "median", "mode", "cumsum", "cumprod", "topk_ind", "topk_indices", "botk_ind", - "botk_indices", "cubic_ease", "cubic", "elastic_ease", "elastic", "sine_ease", "sine", "smootherstep", "dist", - "distance", "remap", "cossim", "cosine_similarity", "count", "cnt", "length", "flatten", "append", "get_value", - "flow_apply", "batch_shuffle", "shuffle", "motion_mask", "flow_to_image", "overlay", "pad", "cross", "matmul", "rife", - "bnot", "bitwise_not", "bitcount", "popcount", "popcnt", "shape", "band", "bitwise_and", "bxor", "bitwise_xor", - "bor", "bitwise_or", "tensor", "stack_push", "stack_pop", "stack_clear", "stack_has", "stack_get", "timestamp", "now", - "sort", "argsort", "argmin", "argmax", "softmax", "softmin", "unique", "flip", "cov", "corr", "correlation", "entropy", - "crop", "cat", "concatenate", "concat", "float", "int", "linspace", "logspace", "roll", "select", - "noise", "randn", "random_normal", "rand", "randu", "random_uniform", "randc", "random_cauchy", "rande", - "random_exponential", "randln", "random_log_normal", "randb", "random_bernoulli", "randp", "random_poisson", "randg", - "random_gamma", "randbeta", "random_beta", "randl", "random_laplace", "randgumbel", "random_gumbel", "randw", - "random_weibull", "randchi2", "random_chi2", "randt", "random_studentt", "perlin", "perlin_noise", "cellular", "voronoi", - "worley", "cellular_noise", "voronoi_noise", "plasma", "turbulence", "plasma_noise", "ridged", "ridged_noise", - "domain_warp", "domain_warp_noise", - "upper", "lower", "split", "join", "substring", "substr", "find", "trim", "replace", - "dilate", "erode", "morph_open", "morph_close", - "rgb_to_hsv", "hsv_to_rgb", - "rgb_to_oklab", "oklab_to_rgb", "rgb_to_cielab", "cielab_to_rgb", - "int_to_rgb", "rgb_to_int", - "where", "histogram", "hist", "flow_mag", "flow_magnitude", "flow_ang", "flow_angle", - "interpolate_linear", "interpolate_area", "interpolate_nearest", "interpolate_nearest_exact"]); - const BRACKET_PAIRS = { "(": ")", "[": "]", "{": "}" }; const OPENING_BRACKETS = new Set(["(", "[", "{"]);