Skip to content

Compiler-Organization/CommonC

Repository files navigation

The official Common C language compiler


The common C langugage is developed to deliver the performance of natively compiled languages whilst maintaining the ease of memory management without workarounds like garbage collection and borrow checkers. Common C is object oriented and statically typed.

Common C will be targeting both x86_64 for Intel and AMD aswell as .NET (CIL).

Currently the .NET target is under development, with the x86_64 target to be developed as soon as the .NET target is satisfactory.

Contributions are welcome with open arms!


Thoughts behind Common C

Other languages are either easy to write, but perform poorly (E.g Python, JavaScript, etc) or they perform well but are difficult to write (E.g C, C++, Rust, etc). Common C is designed to be easy to write and perform well without workarounds like garbage collection and borrow checkers.

The philosophy behind the syntax is to be easily readable and writable. Syntax is developed so that the shortest combination of keystrokes produces functionality. We keep in mind that not every developed posesses giant hands that reach across the keyboard.

An example of this is using log as the standard output function. C# uses Console.WriteLine and Rust uses println!. Both of these are long to write and require more keystrokes than simply log.


"New" functionality

Unpacking tables has never been easier! Here is an example of unpacking a table in Common C:

// array = { "Hello", "there", "world!", "I'm", "a", "machine", "with", "finite", "possibilities!" }

log(array->3..5)

// I'm a machine

Here we are unpacking the array from index 3 to index 5 and logging it to the console.


Language design

Common C uses top-level, global and public declarations for functions, structs and globals - meaning everything can be accessed from anywhere. Given CommonC's ergonomic style, it has been decided that having access to everything anywhere is the "free-est" way of programming. You do not have to declare the visibility of user-types like functions and globals. The only exception is uninitialized user-types like struct declarations.

There really is no point in using var or equal in a modern, statically typed language apart from extra syntax clutter - hence the type is directly stated instead.

Semicolon after statements is optional simply because some people prefer it, though it has no function.

Table of contents

  • Expressions
    • String
      • Primitive type string.
    • Boolean
      • Primitive type boolean.
    • Number
      • Primitive type number.
    • Identifier
      • Generic identifiers.
    • Array
      • Unbound, typeless array.
    • Array initializer
      • Bound, typed array initializer.
    • Index
      • Index of array.
    • Length
      • Length of array.
    • Range
      • Range between two expressions.
    • Call
      • Function call.
    • Member
      • Parent / member relationship.
    • Relational
      • Equals.
      • Not equals.
      • Greater than.
      • Greater than or equals.
      • Less than.
      • Less than or equals.
    • Arithmetic
      • Addition.
      • Subtraction.
      • Multiplication.
      • Division.
      • Modulus.
      • Power.
      • Left shift.
      • Right shift.
    • Negate
      • Negate expression from positive to negative.
    • Not
      • Reverse boolean.
    • Object initializer
      • Bound, typed object initializer.
    • Parameter
      • Function declaration parameter.
    • Parenthesized
      • Parenthesized expression for control flow.
    • Type
      • Native, reserved types.
    • Unpack
      • Unpacking arrays.
  • Statements

Expressions

String

Generic string taking any character. Starts with a quotation mark and terminates with a quotation mark.

"<any>"

Example

"Hello, world!"

Boolean

Deterministic boolean.

true / false

Example

bool isTrue = true

Number

Any number of any length.

123

Example

person.age = 50

Identifier

Generic identifier starting with a letter and containing letters or numbers.

someVariable

Example

log(yourName)

Array

An array of expression, seperated by a comma.

{ <expr[]> }

Example

{ 5, 10, 15 }

Array initializer

Initializes a new array with specified type and size. Each item in the initialized array is seperated by a comma.

<expr | type>[<expr | number>] {
    <expr[]>
}

Example

int[3] { 5, 10, 15 }

Index

Accesses an array through a specified index.

<expr | array<any>>[<expr>]

Example

int arr = int[3] { 5, 10, 15 }

