Skip to content

Commit a7e5301

Browse files
committed
add comments and update to cangjie 1.0.5
1 parent 926cbd9 commit a7e5301

5 files changed

Lines changed: 114 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ jobs:
1212
- os: ubuntu-latest
1313
cangjie_version: 1.0.0
1414
- os: ubuntu-latest
15-
cangjie_version: 1.0.4
15+
cangjie_version: 1.0.5
1616
- os: windows-latest
17-
cangjie_version: 1.0.4
17+
cangjie_version: 1.0.5
1818
- os: macos-latest
19-
cangjie_version: 1.0.4
19+
cangjie_version: 1.0.5
2020

2121
steps:
2222
- name: Checkout
2323
uses: actions/checkout@v4
2424

2525
- name: Setup Cangjie
26-
uses: Zxilly/setup-cangjie@v2.1.0
26+
uses: Zxilly/setup-cangjie@d932a790bbb53ff47ead0609829d91a1e831a440
2727
with:
2828
channel: lts
2929
version: ${{ matrix.cangjie_version }}

License.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT No Attribution
22

3-
Copyright (c) 2025 causerp
3+
Copyright (c) 2026 causerp
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

cjpm.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cjc-version = "1.0.0"
33
name = "cenum"
44
description = "C enum macro"
5-
version = "1.1.1"
5+
version = "1.1.2"
66
target-dir = ""
77
src-dir = ""
88
output-type = "dynamic"

src/common.cj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ import std.math.abs
66
77
// cjlint-ignore -start !G.FUN.01 function should be less than 50 lines of code
88

