textbringer-tree-sitter 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CLAUDE.md +8 -0
- data/lib/textbringer/tree_sitter/node_maps/ruby.rb +4 -79
- data/lib/textbringer/tree_sitter/node_maps/sql.rb +14 -21
- data/lib/textbringer/tree_sitter/version.rb +1 -1
- data/lib/textbringer/tree_sitter_adapter.rb +7 -5
- data/samples/Sample.java +289 -0
- data/samples/sample.c +176 -0
- data/samples/sample.cob +174 -0
- data/samples/sample.cr +321 -0
- data/samples/sample.cs +273 -0
- data/samples/sample.ex +307 -0
- data/samples/sample.groovy +258 -0
- data/samples/sample.haml +154 -0
- data/samples/sample.html +168 -0
- data/samples/sample.js +266 -0
- data/samples/sample.json +68 -0
- data/samples/sample.pas +276 -0
- data/samples/sample.php +303 -0
- data/samples/sample.py +290 -0
- data/samples/sample.rb +160 -0
- data/samples/sample.rs +391 -0
- data/samples/sample.sh +142 -0
- data/samples/sample.sql +352 -0
- data/samples/sample.swift +482 -0
- data/samples/sample.tf +150 -0
- data/samples/sample.yaml +182 -0
- metadata +22 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 28756ea2a173ddd14d86a892280b4bb9aef8b18c092fc2f406a447bf45d409da
|
|
4
|
+
data.tar.gz: e81b17e1b08b48f2ce5220d750d0431311a7ecd6463c37007f928fd515258c0d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 75175a3f5529504d9318c2667c5bfbc0cfa368dc723da8b3ef3384b51db0d65b56eb6308d761ec4461df3caef9e89465af28d29fd622c94d6fa011206d98c8e4
|
|
7
|
+
data.tar.gz: 58e279671e32eba8b409daf1c7def3662de72abe3dadf5ecf39333d71679a6ac41b4e757ecb1fa93e60e7cfad236e5facb8591203a1514a34bfd8360fb222976
|
data/CLAUDE.md
CHANGED
|
@@ -150,6 +150,14 @@ c++ -shared -fPIC -O2 -std=c++14 -Isrc src/parser.c src/scanner.cc -o libtree-si
|
|
|
150
150
|
cp libtree-sitter-hcl.dylib ~/.textbringer/parsers/darwin-arm64/
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
+
## リリース手順
|
|
154
|
+
|
|
155
|
+
1. `lib/textbringer/tree_sitter/version.rb` のバージョンを更新
|
|
156
|
+
2. PR を作成してマージ
|
|
157
|
+
3. main でタグを打つ(`git tag vX.Y.Z && git push origin vX.Y.Z`)
|
|
158
|
+
4. **gem push は GitHub Actions が自動で行う**(手動で `gem push` する必要はない)
|
|
159
|
+
5. GitHub Release のリリースノートを整備する(CI が自動作成するが、内容は手動で書き換える)
|
|
160
|
+
|
|
153
161
|
## 参考実装
|
|
154
162
|
|
|
155
163
|
- [textbringer-rouge](https://github.com/yancya/textbringer-rouge) - Window モンキーパッチの実装パターン
|
|
@@ -8,10 +8,7 @@ module Textbringer
|
|
|
8
8
|
RUBY_FEATURES = {
|
|
9
9
|
comment: %i[comment],
|
|
10
10
|
string: %i[
|
|
11
|
-
string
|
|
12
11
|
string_content
|
|
13
|
-
string_array
|
|
14
|
-
symbol_array
|
|
15
12
|
heredoc_content
|
|
16
13
|
heredoc_body
|
|
17
14
|
heredoc_beginning
|
|
@@ -23,10 +20,7 @@ module Textbringer
|
|
|
23
20
|
bare_string
|
|
24
21
|
escape_sequence
|
|
25
22
|
character
|
|
26
|
-
subshell
|
|
27
23
|
regex
|
|
28
|
-
chained_string
|
|
29
|
-
interpolation
|
|
30
24
|
uninterpreted
|
|
31
25
|
],
|
|
32
26
|
keyword: %i[
|
|
@@ -74,87 +68,18 @@ module Textbringer
|
|
|
74
68
|
file
|
|
75
69
|
line
|
|
76
70
|
],
|
|
77
|
-
function_name: %i[
|
|
78
|
-
method
|
|
79
|
-
singleton_method
|
|
80
|
-
call
|
|
81
|
-
],
|
|
71
|
+
function_name: %i[],
|
|
82
72
|
variable: %i[
|
|
83
73
|
identifier
|
|
84
74
|
instance_variable
|
|
85
75
|
class_variable
|
|
86
76
|
global_variable
|
|
87
77
|
],
|
|
88
|
-
type: %i[
|
|
89
|
-
operator: %i[
|
|
90
|
-
binary
|
|
91
|
-
unary
|
|
92
|
-
assignment
|
|
93
|
-
operator
|
|
94
|
-
operator_assignment
|
|
95
|
-
],
|
|
78
|
+
type: %i[],
|
|
79
|
+
operator: %i[],
|
|
96
80
|
punctuation: %i[],
|
|
97
81
|
builtin: %i[nil true false],
|
|
98
|
-
property: %i[
|
|
99
|
-
hash
|
|
100
|
-
array
|
|
101
|
-
block
|
|
102
|
-
hash_pattern
|
|
103
|
-
array_pattern
|
|
104
|
-
find_pattern
|
|
105
|
-
hash_splat_argument
|
|
106
|
-
hash_splat_nil
|
|
107
|
-
hash_splat_parameter
|
|
108
|
-
splat_argument
|
|
109
|
-
splat_parameter
|
|
110
|
-
rest_assignment
|
|
111
|
-
pair
|
|
112
|
-
argument_list
|
|
113
|
-
block_argument
|
|
114
|
-
block_body
|
|
115
|
-
block_parameter
|
|
116
|
-
block_parameters
|
|
117
|
-
keyword_parameter
|
|
118
|
-
keyword_pattern
|
|
119
|
-
optional_parameter
|
|
120
|
-
forward_argument
|
|
121
|
-
forward_parameter
|
|
122
|
-
destructured_parameter
|
|
123
|
-
left_assignment_list
|
|
124
|
-
right_assignment_list
|
|
125
|
-
destructured_left_assignment
|
|
126
|
-
lambda_parameters
|
|
127
|
-
method_parameters
|
|
128
|
-
parenthesized_pattern
|
|
129
|
-
pattern
|
|
130
|
-
alternative_pattern
|
|
131
|
-
expression_reference_pattern
|
|
132
|
-
variable_reference_pattern
|
|
133
|
-
test_pattern
|
|
134
|
-
body_statement
|
|
135
|
-
parenthesized_statements
|
|
136
|
-
program
|
|
137
|
-
scope_resolution
|
|
138
|
-
element_reference
|
|
139
|
-
setter
|
|
140
|
-
conditional
|
|
141
|
-
if_modifier
|
|
142
|
-
unless_modifier
|
|
143
|
-
while_modifier
|
|
144
|
-
until_modifier
|
|
145
|
-
rescue_modifier
|
|
146
|
-
case_match
|
|
147
|
-
in_clause
|
|
148
|
-
match_pattern
|
|
149
|
-
if_guard
|
|
150
|
-
unless_guard
|
|
151
|
-
do_block
|
|
152
|
-
begin_block
|
|
153
|
-
end_block
|
|
154
|
-
empty_statement
|
|
155
|
-
exception_variable
|
|
156
|
-
exceptions
|
|
157
|
-
]
|
|
82
|
+
property: %i[]
|
|
158
83
|
}.freeze
|
|
159
84
|
|
|
160
85
|
# Feature → Face の展開
|
|
@@ -6,44 +6,37 @@ module Textbringer
|
|
|
6
6
|
SQL_FEATURES = {
|
|
7
7
|
keyword: %i[
|
|
8
8
|
SELECT FROM WHERE JOIN LEFT RIGHT INNER OUTER FULL NATURAL CROSS
|
|
9
|
-
ON AND OR NOT IN IS NULL BETWEEN
|
|
9
|
+
ON AND OR NOT IN IS NULL BETWEEN
|
|
10
10
|
INSERT INTO VALUES UPDATE SET DELETE
|
|
11
11
|
CREATE DROP ALTER TABLE INDEX VIEW SCHEMA DATABASE
|
|
12
|
-
PRIMARY_KEY
|
|
12
|
+
PRIMARY_KEY UNIQUE CHECK CONSTRAINT REFERENCES
|
|
13
13
|
GROUP_BY ORDER_BY HAVING LIMIT OFFSET FETCH
|
|
14
14
|
UNION INTERSECT EXCEPT ALL DISTINCT
|
|
15
15
|
AS ASC DESC NULLS FIRST LAST
|
|
16
16
|
CASE WHEN THEN ELSE END
|
|
17
17
|
BEGIN COMMIT ROLLBACK TRANSACTION
|
|
18
|
-
IF EXISTS
|
|
18
|
+
IF EXISTS
|
|
19
19
|
TRUE FALSE DEFAULT
|
|
20
20
|
WITH RECURSIVE
|
|
21
21
|
GRANT REVOKE PRIVILEGES
|
|
22
22
|
RETURNS RETURN FUNCTION PROCEDURE
|
|
23
23
|
FOR EACH ROW TRIGGER
|
|
24
|
-
CASCADE RESTRICT
|
|
24
|
+
CASCADE RESTRICT
|
|
25
25
|
VOLATILE STABLE IMMUTABLE
|
|
26
26
|
DECLARE EXECUTE
|
|
27
|
+
like
|
|
27
28
|
],
|
|
28
29
|
comment: %i[comment line_comment block_comment],
|
|
29
|
-
string: %i[
|
|
30
|
+
string: %i[content],
|
|
30
31
|
number: %i[number],
|
|
31
|
-
type: %i[
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
UUID BYTEA
|
|
40
|
-
SERIAL BIGSERIAL
|
|
41
|
-
],
|
|
42
|
-
function_name: %i[function_call],
|
|
43
|
-
variable: %i[identifier dotted_name],
|
|
44
|
-
operator: %i[binary_operator comparison_operator],
|
|
45
|
-
punctuation: %i[delimiter],
|
|
46
|
-
constant: %i[boolean_expression],
|
|
32
|
+
type: %i[],
|
|
33
|
+
function_name: %i[],
|
|
34
|
+
variable: %i[identifier],
|
|
35
|
+
operator: %i[],
|
|
36
|
+
punctuation: %i[],
|
|
37
|
+
constant: %i[],
|
|
38
|
+
builtin: %i[],
|
|
39
|
+
property: %i[],
|
|
47
40
|
}.freeze
|
|
48
41
|
|
|
49
42
|
SQL = SQL_FEATURES.flat_map { |face, nodes|
|
|
@@ -166,11 +166,13 @@ module Textbringer
|
|
|
166
166
|
end
|
|
167
167
|
|
|
168
168
|
def visit_node(node, &block)
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
169
|
+
if node.child_count == 0
|
|
170
|
+
block.call(node, node.start_byte, node.end_byte)
|
|
171
|
+
else
|
|
172
|
+
node.child_count.times do |i|
|
|
173
|
+
child = node.child(i)
|
|
174
|
+
visit_node(child, &block) if child
|
|
175
|
+
end
|
|
174
176
|
end
|
|
175
177
|
end
|
|
176
178
|
|
data/samples/Sample.java
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// Line comment
|
|
2
|
+
/* Block comment */
|
|
3
|
+
/**
|
|
4
|
+
* Javadoc comment
|
|
5
|
+
* @author Sample
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
package com.example.sample;
|
|
9
|
+
|
|
10
|
+
import java.util.List;
|
|
11
|
+
import java.util.ArrayList;
|
|
12
|
+
import java.util.Map;
|
|
13
|
+
import java.util.HashMap;
|
|
14
|
+
import java.util.Optional;
|
|
15
|
+
import java.util.stream.Collectors;
|
|
16
|
+
|
|
17
|
+
// --- Enum ---
|
|
18
|
+
enum Color {
|
|
19
|
+
RED("red"),
|
|
20
|
+
GREEN("green"),
|
|
21
|
+
BLUE("blue");
|
|
22
|
+
|
|
23
|
+
private final String name;
|
|
24
|
+
|
|
25
|
+
Color(String name) {
|
|
26
|
+
this.name = name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public String getName() {
|
|
30
|
+
return name;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// --- Interface ---
|
|
35
|
+
interface Greeter {
|
|
36
|
+
String greet(String name);
|
|
37
|
+
|
|
38
|
+
default String farewell(String name) {
|
|
39
|
+
return "Goodbye, " + name + "!";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static Greeter create() {
|
|
43
|
+
return name -> "Hello, " + name + "!";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// --- Abstract class ---
|
|
48
|
+
abstract class Animal {
|
|
49
|
+
protected String name;
|
|
50
|
+
|
|
51
|
+
public Animal(String name) {
|
|
52
|
+
this.name = name;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public abstract String speak();
|
|
56
|
+
|
|
57
|
+
@Override
|
|
58
|
+
public String toString() {
|
|
59
|
+
return name + " says " + speak();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// --- Record (Java 16+) ---
|
|
64
|
+
record Point(double x, double y) {
|
|
65
|
+
double distanceTo(Point other) {
|
|
66
|
+
return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- Sealed class (Java 17+) ---
|
|
71
|
+
sealed class Shape permits Circle, Rectangle {
|
|
72
|
+
public double area() { return 0; }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
final class Circle extends Shape {
|
|
76
|
+
private final double radius;
|
|
77
|
+
|
|
78
|
+
Circle(double radius) {
|
|
79
|
+
this.radius = radius;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@Override
|
|
83
|
+
public double area() {
|
|
84
|
+
return Math.PI * radius * radius;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
final class Rectangle extends Shape {
|
|
89
|
+
private final double width, height;
|
|
90
|
+
|
|
91
|
+
Rectangle(double width, double height) {
|
|
92
|
+
this.width = width;
|
|
93
|
+
this.height = height;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Override
|
|
97
|
+
public double area() {
|
|
98
|
+
return width * height;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// --- Generic class ---
|
|
103
|
+
class Container<T extends Comparable<T>> {
|
|
104
|
+
private final List<T> items = new ArrayList<>();
|
|
105
|
+
|
|
106
|
+
public void add(T item) {
|
|
107
|
+
items.add(item);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public Optional<T> max() {
|
|
111
|
+
return items.stream().max(T::compareTo);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// --- Main class ---
|
|
116
|
+
public class Sample {
|
|
117
|
+
// --- Constants ---
|
|
118
|
+
public static final int MAX_SIZE = 100;
|
|
119
|
+
private static final String GREETING = "Hello";
|
|
120
|
+
|
|
121
|
+
// --- Instance fields ---
|
|
122
|
+
private final String name;
|
|
123
|
+
private int count;
|
|
124
|
+
private volatile boolean running;
|
|
125
|
+
private transient String temp;
|
|
126
|
+
|
|
127
|
+
// --- Constructor ---
|
|
128
|
+
public Sample(String name) {
|
|
129
|
+
this.name = name;
|
|
130
|
+
this.count = 0;
|
|
131
|
+
this.running = true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// --- Methods ---
|
|
135
|
+
public synchronized void increment() {
|
|
136
|
+
count++;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public String getName() {
|
|
140
|
+
return name;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// --- Varargs ---
|
|
144
|
+
public static int sum(int... numbers) {
|
|
145
|
+
int total = 0;
|
|
146
|
+
for (int n : numbers) {
|
|
147
|
+
total += n;
|
|
148
|
+
}
|
|
149
|
+
return total;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// --- Generics method ---
|
|
153
|
+
public static <T> List<T> repeat(T item, int times) {
|
|
154
|
+
List<T> list = new ArrayList<>();
|
|
155
|
+
for (int i = 0; i < times; i++) {
|
|
156
|
+
list.add(item);
|
|
157
|
+
}
|
|
158
|
+
return list;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public static void main(String[] args) {
|
|
162
|
+
// --- Strings ---
|
|
163
|
+
String simple = "Hello, World!";
|
|
164
|
+
String escaped = "tab\tnewline\nnull\0";
|
|
165
|
+
String textBlock = """
|
|
166
|
+
This is a text block
|
|
167
|
+
with "quotes" inside
|
|
168
|
+
and interpolation-like: %s
|
|
169
|
+
""".formatted("value");
|
|
170
|
+
char ch = 'A';
|
|
171
|
+
|
|
172
|
+
// --- Numbers ---
|
|
173
|
+
int dec = 42;
|
|
174
|
+
long big = 1_234_567_890L;
|
|
175
|
+
float f = 3.14f;
|
|
176
|
+
double d = 2.71828;
|
|
177
|
+
int hex = 0xFF;
|
|
178
|
+
int oct = 077;
|
|
179
|
+
int bin = 0b1010;
|
|
180
|
+
|
|
181
|
+
// --- Booleans & Null ---
|
|
182
|
+
boolean flag = true;
|
|
183
|
+
boolean nope = false;
|
|
184
|
+
Object nothing = null;
|
|
185
|
+
|
|
186
|
+
// --- Type inference ---
|
|
187
|
+
var inferred = "inferred";
|
|
188
|
+
var list = new ArrayList<String>();
|
|
189
|
+
|
|
190
|
+
// --- Control flow ---
|
|
191
|
+
if (dec > 0) {
|
|
192
|
+
System.out.println("positive");
|
|
193
|
+
} else if (dec < 0) {
|
|
194
|
+
System.out.println("negative");
|
|
195
|
+
} else {
|
|
196
|
+
System.out.println("zero");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// --- Switch expression (Java 14+) ---
|
|
200
|
+
String result = switch (dec) {
|
|
201
|
+
case 1, 2, 3 -> "small";
|
|
202
|
+
case 42 -> "answer";
|
|
203
|
+
default -> {
|
|
204
|
+
yield "other";
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// --- Loops ---
|
|
209
|
+
for (int i = 0; i < 10; i++) {
|
|
210
|
+
if (i == 5) break;
|
|
211
|
+
if (i == 3) continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
for (String arg : args) {
|
|
215
|
+
System.out.println(arg);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
int j = 10;
|
|
219
|
+
while (j > 0) {
|
|
220
|
+
j--;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
do {
|
|
224
|
+
j++;
|
|
225
|
+
} while (j < 5);
|
|
226
|
+
|
|
227
|
+
// --- Operators ---
|
|
228
|
+
int sum = 1 + 2;
|
|
229
|
+
int diff = 5 - 3;
|
|
230
|
+
int prod = 4 * 2;
|
|
231
|
+
int quot = 10 / 3;
|
|
232
|
+
int mod = 10 % 3;
|
|
233
|
+
boolean logic = true && false || !true;
|
|
234
|
+
int bitAnd = 0xFF & 0x0F;
|
|
235
|
+
int ternary = sum > 0 ? sum : -sum;
|
|
236
|
+
|
|
237
|
+
// --- Instanceof pattern matching (Java 16+) ---
|
|
238
|
+
Object obj = "test";
|
|
239
|
+
if (obj instanceof String s) {
|
|
240
|
+
System.out.println(s.length());
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// --- Exception handling ---
|
|
244
|
+
try {
|
|
245
|
+
throw new RuntimeException("oops");
|
|
246
|
+
} catch (RuntimeException e) {
|
|
247
|
+
System.out.println(e.getMessage());
|
|
248
|
+
} finally {
|
|
249
|
+
System.out.println("cleanup");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// --- Try with resources ---
|
|
253
|
+
try (var resource = new java.io.StringReader("test")) {
|
|
254
|
+
resource.read();
|
|
255
|
+
} catch (Exception e) {
|
|
256
|
+
e.printStackTrace();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// --- Lambda & Streams ---
|
|
260
|
+
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
|
|
261
|
+
List<Integer> evens = numbers.stream()
|
|
262
|
+
.filter(n -> n % 2 == 0)
|
|
263
|
+
.map(n -> n * 2)
|
|
264
|
+
.collect(Collectors.toList());
|
|
265
|
+
|
|
266
|
+
// --- Method reference ---
|
|
267
|
+
numbers.forEach(System.out::println);
|
|
268
|
+
|
|
269
|
+
// --- Map ---
|
|
270
|
+
Map<String, Integer> scores = new HashMap<>();
|
|
271
|
+
scores.put("Alice", 95);
|
|
272
|
+
scores.put("Bob", 87);
|
|
273
|
+
scores.getOrDefault("Charlie", 0);
|
|
274
|
+
|
|
275
|
+
// --- Optional ---
|
|
276
|
+
Optional<String> opt = Optional.of("value");
|
|
277
|
+
opt.ifPresent(System.out::println);
|
|
278
|
+
String val = opt.orElse("default");
|
|
279
|
+
|
|
280
|
+
// --- Assert ---
|
|
281
|
+
assert dec == 42 : "Expected 42";
|
|
282
|
+
|
|
283
|
+
// --- Synchronized block ---
|
|
284
|
+
Object lock = new Object();
|
|
285
|
+
synchronized (lock) {
|
|
286
|
+
System.out.println("synchronized");
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
data/samples/sample.c
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/* Block comment */
|
|
2
|
+
// Line comment
|
|
3
|
+
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
#include <stdbool.h>
|
|
8
|
+
|
|
9
|
+
/* --- Preprocessor --- */
|
|
10
|
+
#define MAX_SIZE 100
|
|
11
|
+
#define SQUARE(x) ((x) * (x))
|
|
12
|
+
|
|
13
|
+
#ifdef DEBUG
|
|
14
|
+
#define LOG(msg) printf("DEBUG: %s\n", msg)
|
|
15
|
+
#else
|
|
16
|
+
#define LOG(msg)
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#ifndef HEADER_GUARD
|
|
20
|
+
#define HEADER_GUARD
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
/* --- Types --- */
|
|
24
|
+
typedef unsigned long ulong;
|
|
25
|
+
|
|
26
|
+
typedef struct {
|
|
27
|
+
char name[MAX_SIZE];
|
|
28
|
+
int age;
|
|
29
|
+
float score;
|
|
30
|
+
} Person;
|
|
31
|
+
|
|
32
|
+
typedef union {
|
|
33
|
+
int i;
|
|
34
|
+
float f;
|
|
35
|
+
char c;
|
|
36
|
+
} Value;
|
|
37
|
+
|
|
38
|
+
typedef enum {
|
|
39
|
+
RED = 0,
|
|
40
|
+
GREEN = 1,
|
|
41
|
+
BLUE = 2
|
|
42
|
+
} Color;
|
|
43
|
+
|
|
44
|
+
/* --- Constants --- */
|
|
45
|
+
static const int ZERO = 0;
|
|
46
|
+
const char *NULL_STR = NULL;
|
|
47
|
+
|
|
48
|
+
/* --- Function declarations --- */
|
|
49
|
+
int add(int a, int b);
|
|
50
|
+
void greet(const char *name);
|
|
51
|
+
static inline int max(int a, int b);
|
|
52
|
+
Person *create_person(const char *name, int age);
|
|
53
|
+
|
|
54
|
+
/* --- Function definitions --- */
|
|
55
|
+
int add(int a, int b) {
|
|
56
|
+
return a + b;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void greet(const char *name) {
|
|
60
|
+
printf("Hello, %s!\n", name);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static inline int max(int a, int b) {
|
|
64
|
+
return (a > b) ? a : b;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
Person *create_person(const char *name, int age) {
|
|
68
|
+
Person *p = (Person *)malloc(sizeof(Person));
|
|
69
|
+
if (p == NULL) {
|
|
70
|
+
return NULL;
|
|
71
|
+
}
|
|
72
|
+
strncpy(p->name, name, MAX_SIZE - 1);
|
|
73
|
+
p->name[MAX_SIZE - 1] = '\0';
|
|
74
|
+
p->age = age;
|
|
75
|
+
p->score = 0.0f;
|
|
76
|
+
return p;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* --- Main --- */
|
|
80
|
+
int main(int argc, char *argv[]) {
|
|
81
|
+
/* Strings */
|
|
82
|
+
char *str = "Hello, World!";
|
|
83
|
+
char ch = 'A';
|
|
84
|
+
char escaped[] = "tab\tnewline\nnull\0";
|
|
85
|
+
|
|
86
|
+
/* Numbers */
|
|
87
|
+
int dec = 42;
|
|
88
|
+
int hex = 0xFF;
|
|
89
|
+
int oct = 077;
|
|
90
|
+
int bin = 0b1010;
|
|
91
|
+
long big = 1234567890L;
|
|
92
|
+
float f = 3.14f;
|
|
93
|
+
double d = 2.71828;
|
|
94
|
+
|
|
95
|
+
/* Booleans (C99) */
|
|
96
|
+
bool flag = true;
|
|
97
|
+
bool nope = false;
|
|
98
|
+
|
|
99
|
+
/* Pointers */
|
|
100
|
+
int *ptr = &dec;
|
|
101
|
+
int **dbl_ptr = &ptr;
|
|
102
|
+
void *void_ptr = NULL;
|
|
103
|
+
|
|
104
|
+
/* Arrays */
|
|
105
|
+
int arr[5] = {1, 2, 3, 4, 5};
|
|
106
|
+
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
|
|
107
|
+
|
|
108
|
+
/* Control flow */
|
|
109
|
+
if (dec > 0) {
|
|
110
|
+
printf("positive\n");
|
|
111
|
+
} else if (dec < 0) {
|
|
112
|
+
printf("negative\n");
|
|
113
|
+
} else {
|
|
114
|
+
printf("zero\n");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
switch (arr[0]) {
|
|
118
|
+
case 1:
|
|
119
|
+
printf("one\n");
|
|
120
|
+
break;
|
|
121
|
+
case 2:
|
|
122
|
+
printf("two\n");
|
|
123
|
+
break;
|
|
124
|
+
default:
|
|
125
|
+
printf("other\n");
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* Loops */
|
|
130
|
+
for (int i = 0; i < 5; i++) {
|
|
131
|
+
if (i == 3) continue;
|
|
132
|
+
printf("%d\n", arr[i]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
int j = 0;
|
|
136
|
+
while (j < 10) {
|
|
137
|
+
j++;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
do {
|
|
141
|
+
j--;
|
|
142
|
+
} while (j > 0);
|
|
143
|
+
|
|
144
|
+
/* Operators */
|
|
145
|
+
int sum = 1 + 2;
|
|
146
|
+
int diff = 5 - 3;
|
|
147
|
+
int prod = 4 * 2;
|
|
148
|
+
int quot = 10 / 3;
|
|
149
|
+
int mod = 10 % 3;
|
|
150
|
+
int neg = -sum;
|
|
151
|
+
int bit_and = 0xFF & 0x0F;
|
|
152
|
+
int bit_or = 0x10 | 0x01;
|
|
153
|
+
int bit_xor = 0xFF ^ 0x0F;
|
|
154
|
+
int bit_not = ~0;
|
|
155
|
+
int lshift = 1 << 4;
|
|
156
|
+
int rshift = 16 >> 2;
|
|
157
|
+
bool logic = (1 > 0) && (2 > 1) || !(3 > 4);
|
|
158
|
+
int ternary = (sum > 0) ? sum : -sum;
|
|
159
|
+
int size = sizeof(Person);
|
|
160
|
+
|
|
161
|
+
/* Struct usage */
|
|
162
|
+
Person *alice = create_person("Alice", 30);
|
|
163
|
+
printf("Name: %s, Age: %d\n", alice->name, alice->age);
|
|
164
|
+
free(alice);
|
|
165
|
+
|
|
166
|
+
/* Enum usage */
|
|
167
|
+
Color c = RED;
|
|
168
|
+
if (c == GREEN) {
|
|
169
|
+
printf("green\n");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* Goto (rare but valid) */
|
|
173
|
+
goto end;
|
|
174
|
+
end:
|
|
175
|
+
return 0;
|
|
176
|
+
}
|