From a440a0d03247a0146123c4d58794aac9d77d3168 Mon Sep 17 00:00:00 2001 From: "csander@caltech.edu" <csander@caltech.edu> Date: Tue, 13 Oct 2020 05:48:16 +0000 Subject: [PATCH] Add clang-format --- .clang-format | 19 +++++++++++++++++++ .githooks/pre-commit | 12 ++++++++++++ include/ast.h | 11 +---------- include/parser.h | 2 ++ runtime/print_int.c | 9 +++++---- runtime/timing.c | 10 ++++------ src/ast.c | 4 ++-- src/compile.c | 6 +++--- src/compiler.c | 11 ++++------- src/parser.c | 12 ++++++------ 10 files changed, 58 insertions(+), 38 deletions(-) create mode 100644 .clang-format create mode 100755 .githooks/pre-commit diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6facf9c --- /dev/null +++ b/.clang-format @@ -0,0 +1,19 @@ +--- +# We'll use defaults from the Google style, but with 4 columns indentation. +BasedOnStyle: Google +# Never allow single-line functions +AllowShortFunctionsOnASingleLine: None +# Put "else" on a new line +BreakBeforeBraces: Custom +BraceWrapping: + BeforeElse: true +# Allow lines up to 90 characters +ColumnLimit: 90 +# Indent each block by 4 spaces +IndentWidth: 4 +TabWidth: 4 +# Require 1 space before a comment on the same line +SpacesBeforeTrailingComments: 1 +# Put a space after a cast, e.g. `(void) arg;` +SpaceAfterCStyleCast: true +--- diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..dd54775 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,12 @@ +#!/bin/bash + +if loc="$(type -p "clang-format")" && [[ -x $loc ]]; then + for name in $(git diff --cached --name-only --diff-filter=ACM); do + if echo $name | grep -Eq "\.[ch]$"; then + echo "Formatting ${name}..." + clang-format -i $name + fi + done +else + echo "You do not have clang-format installed; so, we were unable to unify the formatting in your files." +fi diff --git a/include/ast.h b/include/ast.h index 85f50b6..8e0fe35 100644 --- a/include/ast.h +++ b/include/ast.h @@ -11,16 +11,7 @@ #include <stdint.h> /** The types of AST nodes */ -typedef enum { - NUM, - BINARY_OP, - VAR, - SEQUENCE, - PRINT, - LET, - IF, - WHILE -} node_type_t; +typedef enum { NUM, BINARY_OP, VAR, SEQUENCE, PRINT, LET, IF, WHILE } node_type_t; /** The type of a TeenyBASIC variable name */ typedef char var_name_t; diff --git a/include/parser.h b/include/parser.h index ace4d02..2653fdf 100644 --- a/include/parser.h +++ b/include/parser.h @@ -1,6 +1,8 @@ #ifndef PARSER_H #define PARSER_H +#include <stdio.h> + #include "ast.h" /** Parses the next statement from the provided TeenyBASIC file into an AST */ diff --git a/runtime/print_int.c b/runtime/print_int.c index a4f9306..970191f 100644 --- a/runtime/print_int.c +++ b/runtime/print_int.c @@ -1,13 +1,13 @@ #include <inttypes.h> #include <stdio.h> + #include "ast.h" // A function that can be called from assembly to print an integer void print_int(value_t value) { printf("%" PRId64 "\n", value); // Clobber all caller-save registers - asm( - "movq $0x6A2CFE91073BD845, %%rax\n" + asm("movq $0x6A2CFE91073BD845, %%rax\n" "movq $0x03BAD7C14F2E6589, %%rdi\n" "movq $0x5D41EA960C72F8B3, %%rsi\n" "movq $0xEC364B2D5A7F9810, %%rdx\n" @@ -16,6 +16,7 @@ void print_int(value_t value) { "movq $0x92E1587A4BDCF630, %%r9\n" "movq $0x47DC36501F89AEB2, %%r10\n" "movq $0xAF4B29785C61ED30, %%r11\n" - : : : "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11" - ); + : + : + : "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11"); } diff --git a/runtime/timing.c b/runtime/timing.c index bd1a4f5..d076e4f 100644 --- a/runtime/timing.c +++ b/runtime/timing.c @@ -1,8 +1,8 @@ #include <assert.h> #include <math.h> #include <stdint.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <time.h> @@ -51,9 +51,7 @@ int main(int argc, char *argv[]) { printf( "test_name,mean_log_duration,variance_log_duration\n" "%s,%f,%e\n", - test_name, mean_log_duration, variance_log_duration - ); - fprintf(stderr, "%s mean duration: %e seconds (+/- %e x)\n", - test_name, exp(mean_log_duration), expm1(sqrt(variance_log_duration)) - ); + test_name, mean_log_duration, variance_log_duration); + fprintf(stderr, "%s mean duration: %e seconds (+/- %e x)\n", test_name, + exp(mean_log_duration), expm1(sqrt(variance_log_duration))); } diff --git a/src/ast.c b/src/ast.c index 7b39ec1..6ae1bb0 100644 --- a/src/ast.c +++ b/src/ast.c @@ -1,11 +1,11 @@ +#include "ast.h" + #include <assert.h> #include <inttypes.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include "ast.h" - node_t *init_num_node(value_t value) { num_node_t *node = malloc(sizeof(num_node_t)); assert(node != NULL); diff --git a/src/compile.c b/src/compile.c index 1554ad7..ae52d90 100644 --- a/src/compile.c +++ b/src/compile.c @@ -1,8 +1,8 @@ -#include <stdlib.h> -#include <stdio.h> - #include "compile.h" +#include <stdio.h> +#include <stdlib.h> + bool compile_ast(node_t *node) { /* You should remove this cast to void in your solution. * It is just here so the code compiles without warnings. */ diff --git a/src/compiler.c b/src/compiler.c index b0a1170..25b6ceb 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -1,8 +1,8 @@ -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> -#include "parser.h" #include "compile.h" +#include "parser.h" void usage(char *program) { fprintf(stderr, "USAGE: %s <program file>\n", program); @@ -20,8 +20,7 @@ void header(void) { ".text\n" ".globl basic_main\n" "basic_main:\n" - " # The main() function\n" - ); + " # The main() function\n"); } /** @@ -30,9 +29,7 @@ void header(void) { * goes between the header and the footer. */ void footer(void) { - printf( - " ret\n" - ); + printf(" ret\n"); } int main(int argc, char *argv[]) { diff --git a/src/parser.c b/src/parser.c index aa01624..fcc5eb1 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,3 +1,5 @@ +#include "parser.h" + #include <assert.h> #include <ctype.h> #include <errno.h> @@ -6,8 +8,6 @@ #include <stdlib.h> #include <string.h> -#include "parser.h" - // maxint = -9223372036854775808 const size_t MAX_KEYWORD_LENGTH = 100; const value_t DEFAULT_STEP = 1; @@ -35,8 +35,8 @@ bool is_comparison_op(char c) { return c == '<' || c == '=' || c == '>'; } bool is_operator(char c) { - return is_open_paren(c) || is_close_paren(c) || - is_factor_op(c) || is_term_op(c) || is_comparison_op(c); + return is_open_paren(c) || is_close_paren(c) || is_factor_op(c) || is_term_op(c) || + is_comparison_op(c); } bool is_comment_start(char c) { return c == '#'; @@ -340,7 +340,7 @@ node_t *sequence(parser_state_t *state) { if (statement_count == statement_capacity) { statement_capacity = statement_capacity > 0 ? statement_capacity * 2 : 1; - statements = realloc(statements, sizeof(node_t*[statement_capacity])); + statements = realloc(statements, sizeof(node_t *[statement_capacity])); assert(statements != NULL); } statements[statement_count++] = next; @@ -354,7 +354,7 @@ node_t *sequence(parser_state_t *state) { } if (statement_count > 0) { - statements = realloc(statements, sizeof(node_t*[statement_count])); + statements = realloc(statements, sizeof(node_t *[statement_count])); assert(statements != NULL); } return init_sequence_node(statement_count, statements); -- GitLab