9+
/**
10+
* @brief Generates the beginning block of the enum struct.
11+
*
12+
* Constructs the struct definition, constructors, and operator overloads
13+
* based on the provided parameters.
14+
*
15+
* @param visibility The access modifier token (e.g., `public`, `private`).
16+
* @param name The name of the enum struct.
17+
* @param valueType The underlying integer type token (e.g., `Int32`, `UInt8`).
18+
* @param addFlagOps If `true`, adds bitwise operators for flag enums.
19+
* @param ffi_c If `true`, generates a C-compatible struct (without protocols like Hashable).
20+
* @return A `Tokens` object containing the generated struct definition header.
21+
*/
922
private func enumBlockBegin(visibility: Token, name: Token, valueType: Token, addFlagOps!: Bool, ffi_c!: Bool): Tokens {
1023
let block = quote()
1124
if (ffi_c) {
@@ -95,6 +108,16 @@ private func enumBlockBegin(visibility: Token, name: Token, valueType: Token, ad
95108
return block
96109
}
97110

111+
/**
112+
* @brief Parses the value assigned to an enum member.
113+
*
114+
* Analyzes the tokens following an identifier to determine the enum's value.
115+
* If no assignment is found, it uses the `currentEnumIndex`.
116+
*
117+
* @param currentEnumIndex The current auto-incremented index.
118+
* @param tokenIt An iterator over the tokens.
119+
* @return A tuple containing the tokens representing the value and the new index.
120+
*/
98121
private func parseEnumValue(currentEnumIndex: Int32, tokenIt: Iterator<Token>): (Tokens, Int32) {
99122
let valueTokens = quote()
100123
var hasAssign = false
@@ -130,6 +153,19 @@ private const ERROR_MSG = "Invalid CEnum syntax"
130153
private const ERROR_HINT = "Syntax: CEnum[<Private|public|..> <Identifier> <type>]()\n" +
131154
"Example: CEnum[Seek](...), CEnum[public Seek](...), CEnum[public Seek UInt32](...)"
132155

156+
/**
157+
* @brief Generates the full definition of a C-style enum.
158+
*
159+
* This is the core logic function that processes attribute and input tokens
160+
* to produce the enum struct and its static members.
161+
*
162+
* @param attrTokens Tokens containing visibility, name, and type information.
163+
* @param inputTokens Tokens containing the enum member identifiers.
164+
* @param flagEnum If `true`, treats the enum as a bitfield (doubling values for auto-increment).
165+
* @param startValue The starting value for the first enum member.
166+
* @param ffi_c If `true`, generates a C-compatible struct.
167+
* @return The generated `Tokens` representing the complete enum definition.
168+
*/
133169
func makeEnum(attrTokens: Tokens, inputTokens: Tokens, flagEnum!: Bool = false, startValue!: Int32 = 0,
134170
ffi_c!: Bool = false): Tokens {
135171
var visibility = Token(INTERNAL)

src/enums.cj

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,106 @@ macro package cenum
22

33
import std.ast.Tokens
44
5+
/**
6+
* @brief Defines a standard enumeration.
7+
*
8+
* Generates an enum struct that implements `Hashable` and `Comparable`.
9+
* Values auto-increment starting from 0.
10+
*
11+
* @param attrTokens Tokens specifying visibility, name, and optional type.
12+
* @param inputTokens Tokens specifying the enum members.
13+
*/
514
public macro Enum(attrTokens: Tokens, inputTokens: Tokens) {
615
makeEnum(attrTokens, inputTokens)
716
}
817

18+
/**
19+
* @brief Defines a C-compatible standard enumeration.
20+
*
21+
* Generates a C-compatible struct (`@C`) for use with FFI.
22+
* Values auto-increment starting from 0.
23+
*
24+
* @param attrTokens Tokens specifying visibility, name, and optional type.
25+
* @param inputTokens Tokens specifying the enum members.
26+
*/
927
public macro CEnum(attrTokens: Tokens, inputTokens: Tokens) {
1028
makeEnum(attrTokens, inputTokens, ffi_c: true)
1129
}
1230

31+
/**
32+
* @brief Defines a flag (bitfield) enumeration.
33+
*
34+
* Generates an enum struct with bitwise operators (`|`, `&`, `^`, etc.).
35+
* Values auto-increment using bit shifting starting from 0 (0, 1, 2, 4, ...).
36+
*
37+
* @param attrTokens Tokens specifying visibility, name, and optional type.
38+
* @param inputTokens Tokens specifying the enum members.
39+
*/
1340
public macro FlagEnum(attrTokens: Tokens, inputTokens: Tokens) {
1441
makeEnum(attrTokens, inputTokens, flagEnum: true)
1542
}
1643

44+
/**
45+
* @brief Defines a flag (bitfield) enumeration starting at 0.
46+
*
47+
* Similar to `FlagEnum`, explicitly starts the sequence at 0.
48+
* Values: 0, 1, 2, 4, ...
49+
*
50+
* @param attrTokens Tokens specifying visibility, name, and optional type.
51+
* @param inputTokens Tokens specifying the enum members.
52+
*/
1753
public macro FlagEnum0(attrTokens: Tokens, inputTokens: Tokens) {
1854
makeEnum(attrTokens, inputTokens, flagEnum: true)
1955
}
2056

57+
/**
58+
* @brief Defines a flag (bitfield) enumeration starting at 1.
59+
*
60+
* Generates a flag enum where the first value is 1.
61+
* Values: 1, 2, 4, 8, ...
62+
*
63+
* @param attrTokens Tokens specifying visibility, name, and optional type.
64+
* @param inputTokens Tokens specifying the enum members.
65+
*/
2166
public macro FlagEnum1(attrTokens: Tokens, inputTokens: Tokens) {
2267
makeEnum(attrTokens, inputTokens, flagEnum: true, startValue: 1)
2368
}
2469

70+
/**
71+
* @brief Defines a C-compatible flag (bitfield) enumeration.
72+
*
73+
* Generates a C-compatible struct (`@C`) with bitwise operators.
74+
* Values auto-increment starting from 0.
75+
*
76+
* @param attrTokens Tokens specifying visibility, name, and optional type.
77+
* @param inputTokens Tokens specifying the enum members.
78+
*/
2579
public macro CFlagEnum(attrTokens: Tokens, inputTokens: Tokens) {
2680
makeEnum(attrTokens, inputTokens, flagEnum: true, ffi_c: true)
2781
}
2882

83+
/**
84+
* @brief Defines a C-compatible flag (bitfield) enumeration starting at 0.
85+
*
86+
* Generates a C-compatible struct (`@C`) with bitwise operators.
87+
* Values: 0, 1, 2, 4, ...
88+
*
89+
* @param attrTokens Tokens specifying visibility, name, and optional type.
90+
* @param inputTokens Tokens specifying the enum members.
91+
*/
2992
public macro CFlagEnum0(attrTokens: Tokens, inputTokens: Tokens) {
3093
makeEnum(attrTokens, inputTokens, flagEnum: true, ffi_c: true)
3194
}
3295

96+
/**
97+
* @brief Defines a C-compatible flag (bitfield) enumeration starting at 1.
98+
*
99+
* Generates a C-compatible struct (`@C`) with bitwise operators.
100+
* Values: 1, 2, 4, 8, ...
101+
*
102+
* @param attrTokens Tokens specifying visibility, name, and optional type.
103+
* @param inputTokens Tokens specifying the enum members.
104+
*/
33105
public macro CFlagEnum1(attrTokens: Tokens, inputTokens: Tokens) {
34106
makeEnum(attrTokens, inputTokens, flagEnum: true, startValue: 1, ffi_c: true)
35107
}

0 commit comments

Comments
 (0)