Skip to content

Commit c5d839f

Browse files
author
thradams
committed
format option
1 parent e5af5a9 commit c5d839f

File tree

16 files changed

+5819
-3781
lines changed

16 files changed

+5819
-3781
lines changed

manual.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Options
1414
-target=standard Output target C standard (c99, c11, c2x, cxx).
1515
-std=standard Assume that the input sources are for standard (c99, c11, c2x, cxx).
1616
-n Check naming conventions (it is hardcoded for it own naming convention)
17+
-fi Format input (format before language convertion)
18+
-fo Format output (format after language convertion, result parsed again)
1719
```
1820
The ouput dir is `./out`
1921

src/Web/cake.js

Lines changed: 4921 additions & 3684 deletions
Large diffs are not rendered by default.

src/Web/manual.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ <h1 id="toc_0">Command line</h1>
102102
-target=standard Output target C standard (c99, c11, c2x, cxx).
103103
-std=standard Assume that the input sources are for standard (c99, c11, c2x, cxx).
104104
-n Check naming conventions (it is hardcoded for it own naming convention)
105+
-fi Format input (format before language convertion)
106+
-fo Format output (format after language convertion, result parsed again)
105107
</code></pre>
106108

107109
<p>The ouput dir is <code>./out</code></p>

src/build.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
" type.h " \
2626
" pre_expressions.h " \
2727
" expressions.h " \
28-
" visit.h "
28+
" visit.h " \
29+
" formatvisit.h "
2930

3031
#define SOURCE_FILES \
3132
" token.c " \
@@ -41,7 +42,8 @@
4142
" type.c " \
4243
" parser.c " \
4344
" visit.c " \
44-
" error.c "
45+
" error.c " \
46+
" formatvisit.c "
4547

4648

4749
void compile_cake()

src/file.c

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,25 @@
11

22

3-
struct X {
4-
int i;
5-
};
6-
7-
struct Y {
8-
double d;
9-
};
10-
11-
enum E { A = 1 };
12-
enum E e1;
13-
struct X* F() { return 0; }
3+
#include <stdio.h>
144

155
int main()
166
{
17-
enum E { B } E2;
18-
//static_assert(typeid(e2) == typeid(enum E));
19-
//static_assert(typeid(e2) != typeid(e1));
20-
21-
struct X x;
22-
struct Y y;
23-
24-
static_assert(typeid(x) == typeid(struct X));
25-
static_assert(typeid(x) != typeid(struct Y));
26-
27-
static_assert(typeid(int(double)) != typeid(int()));
28-
int aa[10];
29-
30-
static_assert(typeid(*F()) == typeid(struct X));
31-
static_assert(typeid(&aa) == typeid(int(*)[10]));
32-
33-
int* p = 0;
34-
static_assert(typeid(*(p + 1)) == typeid(int));
35-
36-
static_assert(1 == typeid(int));
37-
38-
static_assert(typeid(main) == typeid(int()));
39-
40-
41-
static_assert(typeid(main) != typeid(int(double)));
42-
static_assert(typeid(main) != typeid(int));
43-
44-
45-
struct X x;
46-
enum E e;
47-
static_assert(typeid(e) == typeid(enum E));
48-
static_assert(typeid(x) == typeid(struct X));
49-
static_assert(typeid(e) != typeid(struct X));
50-
51-
52-
53-
static_assert(1L == typeid(long));
54-
static_assert(1UL == typeid(unsigned long));
55-
static_assert(1ULL == typeid(unsigned long long));
56-
static_assert(A == typeid(int));
57-
static_assert(1.0 == typeid(double));
58-
static_assert(1.0f == typeid(float));
59-
static_assert(1.0L == typeid(long double));
60-
7+
8+
do
9+
{
10+
FILE* f = fopen("in.txt", "r");
11+
if (f == NULL) break;
12+
defer fclose(f);
13+
14+
FILE* f2 = fopen("out.txt", "w");
15+
if (f2 == NULL) break;
16+
defer fclose(f2);
17+
18+
//...
19+
20+
/*success here*/
21+
} while (0);
22+
23+
6124
}
25+

src/formatvisit.c

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
#include "formatvisit.h"
2+
#include <assert.h>
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
void ajust_line_and_identation(struct token* token, struct format_visit_ctx* ctx)
8+
{
9+
/*
10+
* Before this token we must have a identation and before identation a new line.
11+
* If we don't have it we need to insert.
12+
*/
13+
14+
if (token && token->level == 0)
15+
{
16+
struct token* previous_token = token->prev;
17+
if (previous_token)
18+
{
19+
if (previous_token->type == TK_BLANKS)
20+
{
21+
char blanks[50] = {0};
22+
if (ctx->identation > 0)
23+
snprintf(blanks, sizeof blanks, "%*c", (ctx->identation * 4), ' ');
24+
25+
/*only adjust the number of spaces*/
26+
free(previous_token->lexeme);
27+
previous_token->lexeme = strdup(blanks);
28+
29+
struct token* previous_previous_token =
30+
previous_token->prev;
31+
32+
if (previous_previous_token->type != TK_NEWLINE)
33+
{
34+
struct error error = { 0 };
35+
struct token_list list = tokenizer("\n", NULL, 0, TK_FLAG_NONE, &error);
36+
token_list_insert_after(&ctx->ast.token_list, previous_previous_token, &list);
37+
}
38+
}
39+
else if (previous_token->type != TK_NEWLINE)
40+
{
41+
char blanks[50];
42+
if (ctx->identation > 0)
43+
{
44+
snprintf(blanks, sizeof blanks, "\n%*c", (ctx->identation * 4), ' ');
45+
}
46+
else
47+
{
48+
snprintf(blanks, sizeof blanks, "\n");
49+
}
50+
51+
struct error error = { 0 };
52+
struct token_list list = tokenizer(blanks, NULL, 0, TK_FLAG_NONE, &error);
53+
token_list_insert_after(&ctx->ast.token_list, previous_token, &list);
54+
}
55+
}
56+
}
57+
}
58+
59+
static void format_visit_unlabeled_statement(struct format_visit_ctx* ctx, struct unlabeled_statement* p_unlabeled_statement, struct error* error);
60+
61+
static void format_visit_statement(struct format_visit_ctx* ctx, struct statement* p_statement, struct error* error)
62+
{
63+
if (p_statement->labeled_statement)
64+
{
65+
//format_visit_labeled_statement(ctx, p_statement->labeled_statement, error);
66+
}
67+
else if (p_statement->unlabeled_statement)
68+
{
69+
format_visit_unlabeled_statement(ctx, p_statement->unlabeled_statement, error);
70+
}
71+
}
72+
73+
74+
75+
static void format_visit_selection_statement(struct format_visit_ctx* ctx, struct selection_statement* p_selection_statement, struct error* error)
76+
{
77+
if (p_selection_statement->secondary_block)
78+
{
79+
80+
ajust_line_and_identation(p_selection_statement->secondary_block->first, ctx);
81+
82+
if (p_selection_statement->secondary_block &&
83+
p_selection_statement->secondary_block->statement &&
84+
p_selection_statement->secondary_block->statement->unlabeled_statement &&
85+
p_selection_statement->secondary_block->statement->unlabeled_statement->primary_block &&
86+
p_selection_statement->secondary_block->statement->unlabeled_statement->primary_block->compound_statement)
87+
{
88+
format_visit_statement(ctx, p_selection_statement->secondary_block->statement, error);
89+
}
90+
else
91+
{
92+
ctx->identation++;
93+
//ajust_line_and_identation(p_selection_statement->secondary_block->first, ctx);
94+
95+
format_visit_statement(ctx, p_selection_statement->secondary_block->statement, error);
96+
ctx->identation--;
97+
}
98+
99+
100+
//ajust_line_and_identation(p_selection_statement->secondary_block->last, ctx);
101+
}
102+
103+
}
104+
105+
static void format_visit_jump_statement(struct format_visit_ctx* ctx, struct jump_statement* p_jump_statement, struct error* error)
106+
{
107+
108+
109+
110+
if (p_jump_statement->token->type == TK_KEYWORD_THROW ||
111+
p_jump_statement->token->type == TK_KEYWORD_RETURN ||
112+
p_jump_statement->token->type == TK_KEYWORD_BREAK ||
113+
p_jump_statement->token->type == TK_KEYWORD_CONTINUE ||
114+
p_jump_statement->token->type == TK_KEYWORD_GOTO)
115+
{
116+
ajust_line_and_identation(p_jump_statement->token, ctx);
117+
}
118+
else
119+
{
120+
assert(false);
121+
}
122+
}
123+
124+
static void format_visit_compound_statement(struct format_visit_ctx* ctx, struct compound_statement* p_compound_statement, struct error* error);
125+
126+
127+
static void format_visit_secondary_block(struct format_visit_ctx* ctx, struct secondary_block* p_secondary_block, struct error* error)
128+
{
129+
format_visit_statement(ctx, p_secondary_block->statement, error);
130+
}
131+
132+
static void format_visit_iteration_statement(struct format_visit_ctx* ctx, struct iteration_statement* p_iteration_statement, struct error* error)
133+
{
134+
135+
if (p_iteration_statement->expression1)
136+
{
137+
//format_visit_expression(ctx, p_iteration_statement->expression1, error);
138+
}
139+
140+
if (p_iteration_statement->expression2)
141+
{
142+
//format_visit_expression(ctx, p_iteration_statement->expression2, error);
143+
}
144+
145+
146+
147+
if (p_iteration_statement->secondary_block)
148+
{
149+
format_visit_secondary_block(ctx, p_iteration_statement->secondary_block, error);
150+
}
151+
}
152+
153+
154+
155+
static void format_visit_primary_block(struct format_visit_ctx* ctx, struct primary_block* p_primary_block, struct error* error)
156+
{
157+
if (p_primary_block->defer_statement)
158+
{
159+
//visit_defer_statement(ctx, p_primary_block->defer_statement, error);
160+
}
161+
else
162+
{
163+
if (p_primary_block->compound_statement)
164+
{
165+
format_visit_compound_statement(ctx, p_primary_block->compound_statement, error);
166+
}
167+
else if (p_primary_block->iteration_statement)
168+
{
169+
format_visit_iteration_statement(ctx, p_primary_block->iteration_statement, error);
170+
}
171+
else if (p_primary_block->selection_statement)
172+
{
173+
format_visit_selection_statement(ctx, p_primary_block->selection_statement, error);
174+
}
175+
}
176+
177+
}
178+
179+
180+
static void format_visit_expression_statement(struct format_visit_ctx* ctx, struct expression_statement* p_expression_statement, struct error* error)
181+
{
182+
if (p_expression_statement->expression)
183+
{
184+
//ajust_line_and_identation(p_expression_statement->first_token, ctx);
185+
}
186+
}
187+
188+
static void format_visit_unlabeled_statement(struct format_visit_ctx* ctx, struct unlabeled_statement* p_unlabeled_statement, struct error* error)
189+
{
190+
if (p_unlabeled_statement->primary_block)
191+
{
192+
format_visit_primary_block(ctx, p_unlabeled_statement->primary_block, error);
193+
}
194+
else if (p_unlabeled_statement->expression_statement)
195+
{
196+
format_visit_expression_statement(ctx, p_unlabeled_statement->expression_statement, error);
197+
}
198+
else if (p_unlabeled_statement->jump_statement)
199+
{
200+
format_visit_jump_statement(ctx, p_unlabeled_statement->jump_statement, error);
201+
}
202+
else
203+
{
204+
assert(false);
205+
}
206+
}
207+
208+
static void format_visit_block_item(struct format_visit_ctx* ctx, struct block_item* p_block_item, struct error* error)
209+
{
210+
ajust_line_and_identation(p_block_item->first_token, ctx);
211+
212+
if (p_block_item->declaration)
213+
{
214+
//visit_declaration(ctx, p_block_item->declaration, error);
215+
}
216+
else if (p_block_item->unlabeled_statement)
217+
{
218+
format_visit_unlabeled_statement(ctx, p_block_item->unlabeled_statement, error);
219+
}
220+
else if (p_block_item->labeled_statement)
221+
{
222+
//visit_labeled_statement(ctx, p_block_item->labeled_statement, error);
223+
}
224+
}
225+
226+
static void format_visit_block_item_list(struct format_visit_ctx* ctx, struct block_item_list* p_block_item_list, struct error* error)
227+
{
228+
struct block_item* p_block_item = p_block_item_list->head;
229+
while (error->code == 0 && p_block_item)
230+
{
231+
format_visit_block_item(ctx, p_block_item, error);
232+
p_block_item = p_block_item->next;
233+
}
234+
}
235+
236+
static void format_visit_compound_statement(struct format_visit_ctx* ctx, struct compound_statement* p_compound_statement, struct error* error)
237+
{
238+
ajust_line_and_identation(p_compound_statement->first, ctx);
239+
240+
ctx->identation++;
241+
format_visit_block_item_list(ctx, &p_compound_statement->block_item_list, error);
242+
243+
ctx->identation--;
244+
245+
ajust_line_and_identation(p_compound_statement->last, ctx);
246+
}
247+
248+
static void format_visit_declaration(struct format_visit_ctx* ctx, struct declaration* p_declaration, struct error* error)
249+
{
250+
if (p_declaration->static_assert_declaration)
251+
{
252+
//format_visit_static_assert_declaration(ctx, p_declaration->static_assert_declaration, error);
253+
}
254+
255+
if (p_declaration->declaration_specifiers)
256+
{
257+
//format_visit_declaration_specifiers(ctx, p_declaration->declaration_specifiers, error);
258+
259+
}
260+
261+
if (p_declaration->init_declarator_list.head)
262+
{
263+
//format_visit_init_declarator_list(ctx, &p_declaration->init_declarator_list, error);
264+
}
265+
266+
if (p_declaration->function_body)
267+
{
268+
format_visit_compound_statement(ctx, p_declaration->function_body, error);
269+
}
270+
}
271+
272+
void format_visit(struct format_visit_ctx* ctx, struct error* error)
273+
{
274+
struct declaration* p_declaration = ctx->ast.declaration_list.head;
275+
while (error->code == 0 && p_declaration)
276+
{
277+
format_visit_declaration(ctx, p_declaration, error);
278+
p_declaration = p_declaration->next;
279+
}
280+
}

0 commit comments

Comments
 (0)