Skip to content

Commit

Permalink
format code using clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
alissonbrunosa committed Jul 3, 2024
1 parent e228dcd commit c97e051
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 149 deletions.
18 changes: 18 additions & 0 deletions c/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Language: Cpp
IndentWidth: 4
UseTab: false
SortIncludes: true
SpaceBeforeAssignmentOperators: true
DerivePointerAlignment: false
IndentCaseLabels: true
PointerAlignment: Left
ColumnLimit: 100
SpaceBeforeParens: ControlStatements
SpaceAfterCStyleCast: true
AlignOperands: AlignAfterOperator
BreakBeforeBinaryOperators: NonAssignment
AllowShortIfStatementsOnASingleLine: true
AlignConsecutiveShortCaseStatements:
Enabled: true
AlignCaseColons: false
AcrossEmptyLines: false
134 changes: 45 additions & 89 deletions c/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static ir_token_t* lexer_read_number(ir_lexer_t* lex);
static ir_token_t* lexer_read_string(ir_lexer_t* lex);

ir_lexer_t* new_lexer(char* source) {
ir_lexer_t* lex = (ir_lexer_t *) malloc(sizeof(ir_lexer_t));
ir_lexer_t* lex = (ir_lexer_t*) malloc(sizeof(ir_lexer_t));
if (lex == NULL) {
printf("Memory allocation failed\n");
exit(1);
Expand All @@ -44,7 +44,7 @@ void lexer_free(ir_lexer_t* lex) {
free(lex);
}

ir_token_t* lexer_next(ir_lexer_t* lex){
ir_token_t* lexer_next(ir_lexer_t* lex) {
lexer_skip_whitespace(lex);

char* current = lex->current;
Expand All @@ -60,7 +60,7 @@ ir_token_t* lexer_next(ir_lexer_t* lex){
int line = lex->line;
int column = lex->column;

switch(*current) {
switch (*current) {
case '"':
return lexer_read_string(lex);

Expand All @@ -79,7 +79,7 @@ ir_token_t* lexer_next(ir_lexer_t* lex){

case '*':
lexer_advance(lex);
return new_token(TOKEN_STAR, current, 1,line, column);
return new_token(TOKEN_STAR, current, 1, line, column);

case '/':
lexer_advance(lex);
Expand Down Expand Up @@ -171,111 +171,69 @@ ir_token_t* lexer_next(ir_lexer_t* lex){
}

const char* token_names[] = {
"TOKEN_EOF",
"TOKEN_IDENT",
"TOKEN_NOT",
"TOKEN_DOT",
"TOKEN_PLUS",
"TOKEN_MINUS",
"TOKEN_STAR",
"TOKEN_SLASH",
"TOKEN_COLON",
"TOKEN_COMMA",
"TOKEN_BANG",
"TOKEN_ARROW",
"TOKEN_ASSIGN",
"TOKEN_EQUAL",
"TOKEN_NOT_EQUAL",
"TOKEN_LESS",
"TOKEN_LESS_EQUAL",
"TOKEN_GREAT",
"TOKEN_GREAT_EQUAL",
"TOKEN_SEMICOLON",
"TOKEN_LEFT_PAREN",
"TOKEN_RIGHT_PAREN",
"TOKEN_LEFT_BRACE",
"TOKEN_RIGHT_BRACE",
"TOKEN_LEFT_BRACKET",
"TOKEN_RIGHT_BRACKET",
"TOKEN_STRING",
"TOKEN_NUMBER",
"TOKEN_OR",
"TOKEN_AND",
"TOKEN_IF",
"TOKEN_ELSE",
"TOKEN_SWITCH",
"TOKEN_CASE",
"TOKEN_DEFAULT",
"TOKEN_FOR",
"TOKEN_IN",
"TOKEN_NONE",
"TOKEN_OBJECT",
"TOKEN_IS",
"TOKEN_VAR",
"TOKEN_FUN",
"TOKEN_THIS",
"TOKEN_RETURN",
"TOKEN_SUPER",
"TOKEN_TRUE",
"TOKEN_FALSE",
"TOKEN_WHILE",
"TOKEN_STOP",
"TOKEN_NEXT",
"TOKEN_UNKNOWN"
"TOKEN_EOF", "TOKEN_IDENT", "TOKEN_NOT", "TOKEN_DOT",
"TOKEN_PLUS", "TOKEN_MINUS", "TOKEN_STAR", "TOKEN_SLASH",
"TOKEN_COLON", "TOKEN_COMMA", "TOKEN_BANG", "TOKEN_ARROW",
"TOKEN_ASSIGN", "TOKEN_EQUAL", "TOKEN_NOT_EQUAL", "TOKEN_LESS",
"TOKEN_LESS_EQUAL", "TOKEN_GREAT", "TOKEN_GREAT_EQUAL", "TOKEN_SEMICOLON",
"TOKEN_LEFT_PAREN", "TOKEN_RIGHT_PAREN", "TOKEN_LEFT_BRACE", "TOKEN_RIGHT_BRACE",
"TOKEN_LEFT_BRACKET", "TOKEN_RIGHT_BRACKET", "TOKEN_STRING", "TOKEN_NUMBER",
"TOKEN_OR", "TOKEN_AND", "TOKEN_IF", "TOKEN_ELSE",
"TOKEN_SWITCH", "TOKEN_CASE", "TOKEN_DEFAULT", "TOKEN_FOR",
"TOKEN_IN", "TOKEN_NONE", "TOKEN_OBJECT", "TOKEN_IS",
"TOKEN_VAR", "TOKEN_FUN", "TOKEN_THIS", "TOKEN_RETURN",
"TOKEN_SUPER", "TOKEN_TRUE", "TOKEN_FALSE", "TOKEN_WHILE",
"TOKEN_STOP", "TOKEN_NEXT", "TOKEN_UNKNOWN",
};

static int is_letter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}

static int is_digit(char c) {
return c >= '0' && c <= '9';
}
static int is_digit(char c) { return c >= '0' && c <= '9'; }

static int is_alphanumetic(char c) {
return is_digit(c) || is_letter(c);
}
static int is_alphanumetic(char c) { return is_digit(c) || is_letter(c); }

static enum ID keyword_lookup(char* literal, int length) {
switch(length) {
switch (length) {
case 2:
if(memcmp(literal, "or", length) == 0) return TOKEN_OR;
if(memcmp(literal, "if", length) == 0) return TOKEN_IF;
if(memcmp(literal, "in", length) == 0) return TOKEN_IN;
if(memcmp(literal, "is", length) == 0) return TOKEN_IS;
if (memcmp(literal, "or", length) == 0) return TOKEN_OR;
if (memcmp(literal, "if", length) == 0) return TOKEN_IF;
if (memcmp(literal, "in", length) == 0) return TOKEN_IN;
if (memcmp(literal, "is", length) == 0) return TOKEN_IS;
break;

case 3:
if(memcmp(literal, "and", length) == 0) return TOKEN_AND;
if(memcmp(literal, "var", length) == 0) return TOKEN_VAR;
if(memcmp(literal, "fun", length) == 0) return TOKEN_FUN;
if(memcmp(literal, "for", length) == 0) return TOKEN_FOR;
if (memcmp(literal, "and", length) == 0) return TOKEN_AND;
if (memcmp(literal, "var", length) == 0) return TOKEN_VAR;
if (memcmp(literal, "fun", length) == 0) return TOKEN_FUN;
if (memcmp(literal, "for", length) == 0) return TOKEN_FOR;
break;

case 4:
if(memcmp(literal, "else", length) == 0) return TOKEN_ELSE;
if(memcmp(literal, "case", length) == 0) return TOKEN_CASE;
if(memcmp(literal, "none", length) == 0) return TOKEN_NONE;
if(memcmp(literal, "this", length) == 0) return TOKEN_THIS;
if(memcmp(literal, "true", length) == 0) return TOKEN_TRUE;
if(memcmp(literal, "stop", length) == 0) return TOKEN_STOP;
if(memcmp(literal, "next", length) == 0) return TOKEN_NEXT;
if (memcmp(literal, "else", length) == 0) return TOKEN_ELSE;
if (memcmp(literal, "case", length) == 0) return TOKEN_CASE;
if (memcmp(literal, "none", length) == 0) return TOKEN_NONE;
if (memcmp(literal, "this", length) == 0) return TOKEN_THIS;
if (memcmp(literal, "true", length) == 0) return TOKEN_TRUE;
if (memcmp(literal, "stop", length) == 0) return TOKEN_STOP;
if (memcmp(literal, "next", length) == 0) return TOKEN_NEXT;
break;

case 5:
if(memcmp(literal, "super", length) == 0) return TOKEN_SUPER;
if(memcmp(literal, "false", length) == 0) return TOKEN_FALSE;
if(memcmp(literal, "while", length) == 0) return TOKEN_WHILE;
if (memcmp(literal, "super", length) == 0) return TOKEN_SUPER;
if (memcmp(literal, "false", length) == 0) return TOKEN_FALSE;
if (memcmp(literal, "while", length) == 0) return TOKEN_WHILE;
break;

case 6:
if(memcmp(literal, "switch", length) == 0) return TOKEN_SWITCH;
if(memcmp(literal, "object", length) == 0) return TOKEN_OBJECT;
if(memcmp(literal, "return", length) == 0) return TOKEN_RETURN;
if (memcmp(literal, "switch", length) == 0) return TOKEN_SWITCH;
if (memcmp(literal, "object", length) == 0) return TOKEN_OBJECT;
if (memcmp(literal, "return", length) == 0) return TOKEN_RETURN;
break;

case 7:
if(memcmp(literal, "default", length) == 0) return TOKEN_DEFAULT;
if (memcmp(literal, "default", length) == 0) return TOKEN_DEFAULT;
break;

default:
Expand All @@ -286,7 +244,7 @@ static enum ID keyword_lookup(char* literal, int length) {
}

static ir_token_t* new_token(enum ID id, char* literal, int length, int line, int column) {
ir_token_t* tok = (ir_token_t *) malloc(sizeof(ir_token_t));
ir_token_t* tok = (ir_token_t*) malloc(sizeof(ir_token_t));
if (tok == NULL) {
printf("Memory allocation failed\n");
exit(1);
Expand All @@ -309,9 +267,7 @@ static void token_free(ir_token_t* tok) {
free(tok);
}

static char lexer_peek(ir_lexer_t* lex) {
return *lex->current;
}
static char lexer_peek(ir_lexer_t* lex) { return *lex->current; }

static char lexer_peek_next(ir_lexer_t* lex) {
if (lexer_peek(lex) == '\0') {
Expand All @@ -338,7 +294,7 @@ static char lexer_advance(ir_lexer_t* lex) {
static void lexer_skip_whitespace(ir_lexer_t* lex) {
while (1) {
char current = lexer_peek(lex);
switch(current) {
switch (current) {
case ' ':
case '\r':
case '\t':
Expand Down
Loading

0 comments on commit c97e051

Please sign in to comment.