Skip to content

Commit

Permalink
Eval replaced with compileString
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-serrano committed Jan 6, 2024
1 parent 09df294 commit 2cd3a10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
9 changes: 5 additions & 4 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ obj.addEventListener(event, e => mach.react({ [ event ]: e.value }));
```


HipHop Eval
-----------
HipHop Compile
--------------

### hiphop.hheval(string) ###
### hiphop.compileString(string) ###
<!-- [:@glyphicon glyphicon-tag function] -->

The `eval` function evaluates a string denoting a HipHop statement.
The `eval` function compiles a string denoting a HipHop statement into
a JavaScript expression.

&#x2605; Example: [eval.hh.js](../test/eval.hh.js)

Expand Down
11 changes: 6 additions & 5 deletions lib/hiphop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* ------------------------------------------------------------- */
/* Author : Colin Vidal */
/* Creation : Mon Jul 16 11:23:59 2018 */
/* Last change : Sat Jan 6 10:17:14 2024 (serrano) */
/* Last change : Sat Jan 6 18:27:21 2024 (serrano) */
/* Copyright : 2018-24 Manuel Serrano */
/* ------------------------------------------------------------- */
/* HipHop language definition */
Expand All @@ -19,7 +19,7 @@ export { ReactiveMachine } from "./machine.js";
export * from "./error.js";
export * from "./lang.js";
export { IN, OUT, INOUT } from "./ast.js";
export { hheval };
export { compileString };

import * as Parser from "../preprocessor/parser.js";
import { isServer } from "./config.js";
Expand All @@ -41,8 +41,9 @@ function script(attrs) {
}
}

function hheval(str) {
return eval(Parser.parseString(`(function() {return ${str}})()`));
function compileString(str) {
const ast = Parser.parseString(`(() => {return ${str}})()\n`);
return ast.generate().toString();
}

if (isServer && process.versions.hop) {
Expand All @@ -51,5 +52,5 @@ if (isServer && process.versions.hop) {
exports[Symbol.compiler] = compiler;
exports[Symbol.script] = script;

exports.eval = hheval;
exports.compileString = compileString;
}
4 changes: 2 additions & 2 deletions test/eval.hh.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as hh from "@hop/hiphop";

const prog = hh.hheval(`hiphop module() {
const prog = eval(hh.compileString(`hiphop module() {
in A; in B; in R; out O;
do {
Expand All @@ -11,6 +11,6 @@ const prog = hh.hheval(`hiphop module() {
}
emit O();
} every(R.now)
}`);
}`));

const mach = new hh.ReactiveMachine(prog, "EABRO");

0 comments on commit 2cd3a10

Please sign in to comment.