Converts typescript to C.
It is being rewritten in C https://github.com/sebbekarlsson/tscc
- Automatically detect requirements and inject
#include
tags - If - else statements
- Classes
- Class member functions
- Class member variables
- functions
- String type
- Number type
- String lists (not working as expected, but does work in some cases D:)
- Number lists (TODO)
- Addition (math)
- Subtraction (math)
- Multiplication (math)
- Division (math)
- While loops
- Interfaces
- For loops (TODO)
... I probably missed something here
First install:
python setup.py install
Then you can use it like this:
typescript <filename>.ts
This will generate a
main.c
file.
When given this typescript snippet:
class Dog {
message: string;
function constructor(): void {
this.message = "Bark Bark\n";
};
function say(): void {
print(this.message);
};
};
class Cat {
message: string;
function constructor(): void {
this.message = "Mew Mew\n";
};
function say(): void {
print(this.message);
};
};
function main(argc: number, argv: string[]): number {
let dog = new Dog();
dog.say();
let cat = new Cat();
cat.say();
};
The output will be this:
#include <stdio.h>
#include <stdlib.h>
typedef struct DOG_STRUCT {
char *message;
void (*constructor)(void *self);
void (*say)(void *self);
} Dog;
void Dog_constructor(Dog *self) { self->message = "Bark Bark\n"; }
void Dog_say(Dog *self) { printf(self->message); }
Dog *init_Dog() {
Dog *x = calloc(1, sizeof(struct DOG_STRUCT));
x->message = calloc(1, sizeof(char *));
x->constructor = Dog_constructor;
x->say = Dog_say;
return x;
};
typedef struct CAT_STRUCT {
char *message;
void (*constructor)(void *self);
void (*say)(void *self);
} Cat;
void Cat_constructor(Cat *self) { self->message = "Mew Mew\n"; }
void Cat_say(Cat *self) { printf(self->message); }
Cat *init_Cat() {
Cat *x = calloc(1, sizeof(struct CAT_STRUCT));
x->message = calloc(1, sizeof(char *));
x->constructor = Cat_constructor;
x->say = Cat_say;
return x;
};
int main(int argc, char **argv) {
Dog *dog = init_Dog();
Dog_constructor(dog);
dog->say(dog);
Cat *cat = init_Cat();
Cat_constructor(cat);
cat->say(cat);
};