Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions docs/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ let message = "hello";
let result = a + b;
```

Script variables currently support integers, floats, and strings.
Script variable types and capabilities:

| Type | Literal/Example | Description | Ops/Comparisons |
| --- | --- | --- | --- |
| Integer (int, internally i64) | `123`, `-42` | Signed 64-bit integer | +, -, *, /; can mix with DWARF integer-like scalars |
| Boolean (bool) | from comparisons: `a < b` | Produced by comparisons/logical expressions | logical AND/OR (script only); when mixing with DWARF integers, treated as 0/1 |
| String | `"hello"` | UTF-8 string literal | Equality `==`, `!=` with DWARF C strings; no ordering comparisons |

Notes:
1. Script variables do not support user-defined structs/arrays/pointers; access such data via DWARF variables (member access, deref, constant index) to obtain scalars first.
2. Floating-point arithmetic is not supported.
3. Unary minus `-` is supported and can be nested (e.g., `-1`, `-(-1)`), parsed as `0 - expr`.
4. Transport encodes booleans as a single byte 0/1; the renderer displays `true`/`false`.

### Local Variables, Parameters, and Global Variables

Expand Down Expand Up @@ -205,7 +217,7 @@ let quotient = a / b; // Division

1. Parentheses `()`
2. Member access `.`, Array access `[]`
3. Pointer dereference `*`, Address of `&`
3. Pointer dereference `*`, Address of `&`, Unary minus `-`
4. Multiplication `/`, Division `/`
5. Addition `+`, Subtraction `-`
6. Comparisons `==`, `!=`, `<`, `<=`, `>`, `>=`
Expand All @@ -226,6 +238,11 @@ let complex = (x + y) / (a - b);
- Operands are treated as booleans with "non-zero is true" semantics
- Current implementation evaluates both sides (no short-circuit yet)

Boolean values

- Comparisons and logical operators produce boolean results.
- Transport encodes booleans as a single byte 0/1. The renderer displays them as `true`/`false`.

Examples

```ghostscope
Expand All @@ -238,6 +255,20 @@ trace main:entry {
}
```

### Unary Minus

- Semantics: negate an expression; recursive nesting is supported.
- Parsing: treated as `0 - expr`, ensuring `-1`, `-x`, and `-(-1)` evaluate as signed integers.

```ghostscope
trace foo.c:42 {
let a = -1; // a = -1
let b = -(-1); // b = 1
print a; // Output: a = -1
print "X:{}", b; // Output: X:1
}
```

### Cross-type Operations With DWARF Values

- Arithmetic (+, -, *, /)
Expand Down
24 changes: 18 additions & 6 deletions docs/zh/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ let result = a + b;
说明:
1. 目前脚本层不支持自定义结构体/数组/指针类型;对于这些聚合类型,请通过 DWARF 变量访问(成员访问、解引用、常量下标)来获取标量后再参与运算。
2. eBPF 不支持浮点运算,故当前脚本变量不支持浮点字面量与浮点运算
3. 一元负号(`-`)已支持并可嵌套:例如 `-1`、`-(-1)` 均合法;解析等价于 `0 - x` 的语义。
4. 布尔传输层使用 0/1 表示,展示层统一渲染为 `true/false`。

### DWARF 变量

Expand Down Expand Up @@ -267,7 +269,7 @@ let quotient = a / b; // 除法

1. 括号 `()`
2. 成员访问 `.`,数组访问 `[]`
3. 指针解引用 `*`,取地址 `&`
3. 指针解引用 `*`,取地址 `&`,一元负号 `-`
4. 乘法 `*`,除法 `/`
5. 加法 `+`,减法 `-`
6. 比较 `==`, `!=`, `<`, `<=`, `>`, `>=`
Expand Down Expand Up @@ -342,11 +344,7 @@ trace foo.c:60 {
print "greet-ok:{}", gm == "Hello, Global!"; // gm: const char* 或 char[]
}

// 纯脚本浮点(编译期折叠)。与 DWARF 混用暂不支持。
trace foo.c:70 {
let x = 1.5 * 2.0; // 编译期折叠
if x > 2.0 { print "ok"; }
}

```

## 栈回溯语句(实现中)
Expand Down Expand Up @@ -466,3 +464,17 @@ trace server_respond {
- 字符串字面量必须使用双引号
- 大多数语句需要分号
- 追踪模式匹配支持文件模糊匹配(参见[命令参考](command-reference.md))
### 一元负号(Unary Minus)

- 语义:对表达式取相反数,支持递归嵌套。
- 解析:按 `0 - expr` 处理,确保 `-1`、`-x`、`-(-1)` 等都按有符号整数求值。
- 示例:

```ghostscope
trace foo.c:42 {
let a = -1; // a = -1
let b = -(-1); // b = 1
print a; // 输出: a = -1
print "X:{}", b; // 输出: X:1
}
```
Loading