Deprecate autoformatter

This commit is contained in:
Bruce Hill 2025-02-21 15:19:19 -05:00
parent 794d236eb9
commit a94337bc11
6 changed files with 21 additions and 44 deletions

View File

@ -37,7 +37,7 @@ BUILTIN_OBJS=stdlib/siphash.o stdlib/arrays.o stdlib/bools.o stdlib/bytes.o stdl
stdlib/structs.o stdlib/enums.o stdlib/moments.o stdlib/mutexeddata.o
TESTS=$(patsubst %.tm,%.tm.testresult,$(wildcard test/*.tm))
all: libtomo.so tomo
all: libtomo.so tomo tomo.1
tomo: tomo.o $(BUILTIN_OBJS) ast.o parse.o environment.o types.o typecheck.o structs.o enums.o compile.o repl.o cordhelpers.o
@echo $(CC) $(CFLAGS_PLACEHOLDER) $(LDFLAGS) $^ $(LDLIBS) -o $@

View File

@ -130,14 +130,6 @@ tomo -t foo.tm
# Outputs: foo.tm.h foo.tm.c
```
Tomo uses the environment variables (`$CC`, `$VERBOSE`, and `$AUTOFMT`), which
control the compilation/running behavior of Tomo. The default behavior is to
use `tcc` (Tiny C Compiler) for fast compilation, `VERBOSE=0`, and
`AUTOFMT='indent -kr -l100 -nbbo -nut -sob'` for autoformatting generated code.
Any of these variables may be overridden, e.g. `CC=gcc VERBOSE=1 AUTOFMT= tomo
foo.tm` (compile with GCC and verbose compiler output without autoformatting
the code).
## Installing
```

View File

@ -62,7 +62,7 @@ static CORD with_source_info(ast_t *ast, CORD code)
if (code == CORD_EMPTY || !ast || !ast->file)
return code;
int64_t line = get_line_number(ast->file, ast->start);
return CORD_asprintf("#line %ld\n%r", line, code);
return CORD_asprintf("\n#line %ld\n%r", line, code);
}
static bool promote(env_t *env, ast_t *ast, CORD *code, type_t *actual, type_t *needed)

9
tomo.1
View File

@ -1,4 +1,4 @@
.\" Automatically generated by Pandoc 3.1.11.1
.\" Automatically generated by Pandoc 3.1.12.1
.\"
.TH "TOMO" "1" "June 11, 2024" "" ""
.SH NAME
@ -56,9 +56,6 @@ Print transpiled C code to the console.
\f[B]\-\-c\-compiler\f[R]
Set which C compiler is used.
.TP
\f[B]\-f\f[R], \f[B]\-\-autoformat\f[R]
Set which autoformat program is used.
.TP
\f[B]\-v\f[R], \f[B]\-\-verbose\f[R]
Print extra verbose output.
.TP
@ -70,9 +67,5 @@ Some options can be configured by setting environment variables.
.TP
\f[B]CC=\f[R]\f[I]c\-compiler\f[R]
Set which C compiler is used.
.TP
\f[B]AUTOFMT=\f[R]\f[I]autoformatter\f[R]
The program used to autoformat generated C code.
Default: \f[B]indent \-kr \-l100 \-nbbo \-nut \-sob\f[R]
.SH AUTHORS
Bruce Hill (\f[I]bruce\[at]bruce\-hill.com\f[R]).

View File

@ -59,9 +59,6 @@ C code, which is then compiled using a C compiler of your choice.
`--c-compiler`
: Set which C compiler is used.
`-f`, `--autoformat`
: Set which autoformat program is used.
`-v`, `--verbose`
: Print extra verbose output.
@ -74,6 +71,3 @@ Some options can be configured by setting environment variables.
`CC=`*c-compiler*
: Set which C compiler is used.
`AUTOFMT=`*autoformatter*
: The program used to autoformat generated C code. Default: `indent -kr -l100 -nbbo -nut -sob`

38
tomo.c
View File

@ -42,7 +42,7 @@ static OptionalBool_t verbose = false,
library_mode = false,
uninstall = false;
static OptionalText_t autofmt = Text("sed '/^\\s*$/d' | indent -kr -l100 -nbbo -nut -sob"),
static OptionalText_t
cflags = Text("-Werror -fdollars-in-identifiers -std=gnu11 -Wno-trigraphs -fsanitize=signed-integer-overflow -fno-sanitize-recover"
" -fno-signed-zeros -fno-finite-math-only -fno-signaling-nans -fno-trapping-math"
" -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE -fPIC -ggdb"
@ -109,8 +109,6 @@ int main(int argc, char *argv[])
{"C", false, &Bool$info, &show_codegen},
{"install", false, &Bool$info, &should_install},
{"I", false, &Bool$info, &should_install},
{"autoformat", false, &Text$info, &autofmt},
{"f", false, &Text$info, &autofmt},
{"c-compiler", false, &Text$info, &cc},
{"optimization", false, &Text$info, &optimization},
{"O", false, &Text$info, &optimization},
@ -299,18 +297,18 @@ void build_library(Text_t lib_dir_name)
env->libname = Text$as_c_string(escape_lib_name(lib_dir_name));
// Build a "whatever.h" header that loads all the headers:
FILE *header_prog = run_cmd("%k 2>/dev/null >'%k.h'", &autofmt, &lib_dir_name);
fputs("#pragma once\n", header_prog);
fputs("#include <tomo/tomo.h>\n", header_prog);
FILE *header = fopen(heap_strf("%k.h", &lib_dir_name), "w");
fputs("#pragma once\n", header);
fputs("#include <tomo/tomo.h>\n", header);
Table_t visited_files = {};
Table_t used_imports = {};
for (size_t i = 0; i < tm_files.gl_pathc; i++) {
const char *filename = tm_files.gl_pathv[i];
Path_t resolved = Path$resolved(Text$from_str(filename), Path("."));
_compile_file_header_for_library(env, resolved, &visited_files, &used_imports, header_prog);
_compile_file_header_for_library(env, resolved, &visited_files, &used_imports, header);
}
if (pclose(header_prog) == -1)
errx(1, "Failed to run autoformat program on header file: %k", &autofmt);
if (fclose(header) == -1)
errx(1, "Failed to write header file: %k.h", &lib_dir_name);
// Build up a list of symbol renamings:
unlink("symbol_renames.txt");
@ -516,10 +514,10 @@ void transpile_header(env_t *base_env, Text_t filename, bool force_retranspile)
CORD h_code = compile_file_header(module_env, ast);
FILE *prog = run_cmd("%k 2>/dev/null >'%k'", &autofmt, &h_filename);
CORD_put(h_code, prog);
if (pclose(prog) == -1)
errx(1, "Failed to run autoformat program on header file: %k", &autofmt);
FILE *header = fopen(Text$as_c_string(h_filename), "w");
CORD_put(h_code, header);
if (fclose(header) == -1)
errx(1, "Failed to write header file: %k", &h_filename);
if (!quiet || verbose)
printf("\x1b[2mTranspiled to %k\x1b[m\n", &h_filename);
@ -542,11 +540,11 @@ void transpile_code(env_t *base_env, Text_t filename, bool force_retranspile)
CORD c_code = compile_file(module_env, ast);
FILE *out = run_cmd("%k 2>/dev/null >'%k'", &autofmt, &c_filename);
if (!out)
errx(1, "Failed to run autoformat program: %k", &autofmt);
FILE *c_file = fopen(Text$as_c_string(c_filename), "w");
if (!c_file)
errx(1, "Failed to write C file: %k", &c_filename);
CORD_put(c_code, out);
CORD_put(c_code, c_file);
binding_t *main_binding = get_binding(module_env, "main");
if (main_binding && main_binding->type->tag == FunctionType) {
@ -558,11 +556,11 @@ void transpile_code(env_t *base_env, Text_t filename, bool force_retranspile)
"\n",
compile_cli_arg_call(module_env, main_binding->code, main_binding->type),
"return 0;\n"
"}\n"), out);
"}\n"), c_file);
}
if (pclose(out) == -1)
errx(1, "Failed to output autoformatted C code to %k: %k", &c_filename, &autofmt);
if (fclose(c_file) == -1)
errx(1, "Failed to output C code to %k", &c_filename);
if (!quiet || verbose)
printf("\x1b[2mTranspiled to %k\x1b[m\n", &c_filename);