Skip to content

Commit 46265f9

Browse files
committed
feat: unify expr print
1 parent f9ec0b1 commit 46265f9

11 files changed

Lines changed: 864 additions & 809 deletions

File tree

docs/scripting.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,19 @@ let message = "hello";
8888
let result = a + b;
8989
```
9090

91-
Script variables currently support integers, floats, and strings.
91+
Script variable types and capabilities:
92+
93+
| Type | Literal/Example | Description | Ops/Comparisons |
94+
| --- | --- | --- | --- |
95+
| Integer (int, internally i64) | `123`, `-42` | Signed 64-bit integer | +, -, *, /; can mix with DWARF integer-like scalars |
96+
| 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 |
97+
| String | `"hello"` | UTF-8 string literal | Equality `==`, `!=` with DWARF C strings; no ordering comparisons |
98+
99+
Notes:
100+
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.
101+
2. Floating-point arithmetic is not supported.
102+
3. Unary minus `-` is supported and can be nested (e.g., `-1`, `-(-1)`), parsed as `0 - expr`.
103+
4. Transport encodes booleans as a single byte 0/1; the renderer displays `true`/`false`.
92104

93105
### Local Variables, Parameters, and Global Variables
94106

@@ -205,7 +217,7 @@ let quotient = a / b; // Division
205217

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

241+
Boolean values
242+
243+
- Comparisons and logical operators produce boolean results.
244+
- Transport encodes booleans as a single byte 0/1. The renderer displays them as `true`/`false`.
245+
229246
Examples
230247

231248
```ghostscope
@@ -238,6 +255,20 @@ trace main:entry {
238255
}
239256
```
240257

258+
### Unary Minus
259+
260+
- Semantics: negate an expression; recursive nesting is supported.
261+
- Parsing: treated as `0 - expr`, ensuring `-1`, `-x`, and `-(-1)` evaluate as signed integers.
262+
263+
```ghostscope
264+
trace foo.c:42 {
265+
let a = -1; // a = -1
266+
let b = -(-1); // b = 1
267+
print a; // Output: a = -1
268+
print "X:{}", b; // Output: X:1
269+
}
270+
```
271+
241272
### Cross-type Operations With DWARF Values
242273

243274
- Arithmetic (+, -, *, /)

docs/zh/scripting.md

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

102104
### DWARF 变量
103105

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

268270
1. 括号 `()`
269271
2. 成员访问 `.`,数组访问 `[]`
270-
3. 指针解引用 `*`,取地址 `&`
272+
3. 指针解引用 `*`,取地址 `&`,一元负号 `-`
271273
4. 乘法 `*`,除法 `/`
272274
5. 加法 `+`,减法 `-`
273275
6. 比较 `==`, `!=`, `<`, `<=`, `>`, `>=`
@@ -342,11 +344,7 @@ trace foo.c:60 {
342344
print "greet-ok:{}", gm == "Hello, Global!"; // gm: const char* 或 char[]
343345
}
344346
345-
// 纯脚本浮点(编译期折叠)。与 DWARF 混用暂不支持。
346-
trace foo.c:70 {
347-
let x = 1.5 * 2.0; // 编译期折叠
348-
if x > 2.0 { print "ok"; }
349-
}
347+
350348
```
351349

352350
## 栈回溯语句(实现中)
@@ -466,3 +464,17 @@ trace server_respond {
466464
- 字符串字面量必须使用双引号
467465
- 大多数语句需要分号
468466
- 追踪模式匹配支持文件模糊匹配(参见[命令参考](command-reference.md)
467+
### 一元负号(Unary Minus)
468+
469+
- 语义:对表达式取相反数,支持递归嵌套。
470+
- 解析:按 `0 - expr` 处理,确保 `-1``-x``-(-1)` 等都按有符号整数求值。
471+
- 示例:
472+
473+
```ghostscope
474+
trace foo.c:42 {
475+
let a = -1; // a = -1
476+
let b = -(-1); // b = 1
477+
print a; // 输出: a = -1
478+
print "X:{}", b; // 输出: X:1
479+
}
480+
```

0 commit comments

Comments
 (0)