Giter Club home page Giter Club logo

holyc-lang's Introduction

The HolyC Programming Language - BETA

alt text

An implementation of Terry A. Davis's HolyC

U0 Main()
{
  "Hello world\n";
}
Main;

Full documentation for the language can be found here: https://holyc-lang.com/

Introduction

A holyc compiler built from scratch in C. Currently it is non optimising, walking the AST and compiling it directly to x86_64 assembly code as text which is fed into gcc to assemble. Floating point arithmetic is supported as are most of the major language features.

Example

Below is a snippet of code showing some of the features supported by this holyc compiler. Namely inheritance, loops, printf by using a string and loops. All c-like control flows are supported by the compiler.

class SomethingWithAnAge
{
  I64 age;
};

class Person : SomethingWithAnAge
{
  U8 name[1<<5];
};

U0 ExampleFunction(U0)
{
  Person *p = MAlloc(sizeof(Person));

  MemCpy(p->name,"Bob",3);
  p->age = 0;

  while (p->age < 42) {
    p->age++;
  }
  "name: %s, age: %d\n",p->name,p->age;
  Free(p);
}

ExampleFunction;

Compatibility

Currently this holyc compiler will compile holyc source code to an x86_64 compatible binary which has been tested on amd linux and an intel mac. Thus most x86_64 architectures should be supported. Creating an IR with some optimisations and compiling to ARM is high on the TODO list.

Building

Run make, then run make install (sudo make install on linux) this will install the compiler and holyc libraries for strings, hashtables, I/O, maths, networking, JSON parsing etc... see ./src/holyc-lib/

Using the compiler

Once the compiler has been compiled the following options are available, they can be displayed by running hcc --help

HolyC Compiler 2024. UNSTABLE
hcc [..OPTIONS] <..file>

OPTIONS:
  -ast     Print the ast and exit
  -tokens  Print the tokens and exit
  -S       Emit assembly only
  -obj     Emit an objectfile
  -lib     Emit a dynamic and static library
  -clibs   Link c libraries like: -clibs=`-lSDL2 -lxml2 -lcurl...`
  -o       Output filename: hcc -o <name> ./<file>.HC
  -run     Immediately run the file (not JIT)
  -g       Not implemented
  -D<var>  Set a compiler #define (does not accept a value)
  --help   Print this message

Differences

  • auto key word for type inference, an addition which makes it easier to write code.
  • cast<type> can be used for casting as well as post-fix type casting.
  • break and continue allowed in loops.
  • You can call any libc code by declaring the prototype with extern "c" <type> <function_name>. Then call the function as you usually would. See here for examples.

Bugs

This is a non exhaustive list of things that are buggy, if you find something's please open an issue or open a pull request. I do, however, intend to fix them when I get time.

  • Using %f for string formatting floats not work
  • Memory management for the compiler is virtually non-existent, presently all the tokens are made before compiling which is very slow.
  • Line number in error messages is sometimes off and does not report the file
  • Function pointers in a parameter list have to come at the end
  • Variable arguments are all passed on the stack
  • Casting between I32 and I64 is very buggy, the most obvious of which is calling a function which expects I64 and calling it with an I32 and vice versa, this will often cause a segmentation fault. As such prefer using I64 for integer types.
  • The preprocessor for #define can presently only accept numerical expressions and strings. It is not like a c compilers preprocessor.

Inspirations & Resources:

A lot of the assembly has been cobbled together by running gcc -S -O0 <file> or clang -s O0 <file>. Which has been effective in learning assembly, as has playing with TempleOS. The following are a non-exhaustive list of compilers and resources that I have found particularly useful for learning.

Want to ask questions?

Find me on twitch: https://www.twitch.tv/Jamesbarford

holyc-lang's People

Contributors

jamesbarford avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

holyc-lang's Issues

"Failed to extract filename" error

When I try to compile any HC file or even check HolyC compiler version I get this error:

~ hcc --version
--version
main.c:114:getASMFileName       Failed to extract filename~ hcc main.HC
main.HC
main.c:114:getASMFileName       Failed to extract filename

Tests aborted with "malloc(): corrupted top size"

When running make unit-test in /src folder, it yields:

$ make unit-test 
cd ../tests && hcc -o test-runner ./run.HC && ./test-runner
malloc(): corrupted top size
Aborted (core dumped)
make: *** [Makefile:53: unit-test] Error 134

Scripting math equations

I'll start by saying great work.

One thing I have found is when doing 'scripting' with math constants i'e

1+1;

Its gets a compile error

parser.c:1365:ParseToplevelDef line 1: Identifier expected: got TK_PUNCT +

  • not an error but a great feature is that if I compile and run U64 a = 99; in bash echo $? == 99 which is really good, I assume whatever is in RAX on exit is what the apps return code will be in bash. So I guess what ever the result is if it remains in RAX on exit that would be great for shell integration.

  • Also I found your channel on twitch, living on the other side of the world I doubt I will ever catch a live stream, but I would be interested if you could do a code walk through, basically going through all the source for the compiler?

  • I am happy to chip in a bit and help where I can, if you wanted to set up some tasks that can be picked from, I would be happy to pick some up.

can i use this code for my holy c ide

hi would you give me permission to use this in my holyc ide, i wont make money from it and i will publish the ide as opensource after it finished
also nice work you seem very talented

"-tokens" compiler option results in segmentation fault

I'm playing around a little bit with HolyC and your compiler, just to get used to it. I wanted to dump the token list and have a look into it. To do so, I copied the example from your README and tried to compile it using the following command hcc -tokens example.HC. This results in a segmentation fault error.

[Feature request] MinGW Support

Hello,
I tried to port your project to MinGW without success.
It would be very nice to have access to your compiler on windows.
Have a good day !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.