Skip to content

4 Basic Concepts

Spydr06 edited this page Oct 26, 2022 · 8 revisions

4 - Basic Concepts

4.1 - Types and Values

CSpydr is statically typed. This means, that variables, functions and parameters have their own type declared by the programmer. The compiler automatically resolves type inference and implicit casts when possible and shows an error when a cast - implicit or not - is invalid. By default, CSpydr has the following types:

Type Category Description
i8 Integer A 8-bit signed integer (-128 -> 127)
u8 Integer A 8-bit unsigned integer (0 -> 255)
i16 Integer A 16-bit signed integer (-32768 -> 32767)
u16 Integer A 16-bit unsigned integer (0 -> 65535)
i32 Integer A 32-bit signed integer (-2147483648 -> 2147483647)
u32 Integer A 32-bit unsigned integer (0 -> 4294967295)
i64 Integer A 64-bit signed integer (-9223372036854775808 -> 9223372036854775807)
u64 Integer A 64-bit unsigned integer (0 -> 18446744073709551615)
f32 Floating-Point A 32-bit single precision floating point number
f64 Floating-Point A 64-bit double precision floating point number
f80 Floating-Point A 80-bit double precision floating point number
bool Integer A 8-bit boolean value (true, false)
void Void The void-type is used for functions returning nothing as well as pointers with an unknown base type.
char Integer A 8-bit signed integer (-128 -> 127) made for representing ascii characters
fn Integer A 64-bit unsigned integer (pointer) to a function or lambda expression, can have return- and argument types specified: fn<i32>(i32, i32)
enum Integer A 32-bit signed integer made for defining constants

These types can be put into three different Categories: integer, floating-point and void. The compiler can convert between types of the same category without problems. Else, there will be errors and/or warnings. Sometimes, data can be lost though, e.g. converting from i64 to i8.

4.2 - Type Modifiers

The basic types can be modified using type modifiers. CSpydr has six type modifiers:

Type Category Example Description
struct Struct struct { a: i32, b: char } Structs can group multiple types together making one big type.
union Struct union { a: i32, b: char } Unions can group multiple types together, while keeping the same memory pointer for each entry. This makes it possible to have multiple types for one identifier.
& Integer &char &void Pointers store memory addresses to the base type. They are 64-bit unsigned integers. If the base type is not known, &void can be used.
[x] Array i32[4] char[10] Arrays can group multiple indices of a base type together to form one big type. The first 8 bytes of an array contain its length, which is accessible using the len operator.
[] Array/Integer i32[] char[] VLAs (Variable Length Arrays) can be either a pointer or an implicitly-sized array to a base type depending on the context.
'c[x] Array i32'c[4] char'c[10] C-like arrays represent arrays iin the same basic format as C. While CSpydr's normal array type also contains the length, these arrays only consist of the indices.
typeof * typeof x The typeof operator is not a type of its own, it returns the type of the right expression.

4.3 - Truthiness

Truthiness is the property of a value to be treated as true or false. Values equal to 0, nil or '\0' are false. All other values (non-null values) are true. Truthiness plays a major role in control flow and other parts of programs and is a code feature of most programming languages.

4.4 - Naming conventions

CSpydr has no pre-defined naming conventions as Rust, for example. These are the naming conventions I used for developing CSpydr, which I advise you to follow to keep the code clean:

  • PascalCase: Used for type definitions.
  • snake_case: Used for functions, variables, function arguments and namespaces.
  • UPPER_SNAKE_CASE: Used for enum members, constants and macros.
  • Identifiers starting with __ (two underscores) are meant for internal use in libraries and should not be accessed by the user (although they can).

> ***Note:** Identifiers starting with **`__csp_`** should not be used as they are reserved for internal functions of the C transpiler.*

Next: 5 - The Language