log(arr[1])

Length

Gets the length of an array.

#<expr>

Example

#arr

Call

Performs a call on the given function. Arguments are seperated by a comma.

<expr>(<expr[]>)

Example

log("Hello, world!")

Member

Accesses the member of a parent. Parent and member are seperated by a dot. Can be nested.

<expr>.<expr>

Example

Parent.member

Relational

Determines wether two expressions meet a conditional and their relation to eachother.

Supported operators

  • == - Equals.
  • != - Not equals.
  • > - Greater than.
  • >= - Greater than or equals.
  • < - Less than.
  • <= - Less than or equals.
<expr> <operator> <expr>

Example

4 > 2

Arithmetic

Performs an arithmetic operation on two expressions.

Supported operators

  • + - Addition.
  • - - Subtraction.
  • * - Multiplication.
  • / - Division.
  • ^ - Power.
  • % - Modulus.
<expr> <operator> <expr>

Example

2 + 2

Negate

Changes an expression to its additive inverse.

-<expr>

Example

-100

Not

Changes a number or boolean to its binary counterpart.

!<expr>

Example

!true

Object initializer

Initializes a new object of a specified type. Properties of the object can be assigned, with name and value seperated by a colon and each property seperated by a comma.

<expr | type> {
    <expr>: <expr>,
}

Example

Person {
    name: "Kai",
    age: 26
}

Parameter

The parameter structure used in function declarations.

<expr | type> <expr>

Example

str name

Parenthesized

Wraps an expression in parentheses to override syntax precedence.

(<expr>)

Example

(2 + 2) * 5

Type

Primitive reserved types.

Supported types

  • str | string
  • i8
  • u8
  • i16
  • u16
  • i32
  • u32
  • i64
  • u64
  • i128
  • u128
  • f32
  • f64
  • bool
  • fn
<type>

Example

i32

Unpack

Unpacks an array into seperate components.

<expr> | array<any>> -> <expr | range>

Example

arr -> 0..5

Statements

Assignment

Assigns an expression to a local, global or property

<expr> = <expr>

Example

message = "Hello!"

Function call

Calls a function and, if the return type is not fn, returns it.

<expr>(<expr>)

Example

log("Hello, world!")

Closure

Creates a new closure for control flow purposes.

{ <statements> }

Example

{ log("Hello, world!") }

For loop

Repeats a closure until numeric range has been nivelled. Sets the current iteration to a user-specified local.

for <expr | range>, <expr | identifier> {
    <statements>
}

Example

for 0..5, i {
    log(i)
}

Function declaration

Declares a new function with parameters and a return type. Parentheses for parameters is not needed if there are none.

<expr | type> <expr | identifier>(<parameters>) {
    <statements>
}

Example

fn testFunc(str message) {
    log(message)
}

fn main {
    testFunc("Hello, world!")
}

Return

Returns an expression and exits the function immediately.

return <expr>

Example

return true

Struct

Declares a new struct object with user-defined properties. Properties are seperated by a comma and cannot be assigned a default value.

struct <expr | identifier> {
    <expr | type> <expr | identifier>,
}

Example

struct Person {
    str name,
    i32 age
}

Variable declaration

Declares a new variable. If the declaration is in a closure, it will become a local. If the variable is declared on the global scope, it will become a global. Declarations do not require a default value.

<expr | type> <expr | identifier> = <expr>

Example

str message = "Hello, world!"

While

Repeats a closure until the condition is met.

while <expr> {
    <statements>
}

Example

while true {
    log("To infinity...")
}

If

Branches to closure if condition is met. If main condition is not met, checks elseifs and lastly the else case.

if <expr> {
    <statements>
}
elseif <expr> {
    <statements>
}
else {
    <statements>
}

Example

if 2 == 2 {
    log(4)
}

Use

Adds a .coc extension, imports the file from the name in the specified working directory, parses its syntax and merges it with the main file.

use <expr | identifier>

Example

use math

About

The official repo for the Common C language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages