Skip to content

Commit

Permalink
Refactor code generator. Update README & examples
Browse files Browse the repository at this point in the history
Signed-off-by: xaxys <[email protected]>
  • Loading branch information
xaxys committed May 4, 2024
1 parent 2f91179 commit 43155e9
Show file tree
Hide file tree
Showing 39 changed files with 5,797 additions and 6,898 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
.idea
.vs
.vscode
.example
.Output
.example*
.output*

# Binaries for programs and plugins
*.exe
Expand Down
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ bubbler [options] <input file>

- `-t <target>`: Target language
- `-o <output>`: Output Path
- `-inner`: Generate Inner Class (Nested Struct)
- `-single`: Generate Single File (Combine all definitions into one file, instead of one generated file per source file)
- `-minimal`: Generate Minimal Code (Usually without default getter/setter methods)
- `-decnum`: Force Generate Decimal Format for Constant Value (Translate `0xFF` to `255`, `0b1111` to `15`, etc.)
- `-signext`: Sign Extension Method used for Integer Field (Options: `shift`, `arith`)

### Examples

```sh
bubble -t c -o output/ example.bb
bubbler -t c-single -o gen.hpp example.bb
bubbler -t dump example.bb
bubbler -t c -minimal -o output/ example.bb
bubbler -t c -single -o gen.hpp example.bb
bubbler -t py -decnum -signext=arith -o output example.bb
```

### Target Languages
Expand All @@ -43,24 +48,20 @@ Run `bubbler` to see the list of supported target languages.

```text
Targets:
dump
c
c_single [c-single]
c_minimal [c-minimal, c_min, c-min]
c_minimal_single [c-minimal-single, c_min_single, c-min-single]
python [py]
python_single [python-single, py-single, py_single]
```

When selecting the target language, you can use the aliases inside `[]`. For example, `c_minimal` can be abbreviated as `c-min`, `c_min`, or `c_minimal`.
When selecting the target language, you can use the aliases inside `[]`. For example, `python` can be abbreviated as `py`.

- `dump`: Output the parse tree (intermediate representation) of the `.bb` file.

- `c`: C language, output one `.bb.h` file and one `.bb.c` file for each `.bb` file.
- `c_single`: C language, output one file that includes all definitions for all `.bb` files. The output file name (including the extension) is determined by the `-o` option.
- `c_minimal`: C language, output one `.bb.h` file and one `.bb.c` file for each `.bb` file. Do not generate getter/setter methods for fields.
- `c_minimal_single`: C language, output one file that includes all definitions for all `.bb` files. The output file name (including the extension) is determined by the `-o` option. Do not generate getter/setter methods for fields.
- With `-single`: Output one file that includes all definitions for all `.bb` files. The output file name (including the extension) is determined by the `-o` option.
- With `-minimal`: No generation of getter/setter methods for fields.

- `python`: Python language, output one `_bb.py` file for each `.bb` file.
- `python-single`: Python language, output one file that includes all definitions for all `.bb` files. The output file name (including the extension) is determined by the `-o` option.
- With `-single`: Output one file that includes all definitions for all `.bb` files. The output file name (including the extension) is determined by the `-o` option.

## Protocol Syntax

Expand Down
43 changes: 23 additions & 20 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ bubbler [options] <input file>
### 选项

