diff --git a/nasal-web-app/package.json b/nasal-web-app/package.json new file mode 100644 index 0000000..f582aca --- /dev/null +++ b/nasal-web-app/package.json @@ -0,0 +1,16 @@ +{ + "name": "nasal-web-app", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.21.1", + "ffi-napi": "^4.0.3" + } +} diff --git a/nasal-web-app/public/index.html b/nasal-web-app/public/index.html new file mode 100644 index 0000000..5b194cf --- /dev/null +++ b/nasal-web-app/public/index.html @@ -0,0 +1,620 @@ + + + + + + Nasal Interpreter Web Demo + + + + + +
+
+ +

Nasal Interpreter Web Demo

+

Write and execute Nasal code directly in your browser

+
+ +
+ Web App by + + LIANG Sidi + +
+
+
+ +
+
+

Code Editor

+ +
+
+
+

Output

+
+
+
+
+
+ +
+ + +
+ +
+

Example Programs:

+ + + +
+
+ + + + + + diff --git a/nasal-web-app/server.js b/nasal-web-app/server.js new file mode 100644 index 0000000..5e69dae --- /dev/null +++ b/nasal-web-app/server.js @@ -0,0 +1,50 @@ +const express = require('express'); +const ffi = require('ffi-napi'); +const path = require('path'); + +const app = express(); + +app.use(express.json()); +app.use(express.static('public')); + +const nasalLib = ffi.Library(path.join(__dirname, '../module/libnasal-web'), { + 'nasal_init': ['pointer', []], + 'nasal_cleanup': ['void', ['pointer']], + 'nasal_eval': ['string', ['pointer', 'string']], + 'nasal_get_error': ['string', ['pointer']] +}); + +app.post('/eval', (req, res) => { + const { code } = req.body; + if (!code) { + return res.status(400).json({ error: 'No code provided' }); + } + + const ctx = nasalLib.nasal_init(); + try { + const result = nasalLib.nasal_eval(ctx, code); + const error = nasalLib.nasal_get_error(ctx); + + // Check if there's an error first + if (error && error !== 'null' && error.trim() !== '') { + console.log('Nasal error:', error); // For debugging + res.json({ error: error }); + } else if (result && result.trim() !== '') { + console.log('Nasal output:', result); // For debugging + res.json({ result: result }); + } else { + res.json({ error: 'No output or error returned' }); + } + } catch (err) { + console.error('Server error:', err); // For debugging + res.status(500).json({ error: err.message }); + } finally { + nasalLib.nasal_cleanup(ctx); + } +}); + +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); + console.log(`Visit http://localhost:${PORT} to use the Nasal interpreter`); +}); \ No newline at end of file diff --git a/nasal-web-app/std b/nasal-web-app/std new file mode 120000 index 0000000..6ef7545 --- /dev/null +++ b/nasal-web-app/std @@ -0,0 +1 @@ +../std \ No newline at end of file