Skip to content

LukeSchoen/CPrime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cPrime

what is cPrime?

Compilation speed of C. Expressiveness of C++. (*or as close as is possible)

I've always wanted to create a coding language which selectively adopted the most powerful features of C++ without incidentilly dragging in all its hidden-complexities and past-mistakes; CPrime is my first attempt.

This Repo provides a full working self-compiling set of src code plus a demo windows.exe/build.cmd script (Note: primes compiler is pure C and is able to compile happily under linux, mac, android etc)

Properties of the cPrime Langauge:

  • Able to compile any C program (including itself), and many cpp files.
  • Can itself be compiled in any C++ compiler and runs exactly as if it was C++

Info: CPrime's c compiler was built on top of TCC By Fabrice Bellard: https://bellard.org/tcc/


What does cPrime Solve?

C is an incredible programming language! (simple / fast / effective)

But it lacks Object-Orientation which means it doesn't easily scale to bigger projects.

Here's Steve Jobs explaing OOP in the 90s: https://youtu.be/vqC041CtKXQ?t=14

I think of it like this:

A bag of functions and structs which must be used in a certain way (c code) is unlikely to get reused.

C++ technically solved this long ago by adding powerful models of abstraction (objects / classes etc)

Objects are able to pack functionaltiy and data together, while also providing controls against missuse.

Cpp has no feature deprication mechanism and it's old - so real cpp compilers have become heavy and slow.

under LLVM/CLANG & MSVC C++ compilation is generally ~20-200X slower than c in LCC, TCC (or CPrime).

CPrime is basically my attempt to try and sit in the middle (getting the best of both worlds)

  • Keep the compatibiltiy simplicity and compiler performance of raw C
  • Add the structuring tools that cpp provides for scaling up large projects

Status

Focus is still on core language features and maintaining high performance compile speeds

cPrime is almost like a partial clean room reimplementation of C++ from C, so expect delays.

That being said; Im very fast, I only started on this 2 days ago it's basically already done.

I've ported all my 3D games/projects & they all now build FAST! (it's become my coding language)


Contributing

Experiments/ideas or reports are very welcome (just pop them over in the issues tab)


Language Features

Classes

struct Point
{
  int x;
  int y;

  void translate(int dx int dy)
  {
    this->x += dx;
    this->y += dy;
  }
};
  • class works as a struct alias
  • public private etc is supported
  • typedef boilerplate is now optional

Methods

Supported Syntax

  • obj.fn(arg1)
  • ptr->fn(arg1, arg2)
  • const correct calls
  • name mangling for function overloads

Operators

struct Num {
  int value;
  int operator+(int rhs);
};

int Num::operator+(int rhs)
{
  return this->value + rhs;
}

Supported

  • arithmetic + - * / %
  • comparisons
  • shifts
  • indexing []
  • assignment variants +=

Constructors

struct Widget {
  int value;
  Widget(int seed);
};

Widget::Widget(int seed)
{
  this->value = seed;
}

struct Guard {
  int value;
  ~Guard();
};

Guard::~Guard()
{
  cleanup_total += this->value;
}
  • automatic lifetime handling
  • supports standard C++ syntax

Templates

template<typename T>
T id(T value)
{
  return value;
}

template<typename T>
class List {
  T items[8];
  int count;

  void push(T value)
  {
    this->items[this->count++] = value;
  }
};
  • single type parameter (minimal but practical at the moment)
  • supports recursive templates: eg Map<List> etc

Seperated Decleration / Definition

struct Camera {
  int value;
  void init(int v);
};

void Camera::init(int v)
{
  this->value = v;
}

allows for a clean separation of interface and implementation without any loss in C compatibility


Overloading

struct Accumulator {
  int value;

  void set() { this->value = 5; }
  void set(int v) { this->value = v; }
  void set(float v) { this->value = (int)(v * 10.0f); }
};
  • overload function mangling based on parameter types
  • templates are compatible but not required for overloading

Motivation

Lately it’s been harder to justify using C++ at all. It produces Larger binaries, has slower compile times, heavier toolchains, complex setups etc all for a gain in expressiveness and abstraction that only even affects the coder and only while they are coding (bad trade).

Final Note

Huge respect to Bjarne Stroustrup: C++ has been my every day tool for litterily most of my entire life.

This project is about getting back to something that feels as lightweight and performant as C while keeping what I (and presumably all of us) actually value in C++.

About

Fast C compiler with essential C++ features like constructors, class-templates, overloads, etc

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages