diff options
| author | Arun Isaac | 2025-12-02 23:18:36 +0000 |
|---|---|---|
| committer | Arun Isaac | 2025-12-17 00:06:43 +0000 |
| commit | c822b5eed29a228ff58b86a883a0a167cec654ad (patch) | |
| tree | 410d5246067f8f3fc24ec627a789215945e9efb7 | |
| parent | bd88bc76ce9e4188d1bef77e5df8206799900007 (diff) | |
| download | ravanan-c822b5eed29a228ff58b86a883a0a167cec654ad.tar.gz ravanan-c822b5eed29a228ff58b86a883a0a167cec654ad.tar.lz ravanan-c822b5eed29a228ff58b86a883a0a167cec654ad.zip | |
javascript: Provide evaluate-javascript-expression as the interface.
Rename evaluate-parameter-reference to evaluate-javascript-expression. Adjust invocations and tests. Javascript expressions are more general than parameter references. Hence, it makes sense that the public interface is named after them.
| -rw-r--r-- | ravanan/command-line-tool.scm | 2 | ||||
| -rw-r--r-- | ravanan/javascript.scm | 36 | ||||
| -rw-r--r-- | tests/javascript.scm | 74 |
3 files changed, 56 insertions, 56 deletions
diff --git a/ravanan/command-line-tool.scm b/ravanan/command-line-tool.scm index fd988f7..bb2f1b7 100644 --- a/ravanan/command-line-tool.scm +++ b/ravanan/command-line-tool.scm @@ -173,7 +173,7 @@ keys @code{input}, @code{self} and @code{runtime}." (if (and (string? expression) (javascript-expression? expression)) ;; Evaluate javascript expression. - (evaluate-parameter-reference expression context) + (evaluate-javascript-expression expression context) ;; Not a javascript expression, but some other JSON tree. Return it as is. expression)) diff --git a/ravanan/javascript.scm b/ravanan/javascript.scm index be40fcd..e4d2181 100644 --- a/ravanan/javascript.scm +++ b/ravanan/javascript.scm @@ -36,7 +36,7 @@ #:use-module (ravanan work command-line-tool) #:use-module (ravanan work ui) #:use-module (ravanan work utils) - #:export (evaluate-parameter-reference)) + #:export (evaluate-javascript-expression)) ;; node executable for evaluating javascript on worker nodes (define %worker-node @@ -71,13 +71,13 @@ (define-peg-pattern parameter-reference all (and (ignore "(") symbol (* segment) (ignore ")"))) -(define* (evaluate-parameter-reference-1 expression context expression-lib) - "Compile parameter reference @var{expression} to a G-expression that evaluates +(define* (evaluate-javascript-expression-1 expression context expression-lib) + "Compile javascript @var{expression} to a G-expression that evaluates it. The returned G-expression may reference the variables @code{inputs}, @code{self} or @code{runtime}. @var{expression} must strictly be a single -parameter reference and will not be subject to string interpolation. +javascript expression and will not be subject to string interpolation. -If @var{context} is not @code{#f}, evaluate the parameter reference in that +If @var{context} is not @code{#f}, evaluate the javascript expression in that context and return the value. @var{context} must be an association list with keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}. @@ -120,7 +120,7 @@ keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}. (evaluate-using-node expression context expression-lib)))) (define (evaluate-using-node expression context expression-lib) - "This function is the same as @code{evaluate-parameter-reference-1} but uses + "This function is the same as @code{evaluate-javascript-expression-1} but uses the node javascript engine." (define (context-value name) (scm->json-string (assoc-ref context name))) @@ -144,24 +144,24 @@ the node javascript engine." "var self = " (scm->json-string self) ";" "var runtime = " (scm->json-string runtime) ";")))) -(define (tokenize-parameter-references str) - "Split @var{str} into tokens of parameter reference and literal strings." +(define (tokenize-javascript-expressions str) + "Split @var{str} into tokens of javascript expressions and literal strings." (let ((end (if (string-prefix? "$(" str) (1+ (string-index str #\))) (string-index str #\$)))) (if end (cons (substring str 0 end) - (tokenize-parameter-references (substring str end))) + (tokenize-javascript-expressions (substring str end))) (if (string-null? str) (list) (list str))))) -(define* (evaluate-parameter-reference str #:optional context (expression-lib '())) - "Compile parameter reference @var{str} to a G-expression that references +(define* (evaluate-javascript-expression str #:optional context (expression-lib '())) + "Compile javascript expression @var{str} to a G-expression that references the variables @code{inputs}, @code{self} or @code{runtime}. @var{str} may be subject to string interpolation. -If @var{context} is not @code{#f}, evaluate the parameter reference in that +If @var{context} is not @code{#f}, evaluate the javascript expression in that context and return the value. @var{context} must be an association list with keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}. @@ -171,13 +171,13 @@ keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}. (if (and (string-prefix? "$(" token) (string-suffix? ")" token)) ;; Drop the leading "$" and evaluate. - (evaluate-parameter-reference-1 (string-drop token 1) - context - expression-lib) + (evaluate-javascript-expression-1 (string-drop token 1) + context + expression-lib) ;; token is a string literal. token)) - (match (tokenize-parameter-references str) + (match (tokenize-javascript-expressions str) ;; There is only one token. This is not a string interpolation. Do not ;; serialize JSON. ((only-token) @@ -193,8 +193,8 @@ keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}. (scm->json-string (canonicalize-json token)))) evaluated-tokens) "") - ;; Compile to a G-expression that interpolates parameter reference - ;; string. + ;; Compile to a G-expression that interpolates the javascript + ;; expression string. #~(string-join (map (lambda (token) (if (string? token) token diff --git a/tests/javascript.scm b/tests/javascript.scm index b249877..a84f949 100644 --- a/tests/javascript.scm +++ b/tests/javascript.scm @@ -37,65 +37,65 @@ (test-equal "evaluate parameter reference" "c" - (evaluate-parameter-reference "$(inputs.message['bar'][\"foo\"][2])" - '(("inputs" ("message" ("bar" ("foo" . #("a" "b" "c" "d")))))))) + (evaluate-javascript-expression "$(inputs.message['bar'][\"foo\"][2])" + '(("inputs" ("message" ("bar" ("foo" . #("a" "b" "c" "d")))))))) (test-equal "evaluate parameter reference to JSON object" '(("class" . "File") ("path" . "/foo/bar")) - (evaluate-parameter-reference "$(inputs.fasta)" - '(("inputs" ("fasta" - ("class" . "File") - ("path" . "/foo/bar")))))) + (evaluate-javascript-expression "$(inputs.fasta)" + '(("inputs" ("fasta" + ("class" . "File") + ("path" . "/foo/bar")))))) (test-equal "evaluate parameter reference with string interpolation" "24foo12foobar" - (evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads)$(inputs.output_filename)" - '(("inputs" - ("threads" . 12) - ("output_filename" . "foobar")) - ("runtime" ("cores" . 24))))) + (evaluate-javascript-expression "$(runtime.cores)foo$(inputs.threads)$(inputs.output_filename)" + '(("inputs" + ("threads" . 12) + ("output_filename" . "foobar")) + ("runtime" ("cores" . 24))))) (test-equal "evaluate parameter reference with string interpolation of JSON trees" "foo[0,1,2,3]{\"bar\":2,\"foo\":1}" - (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)" - '(("inputs" - ("object" - ("foo" . 1) - ("bar" . 2)) - ("vector" . #(0 1 2 3)))))) + (evaluate-javascript-expression "foo$(inputs.vector)$(inputs.object)" + '(("inputs" + ("object" + ("foo" . 1) + ("bar" . 2)) + ("vector" . #(0 1 2 3)))))) (test-equal "evaluate parameter reference with node" 3 - (evaluate-parameter-reference "$(inputs.n + 1)" - '(("inputs" ("n" . 2))))) + (evaluate-javascript-expression "$(inputs.n + 1)" + '(("inputs" ("n" . 2))))) (test-equal "evaluate parameter reference to JSON object using node" '(json-ref inputs "fasta") (gexp->sexp-rec - (evaluate-parameter-reference "$(inputs.fasta)"))) + (evaluate-javascript-expression "$(inputs.fasta)"))) (test-equal "evaluate parameter reference with string interpolation using node" "24foo24foobar" - (evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads*2)$(inputs.output_filename)" - '(("inputs" - ("threads" . 12) - ("output_filename" . "foobar")) - ("runtime" ("cores" . 24))))) + (evaluate-javascript-expression "$(runtime.cores)foo$(inputs.threads*2)$(inputs.output_filename)" + '(("inputs" + ("threads" . 12) + ("output_filename" . "foobar")) + ("runtime" ("cores" . 24))))) (test-equal "evaluate parameter reference with string interpolation of JSON trees using node" "foo[0,1,2,3]{\"bar\":2,\"foo\":1}20" - (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)$(inputs.object.foo*20)" - '(("inputs" - ("object" - ("foo" . 1) - ("bar" . 2)) - ("vector" . #(0 1 2 3)))))) + (evaluate-javascript-expression "foo$(inputs.vector)$(inputs.object)$(inputs.object.foo*20)" + '(("inputs" + ("object" + ("foo" . 1) + ("bar" . 2)) + ("vector" . #(0 1 2 3)))))) (test-equal "evaluate parameter reference (without context)" '(json-ref inputs "message" "bar" "foo" 2) (gexp->sexp-rec - (evaluate-parameter-reference "$(inputs.message['bar'][\"foo\"][2])"))) + (evaluate-javascript-expression "$(inputs.message['bar'][\"foo\"][2])"))) (test-equal "evaluate parameter reference with string interpolation (without context)" '(string-join @@ -107,7 +107,7 @@ (json-ref inputs "output_filename"))) "") (gexp->sexp-rec - (evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads)$(inputs.output_filename)"))) + (evaluate-javascript-expression "$(runtime.cores)foo$(inputs.threads)$(inputs.output_filename)"))) (test-equal "evaluate parameter reference with string interpolation of JSON trees (without context)" '(string-join @@ -116,7 +116,7 @@ (list "foo" (json-ref inputs "vector") (json-ref inputs "object"))) "") (gexp->sexp-rec - (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)"))) + (evaluate-javascript-expression "foo$(inputs.vector)$(inputs.object)"))) (test-equal "evaluate parameter reference with node (without context)" '(evaluate-javascript (*approximate*) @@ -126,7 +126,7 @@ "var self = " (scm->json-string self) ";" "var runtime = " (scm->json-string runtime) ";")) (gexp->sexp-rec - (evaluate-parameter-reference "$(inputs.n + 1)"))) + (evaluate-javascript-expression "$(inputs.n + 1)"))) (test-equal "evaluate parameter reference with string interpolation using node (without context)" '(string-join @@ -143,7 +143,7 @@ (json-ref inputs "output_filename"))) "") (gexp->sexp-rec - (evaluate-parameter-reference "$(runtime.cores)foo$(inputs.threads*2)$(inputs.output_filename)"))) + (evaluate-javascript-expression "$(runtime.cores)foo$(inputs.threads*2)$(inputs.output_filename)"))) (test-equal "evaluate parameter reference with string interpolation of JSON trees using node (without context)" '(string-join @@ -160,6 +160,6 @@ "var runtime = " (scm->json-string runtime) ";")))) "") (gexp->sexp-rec - (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)$(inputs.object.foo*20)"))) + (evaluate-javascript-expression "foo$(inputs.vector)$(inputs.object)$(inputs.object.foo*20)"))) (test-end "javascript") |
