Skip to content

Commit baeaddb

Browse files
committed
Add use statement for declarations
1 parent a946f66 commit baeaddb

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/routes/terminal.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ const metrics = require("./metrics");
77
const runtime = require("../runtime");
88

99
const terminal = express();
10-
terminal.use(express.json());
11-
terminal.use(express.text({ type: "application/javascript" }));
1210
terminal.use(cors());
1311

12+
terminal.post(
13+
"/",
14+
(req, res, next) =>
15+
req.is("application/javascript") ? next() : res.status(415).end(),
16+
express.text({ type: "application/javascript" }),
17+
(req, res) => {
18+
const details = runtime.process(req.body, { details: true });
19+
res.send(details);
20+
}
21+
);
22+
23+
terminal.use(express.json(), (err, req, res, next) =>
24+
err ? res.status(422).end() : next()
25+
);
1426
terminal.use(graph);
1527
terminal.use(openapi);
1628
terminal.use(logs);
1729
terminal.use(metrics);
1830

19-
terminal.post("/", (req, res) => {
20-
const mode = req.headers["x-nuc-mode"];
21-
const declarative = mode?.toLowerCase() === "declarative";
22-
const details = runtime.process(req.body, { declarative, details: true });
23-
24-
res.send(details);
25-
});
26-
2731
// eslint-disable-next-line no-unused-vars
2832
terminal.use((err, req, res, next) => {
2933
if (typeof err === "string") {

src/runtime.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ try {
2020
module.exports.process = function (string, options = {}) {
2121
const { options: configOptions } = config();
2222
options = { ...defaultOptions, ...configOptions, ...options };
23-
const { declarative, details } = options;
2423

2524
const before = Date.now();
2625
let result;
2726

2827
try {
29-
const statements = Statement.compile(string, options);
28+
const statements = Statement.compile(string);
29+
30+
if (!statements.length) {
31+
return;
32+
}
33+
3034
transaction.start();
3135
result = stack.process(statements, null, options);
3236
transaction.end();
@@ -42,7 +46,7 @@ module.exports.process = function (string, options = {}) {
4246
datastore.write({
4347
s: string,
4448
$: result.$nuc?.length ? result.$nuc : undefined,
45-
c: declarative ? true : undefined,
49+
c: options.declarative ? true : undefined,
4650
t: time,
4751
r: result.value,
4852
d: date,
@@ -56,11 +60,11 @@ module.exports.process = function (string, options = {}) {
5660

5761
Event.clear();
5862

59-
if (details) {
63+
if (options.details) {
6064
return {
6165
result: result.value,
6266
$nuc: result.$nuc,
63-
declarative,
67+
declarative: options.declarative,
6468
date,
6569
time,
6670
error: !!result.error,

src/stack.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const RETURN = require("./nuc/RETURN");
1313

1414
function process(statements, prior, options = {}) {
1515
const root = new Scope(prior);
16-
const { declarative } = options;
1716

1817
let instructions = statements.map(
1918
(statement) =>
@@ -61,6 +60,14 @@ function process(statements, prior, options = {}) {
6160
value = state.expression(scope, evaluation);
6261
}
6362

63+
if (value === "use declarative") {
64+
options.declarative = true;
65+
}
66+
67+
if (value === "use imperative") {
68+
options.declarative = false;
69+
}
70+
6471
if (instruction.scope === root && !instruction.derivative) {
6572
result.value = value;
6673
}
@@ -217,7 +224,7 @@ function process(statements, prior, options = {}) {
217224

218225
let list = statement.graph(instruction.scope);
219226

220-
if (declarative) {
227+
if (options.declarative) {
221228
if (list) {
222229
list.forEach((target) => {
223230
if (target.previous[statement.key] !== undefined) {

0 commit comments

Comments
 (0)