Skip to content

Commit 8d1a779

Browse files
committed
reformat examples
1 parent d56be66 commit 8d1a779

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

examples/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
Small programs that showcase `cj`:
44

5-
- `simple.c`: minimal program(nop & ret).
6-
- `add.c`: adds a constant to the first argument and returns it; demonstrates
7-
register operands and constants.
8-
- `fibonacci.c`: full control-flow example(labels, branches, loops) emitted
5+
- `simple.c`: minimal
6+
program(nop &ret)
7+
.- `add.c`: adds a constant to the first argument and returns it;
8+
demonstrates register operands and constants.- `fibonacci.c`: full control -
9+
flow example(labels, branches, loops) emitted
910
with the low-level API.
1011
- `hl_fibonacci.c` – fibonacci again, but built entirely with the builder
1112
helpers.

examples/fibonacci.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ typedef int (*fib_fn)(int);
2727
// Reference implementation for comparison
2828
int fib_c(int n)
2929
{
30-
if (n <= 1)
31-
return n;
30+
if (n <= 1) return n;
3231
int a = 0, b = 1;
3332
for (int i = 2; i <= n; i++)
3433
{

examples/hl_fibonacci.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ typedef int (*fib_fn)(int);
66

77
static int fib_c(int n)
88
{
9-
if (n <= 1)
10-
return n;
9+
if (n <= 1) return n;
1110
int a = 0;
1211
int b = 1;
1312
for (int i = 2; i <= n; ++i)

examples/minilang.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ typedef struct
7272

7373
static void next_token(lexer *lx)
7474
{
75-
while (*lx->cur && isspace((unsigned char)*lx->cur))
76-
lx->cur++;
75+
while (*lx->cur && isspace((unsigned char)*lx->cur)) lx->cur++;
7776
char c = *lx->cur;
7877
if (!c)
7978
{
@@ -99,8 +98,7 @@ static void next_token(lexer *lx)
9998
lx->tok.kind = TOK_NUMBER;
10099
lx->tok.value = (int)val;
101100
size_t len = (size_t)(end - lx->cur);
102-
if (len >= sizeof(lx->tok.text))
103-
len = sizeof(lx->tok.text) - 1;
101+
if (len >= sizeof(lx->tok.text)) len = sizeof(lx->tok.text) - 1;
104102
memcpy(lx->tok.text, lx->cur, len);
105103
lx->tok.text[len] = '\0';
106104
lx->cur = end;
@@ -109,11 +107,9 @@ static void next_token(lexer *lx)
109107
if (isalpha((unsigned char)c))
110108
{
111109
const char *start = lx->cur;
112-
while (*lx->cur && (isalnum((unsigned char)*lx->cur) || *lx->cur == '_'))
113-
lx->cur++;
110+
while (*lx->cur && (isalnum((unsigned char)*lx->cur) || *lx->cur == '_')) lx->cur++;
114111
size_t len = (size_t)(lx->cur - start);
115-
if (len >= sizeof(lx->tok.text))
116-
len = sizeof(lx->tok.text) - 1;
112+
if (len >= sizeof(lx->tok.text)) len = sizeof(lx->tok.text) - 1;
117113
memcpy(lx->tok.text, start, len);
118114
lx->tok.text[len] = '\0';
119115
lx->tok.kind = TOK_IDENT;
@@ -261,8 +257,7 @@ static int find_function(function *fns, int count, const char *name)
261257
{
262258
for (int i = 0; i < count; i++)
263259
{
264-
if (strcmp(fns[i].name, name) == 0)
265-
return i;
260+
if (strcmp(fns[i].name, name) == 0) return i;
266261
}
267262
return -1;
268263
}
@@ -379,14 +374,12 @@ int main(void)
379374
resolve_calls(&arena, functions, function_count);
380375

381376
cj_ctx *cj = create_cj_ctx();
382-
for (int i = 0; i < function_count; i++)
383-
functions[i].entry = cj_create_label(cj);
377+
for (int i = 0; i < function_count; i++) functions[i].entry = cj_create_label(cj);
384378

385379
codegen cg = {.cj = cj, .functions = functions};
386380
emit_function(&cg, &functions[main_idx]);
387381
for (int i = 0; i < function_count; i++)
388-
if (i != main_idx)
389-
emit_function(&cg, &functions[i]);
382+
if (i != main_idx) emit_function(&cg, &functions[i]);
390383

391384
cj_fn module = create_cj_fn(cj);
392385
if (!module)

0 commit comments

Comments
 (0)