Git Product home page Git Product logo

nymph's Introduction

Nymph

Let's see what we can achieve by reworking C syntax.

Overview

Nymph is a simple C like programming language.

Nymph acts as a preprocessor, converting Nymph files (extension *.n) into C files.

This project is very much in development... It is not production ready.

What's New

Nothing right now

Goals

Completed

  • Class-Based OOP

  • Subtyping

In Progress

  • TBD

Pending

  • Destructors?

  • Type Inference?

  • Reflection?

  • Default function arguments?

  • Lambdas?

Example

mammal.n

#include <stdio.h>
#include <stdlib.h>

class Mammal {

    + int population = 0;             // Class Variable (+)
    - int height = 0, weight = 100;   // Object Variable (-)

    + Mammal *init(int height, int weight) {  // Class Method (+) Constructor
        this->height = height;
        this->weight = weight;
        Mammal->population++;
        return this;
    }

    - void print() {                          // Object Method (-)
        printf("print instance properties...\n");
    }
}

human.n

#include "mammal.n"
#include <stdio.h>
#include <stdlib.h>

class Human : Mammal {

    - char *name = NULL; // Object Variable (-)

    + Human *init(char *name, int height, int weight) { // Class Method (+) Constructor
        this = super->init(height, weight);
        this->name = name;
        return this;
    }

    - void died() {                                     // Object Method (-) Constructor
        free(this->name);
        free(this);
        Mammal->population--;
    }
}

int main(void) {

    char *name = malloc(5);
    memset(name, 0, sizeof(name));
    strcpy(name, "Fred");
    Human *person1 = Human->init(name, 76, 146); // Class Method Constructor Call
    person1->print();                            // Object Method Call
    person1->died();                             // Object Method Call

    return 0;
}
nymph -r human.n

nymph's People

Contributors

halosm1th avatar isaachier avatar maelswarm 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  avatar  avatar  avatar

nymph's Issues

New Parser

Nymph current parser is getting out of hand.
Let's make a new and better one.

Where to go next?

Aside from virtual procedures, are there any features you guys would like?

Segmentation fault

Are you interested in fixing these sort of errors?

Example input:

#inc <stdlib.h>

object Rect {
    int h;
    int w;
}

public void Rect*.print() ;
    printf("%i %i\n", this->h, this->w);
}

public void Rect**.create() {
    (*this) = new Rect;
    (*this)->w = 1;
    (*this)->h = 1;
}

int main(int argc, const char * argv[]) {
    Rect *myRect;

    &myRect.create();

    myRect.print();

    free(myRect);

    return 0;
}

gdb backtrace:

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007f13cb15b3fa in __GI_abort () at abort.c:89
#2  0x00007f13cb197bd0 in __libc_message (do_abort=do_abort@entry=2, fmt=fmt@entry=0x7f13cb28cdd0 "*** Error in `%s': %s: 0x%s ***\n")
    at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007f13cb19df96 in malloc_printerr (action=3, str=0x7f13cb28ce28 "munmap_chunk(): invalid pointer", ptr=<optimized out>, ar_ptr=<optimized out>)
    at malloc.c:5049
#4  0x0000564c8a6e88cc in functionCall (token=0x564c8c868998 ") ;\n    printf(\"%i %i\\n\", this->h, this->w);", pos=0x564c8c834f80, outputCFP=0x564c8c8351d0,
    outputHFP=0x564c8c835400, myDict=0x564c8c833010, myDictLen=0x564c8c834f60) at nymph_compiler.c:473
#5  0x0000564c8a6e9275 in parse (token=0x564c8c837686 "public void Rect*.print() ;\n    printf(\"%i %i\\n\", this->h, this->w);", pos=0x564c8c834f80,
    outputCFP=0x564c8c8351d0, outputHFP=0x564c8c835400, myDict=0x564c8c833010, myDictLen=0x564c8c834f60) at nymph_compiler.c:620
#6  0x0000564c8a6e9795 in main (argc=3, argv=0x7ffd27b45448) at nymph_compiler.c:697

.

.

char *postPrepareFunction(char *statement) can read past start of statement array

Around line 1106 of nymph_pp.c there is a chance to read out of bounds. The character before the start of the statement pointer can be read.

I found this error with the provided box.n sample files.

1106-1108

char *postPrepareFunction(char *statement) {
    char *tmp = strstr(statement, "(") - 1;
    for (; isalnum(tmp[0]) != 0; tmp--) {}

I think those same lines of code could be changed to:

	char *tmp = strstr(statement, "(") - 1;
	while (statement - tmp >= 0 && isalnum(tmp[0]) != 0)
		tmp--;

char *parseObj(char *buffer, FILE *hFile) not setting string length

Around line 909 in nymph_pp.c there is a non-terminated string that is being passed into strlen().
Around line 932 in the same file the same non-terminated string is being returned to the calling function.

I believe both of these will lead to crashing conditions. I found this error with the provided box.n sample files.

909-920:

        char *str = malloc(1000*sizeof(char));
        strcat(str, "typedef struct ");
        strcat(str, name);
        strcat(str, " ");
        strcat(str, name);
        strcat(str, ";\n");
        fwrite(str, 1, strlen(str), hFile);
        
        strcpy(str, "struct ");
        strcat(str, name);
        strcat(str, " {\n");
        fwrite(str, 1, strlen(str), hFile);

924-932:

        char *str = malloc(1000*sizeof(char));
        strcat(str, "typedef struct ");
        strcat(str, name);
        strcat(str, " ");
        strcat(str, name);
        strcat(str, ";\n");
        buffer = str_replace(buffer, "obj", "struct");
        strcat(str, buffer);
        return str;

I believe both of these errors can be avoided by using calloc(1, instead of malloc(.

Self-referential structs.

To do a typical self-referentially defined structure in C you typically have to do the following:

struct node; /* This is necessary ... */
struct node {
    char msg[128];
    struct node* next; /* ... so that the compiler knows what this is */
};

So the question is, why not make it so that in Nymph all named structs are scanned for and then just declared in bare format at the top of the preprocessed C file, so that you can omit this seemingly unnecessary redundant empty code from the user.

Make does not work

Using the make file that you provided, it does not work. After fixing square to rect it worked but now fails because there is no method named create or print.
Output from running make:
gcc -std=c11 nymph_compiler.c -o nymph ./nymph rect.n rect public public ./nymph box.n box public public ./nymph main.n main gcc -std=c11 -c rect.c rect.h gcc -std=c11 -c box.c box.h gcc -std=c11 -c main.c main.h main.c: In function ‘main’: main.c:9:1: warning: implicit declaration of function ‘create’ [-Wimplicit-function-declaration] create(&myBox); ^ main.c:14:1: warning: implicit declaration of function ‘print’ [-Wimplicit-function-declaration] print(myBox); ^ gcc -std=c11 main.o box.o rect.o -o out main.o: In function main':
main.c:(.text+0x2b): undefined reference to create' main.c:(.text+0x3c): undefined reference to create'
main.c:(.text+0x4d): undefined reference to print' main.c:(.text+0x5e): undefined reference to print'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'nymph' failed
make: *** [nymph] Error 1
`

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.