Skip to content

Commit

Permalink
Merge pull request #55 from sidi762/master
Browse files Browse the repository at this point in the history
Added web app demo
  • Loading branch information
ValKmjolnir authored Nov 9, 2024
2 parents 0c64090 + ebca28d commit 0e85bfc
Show file tree
Hide file tree
Showing 15 changed files with 2,059 additions and 25 deletions.
76 changes: 60 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Build directories
build/
out/
dist/
cmake-build-*/

# IDE and editor files
.vscode/
.idea/
.vs/
*.swp
*.swo
*~
.DS_Store
.env
.env.local

# C++ specific
# Prerequisites
*.d

Expand Down Expand Up @@ -30,32 +48,58 @@
*.exe
*.out
*.app
nasal
nasal.exe

# VS C++ sln
# Visual Studio specific
*.sln
*.vcxproj
*.vcxproj.filters
*.vcxproj.user
.vs
x64
x64/
CMakePresents.json
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb

# nasal executable
nasal
nasal.exe
# CMake
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake
_deps/

# misc
.vscode
dump
# Node.js specific (for the web app)
node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log
package-lock.json

# Project specific
dump/
fgfs.log
.temp.*
*.ppm

# build dir
build
out
# Logs and databases
*.log
*.sqlite
*.sqlite3
*.db

# macOS special cache directory
# OS generated files
.DS_Store

# ppm picture generated by ppmgen.nas
*.ppm
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ target_link_libraries(mat module-used-object)
add_library(nasock SHARED ${CMAKE_SOURCE_DIR}/module/nasocket.cpp)
target_include_directories(nasock PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(nasock module-used-object)

# Add web library
add_library(nasal-web SHARED
src/nasal_web.cpp
${NASAL_OBJECT_SOURCE_FILE}
)
target_include_directories(nasal-web PRIVATE ${CMAKE_SOURCE_DIR}/src)
set_target_properties(nasal-web PROPERTIES
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
55 changes: 51 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [__Trace Back Info__](#trace-back-info)
* [__Debugger__](#debugger)
* [__REPL__](#repl)
* [__Web Interface__](#web-interface)

__Contact us if having great ideas to share!__

Expand Down Expand Up @@ -121,7 +122,7 @@ runtime.windows.set_utf8_output();

![error](./doc/gif/error.gif)

<details><summary>Must use `var` to define variables</summary>
<details><summary>Must use `var` to define variables</summary>

This interpreter uses more strict syntax to make sure it is easier for you to program and debug.
And flightgear's nasal interpreter also has the same rule.
Expand All @@ -146,13 +147,13 @@ If you forget to add the keyword `var`, you will get this:
```javascript
code: undefined symbol "i"
--> test.nas:1:9
|
|
1 | foreach(i; [0, 1, 2, 3])
| ^ undefined symbol "i"

code: undefined symbol "i"
--> test.nas:2:11
|
|
2 | print(i)
| ^ undefined symbol "i"
```
Expand Down Expand Up @@ -441,5 +442,51 @@ Nasal REPL interpreter version 11.1 (Nov 1 2023 23:37:30)
>>> use std.json;
{stringify:func(..) {..},parse:func(..) {..}}

>>>
>>>
```

## __Web Interface__

A web-based interface is now available for trying out Nasal code directly in your browser. It includes both a code editor and an interactive REPL (WIP).

### Web Code Editor
- Syntax highlighting using CodeMirror
- Error highlighting and formatting
- Example programs
- Execution time display option
- Configurable execution time limits
- Notice: The security of the online interpreter is not well tested, please use it with sandbox mechanism or other security measures.

### Web REPL
- ** IMPORTANT: The time limit in REPL is not correctly implemented yet. Thus this REPL web binding is not considered finished. Do not use it in production before it's fixed. **
- Interactive command-line style interface in browser
- Multi-line input support with proper prompts (`>>>` and `...`)
- Command history navigation
- Error handling with formatted error messages
- Example snippets for quick testing

### Running the Web Interface

1. Build the Nasal shared library:
```bash
cmake -DBUILD_SHARED_LIBS=ON .
make nasal-web
```

2. Set up and run the web application:

For the code editor:
```bash
cd nasal-web-app
npm install
node server.js
```
Visit `http://127.0.0.1:3000/`

For the REPL:
```bash
cd nasal-web-app
npm install
node server_repl.js
```
Visit `http://127.0.0.1:3001/repl.html`
59 changes: 56 additions & 3 deletions doc/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@

__如果有好的意见或建议,欢迎联系我们!__

* __[email protected]__ (ValKmjolnir)

* __[email protected]__ (Sidi)
- __[email protected]__ (ValKmjolnir)
- __[email protected]__ (Sidi)

## __简介__

Expand Down Expand Up @@ -428,3 +427,57 @@ Nasal REPL interpreter version 11.1 (Nov 1 2023 23:37:30)

>>>
```
## __Web 界面__

现已提供基于 Web 的库以及示例界面,您可以直接在浏览器中编写和运行 Nasal 代码。该界面包括代码编辑器和交互式 REPL(未完成)。

### __Web 代码编辑器__

- **语法高亮:** 使用 CodeMirror 提供增强的编码体验。
- **错误高亮和格式化:** 清晰显示语法和运行时错误。
- **示例程序:** 预加载的示例,帮助您快速上手。
- **执行时间显示选项:** 可选择查看代码执行所需时间。
- **可配置的执行时间限制:** 设置时间限制以防止代码长时间运行。
- **提示:** 在线解释器的安全性尚未得到广泛测试,建议配合沙盒机制等安全措施使用。

### __Web REPL__

- **重要提示:** REPL 中的代码执行时间限制尚未正确实现。此 REPL 库目前不稳定,请勿在生产环境中使用。
- **交互式命令行界面:** 在浏览器中体验熟悉的 REPL 环境。
- **多行输入支持:** 使用 `>>>``...` 提示符无缝输入多行代码。
- **命令历史导航:** 使用箭头键轻松浏览命令历史。
- **格式化的错误处理:** 接收清晰且格式化的错误消息,助力调试。
- **快速测试的示例代码片段:** 访问并运行示例代码片段,快速测试功能。

### __运行 Web 界面__

1. **构建 Nasal 共享库:**

```bash
cmake -DBUILD_SHARED_LIBS=ON .
make nasal-web
```

2. **设置并运行 Web 应用:**

**代码编辑器:**

```bash
cd nasal-web-app
npm install
node server.js
```

在浏览器中访问 `http://127.0.0.1:3000/` 以使用代码编辑器。

**REPL:**

```bash
cd nasal-web-app
npm install
node server_repl.js
```

在浏览器中访问 `http://127.0.0.1:3001/repl.html` 以使用 REPL 界面。

---
17 changes: 17 additions & 0 deletions nasal-web-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"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",
"yargs": "^17.7.2"
}
}
Loading

0 comments on commit 0e85bfc

Please sign in to comment.