Skip to content

Latest commit

 

History

History
139 lines (92 loc) · 3.59 KB

CODING_STYLES.md

File metadata and controls

139 lines (92 loc) · 3.59 KB

Coding Styles

C

We using C23 standard. An other thing to know, we use lily_base library that's include some data structures, such as File, Vec, Stack, etc.

Header Guard

When we name a header guard, we use the file path to name the guard. In addition, headers must be written in upper case. Additionally, the guard name must start with LILY and must end with the file extension, so most often H or HPP. Plus, the last two rules are that for example the name of the folder and the file is repeated with the same name without taking into account the extension of the file, we avoid repeating the same name. Finally, the header guard of the project file must be unique (exception in certain cases, like in tests/core/lily/scanner/util.c).

// path: include/core/lily/analysis/analysis.h
// guard name: CORE_LILY_ANALYSIS_H (bad)
// guard name: LILY_CORE_LILY_ANALYSIS_ANALYSIS_H (bad)
// guard name: LILY_CORE_LILY_ANALYSIS_H (ok)

Function

When you want to name a function, you must add a suffix __ + type name. Plus, the base of the function name must be write in snake-case.

typedef struct Person {
    const char *name;
    Uint8 age;
} Person;

const char *
get_name(const Person *self); // bad

const char *
get_name__Person(const Person *self); // ok

However, if the function is not associated to a type (enum or struct), add a suffix __ + name access.

// path: src/str.c
Usize
len__Str(const char *s);

NOTE: The prefix must be unique, to avoid function name conflicts.

Parameter

If the function is associated to a type, the first parameter, must be named self.

Inline

Take a look at src/ex/README.md to find out how inline functions are managed.

Global variable

You must be define global variable with static keyword.

Constant

All constants must be defined in uppercase.

const int a = 340; // bad
const int A = 340; // ok

Macro

All macros must be defined in uppercase.

#define hello(s) "Hello "s // bad
#define HELLO(s) "Hello "s // ok

Include

For most includes we use the one with angle brackets (#include <header>), but sometimes (e.g. in tests) we use the version with double quotes (#include "header").

Enum

The name of the enumeration must be written in PascalCase. Additionally, if the enumeration is used with a structure (to be used with a union), you must add Kind to the end of the enumeration name.

The items in the enumeration must be written in UpperCase and each item name must begin with the name of the enum (in UpperCase).

Finally, in most cases, you should add Lily in front of the enumeration name (with a few exceptions).

EXCEPTION(S): lily_base.

Here is a small example of using an enumeration with the rules previously listed:

enum LilyAnimalKind {
    LILY_ANIMAL_KIND_CAT,
    LILY_ANIMAL_KIND_DOG,
};

typedef struct LilyCat {
  // ...
} LilyCat;

typedef struct LilyDog {
  // ...
} LilyDog;

typedef struct LilyAnimal {
    enum LilyAnimalKind kind;
    union {
        LilyCat cat;
        LilyDog dog;
    };
} LilyAnimal;

Struct

The name of the structure must be written in PascalCase.

As in enumerations, you must add Lily in front of the structure name (with exceptions).

EXCEPTION(S): lily_base.

C++

We're using C++20 standard.

Lily

...

Abreviations

  • ptr: pointer
  • ast, AST: Abstract Syntax Tree
  • ref: reference
  • vm: virtual machine
  • decl: declaration
  • expr: expression
  • stmt: statement
  • gen: generation, generator