- `-t <target>`: 目标语言
- `-o <output>`: 输出文件
- `-o <output>`: 输出路径
- `-inner`: 生成内部类(嵌套结构体)
- `-single`: 生成单个文件(将所有定义合并到一个文件中,而不是每个源文件生成一个文件)
- `-minimal`: 生成最小代码(通常不包含默认的getter/setter方法)
- `-decnum`: 强制生成十进制格式的常量值(将 `0xFF` 翻译为 `255`, `0b1111` 翻译为 `15` 等)
- `-signext`: 用于整数字段的符号扩展方法(选项: `shift`, `arith`

### 示例

```sh
bubble -t c -o output/ example.bb
bubbler -t c-single -o gen.hpp example.bb
bubbler -t dump example.bb
bubbler -t c -minimal -o output/ example.bb
bubbler -t c -single -o gen.hpp example.bb
bubbler -t py -decnum -signext=arith -o output example.bb
```

### 目标语言
Expand All @@ -43,24 +48,20 @@ bubbler -t dump example.bb

```text
Targets:
dump
c
c_single [c-single]
c_minimal [c-minimal, c_min, c-min]
c_minimal_single [c-minimal-single, c_min_single, c-min-single]
python [py]
python_single [python-single, py-single, py_single]
```

在选择目标语言时可以使用`[]`内的别名,例如,`c_minimal` 可以缩写为 `c-min``c_min``c_minimal`
当选择目标语言时,可以使用 `[]` 中的别名。例如,`python` 可以缩写为 `py`

- `dump`:输出 `.bb` 文件的解析树(中间表示)。

- `c`:C 语言,为每个 `.bb` 文件输出一个 `.bb.h` 文件和一个 `.bb.c` 文件。
- `c_single`:C 语言,为所有 `.bb` 文件输出一个包含所有定义的文件。输出文件名(包括扩展名)由 `-o` 选项确定。
- `c_minimal`:C 语言,为每个 `.bb` 文件输出一个 `.bb.h` 文件和一个 `.bb.c` 文件。不为字段生成 getter/setter 方法
- `c_minimal_single`:C 语言,为所有 `.bb` 文件输出一个包含所有定义的文件。输出文件名(包括扩展名)由 `-o` 选项确定。不为字段生成 getter/setter 方法。
- 使用 `-single`:输出单个文件,其中包含所有`.bb`文件的所有定义。输出文件名(包括扩展名)由`-o`选项确定。
- 使用 `-minimal`:不为字段生成默认的getter/setter方法函数

- `python`:Python 语言,为每个 `.bb` 文件输出一个 `_bb.py` 文件。
- `python-single`Python 语言,为所有 `.bb` 文件输出一个包含所有定义的文件。输出文件名(包括扩展名)由 `-o` 选项确定。
- 使用`-single`输出单个文件,其中包含所有 `.bb` 文件的所有定义。输出文件名(包括扩展名)由 `-o` 选项确定。

## 协议语法

Expand Down Expand Up @@ -313,27 +314,29 @@ struct AnotherTest {

在这个例子中,`arr` 字段的字节顺序被设置为大端序。

> 小贴士:大小端序的设置对于浮点类型同样有效,
### 自定义 getter/setter

可以为字段定义自定义的 getter 和 setter 方法,用于在读取或写入字段值时执行特定的操作。例如:

```bubbler
struct SensorTemperatureData {
uint16 temperature[2] {
get(float64): value / 10 - 40;
set(float64): value == 0 ? 0 : (value + 40) * 10;
set AnotherCustomSet(uint8): value == 0 ? 0 : (value + 40) * 10;
get temperature_display(float64): value / 10 - 40;
set temperature_display(float64): value == 0 ? 0 : (value + 40) * 10;
set another_custom_setter(uint8): value == 0 ? 0 : (value + 40) * 10;
};
}
```

在这个例子中,`temperature` 字段有一个自定义的 getter 方法和两个自定义的 setter 方法。

默认getter返回`float64`类型,并根据`value / 10 - 40`计算结果返回。其中`value`被填充为字段的值,是uint16类型。
自定义getter名为 `temperature_display`, 返回`float64` 类型,并根据 `value / 10 - 40` 计算结果返回。其中 `value` 被填充为字段的值,是uint16类型。

默认setter接收`float64`类型的参数,并根据`value == 0 ? 0 : (value + 40) * 10`计算结果并以此设置字段的值。其中`value`被填充为参数的值,是float64类型
自定义setter名为 `temperature_display`, 接收`float64` 类型的参数,并根据 `value == 0 ? 0 : (value + 40) * 10` 计算结果并以此设置字段的值。其中 `value` 被填充为参数的值,`float64` 类型

自定义setter名为`AnotherCustomSet``uint8`是参数类型。并根据`value == 0 ? 0 : (value + 40) * 10`计算结果并以此设置字段的值。其中`value`被填充为参数的值,是uint8类型
自定义setter名为 `another_custom_setter``uint8`是参数类型。并根据 `value == 0 ? 0 : (value + 40) * 10` 计算结果并以此设置字段的值。其中 `value` 被填充为参数的值,`uint8` 类型

## 贡献

Expand Down
4 changes: 2 additions & 2 deletions compiler/info_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (v *InfoVisitor) VisitProto(ctx *parser.ProtoContext) any {
return &definition.CompileError{
Position: definition.BasePosition{
File: v.Unit.UnitName.Path,
Line: ctx.GetStart().GetLine(),
Column: ctx.GetStart().GetColumn(),
Line: -1,
Column: -1,
},
Err: &definition.PackageNameNotSetError{},
}
Expand Down
Loading

0 comments on commit 43155e9

Please sign in to comment.