Declarations are now written |type| identifier;, generally. Functions are similar |void| func() {}, etc. Special declarations still work, etc

This commit is contained in:
Nathan Braswell
2014-08-01 00:45:48 -07:00
parent 4cf8dbbd5b
commit 5b57770774
31 changed files with 199 additions and 175 deletions

View File

@@ -3,6 +3,7 @@ translation_unit = interpreter_directive WS unorderd_list_part WS ;
unorderd_list_part = import WS unorderd_list_part | function WS unorderd_list_part | type_def WS ";" WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement WS ";" WS unorderd_list_part | import | function | type_def WS ";" | if_comp | simple_passthrough | declaration_statement WS ";" ; unorderd_list_part = import WS unorderd_list_part | function WS unorderd_list_part | type_def WS ";" WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement WS ";" WS unorderd_list_part | import | function | type_def WS ";" | if_comp | simple_passthrough | declaration_statement WS ";" ;
type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | identifier | identifier WS template_inst ; type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | identifier | identifier WS template_inst ;
dec_type = "\|" WS type WS "\|" ;
template_inst = "<" WS type_list WS ">" ; template_inst = "<" WS type_list WS ">" ;
type_list = type_list WS "," WS type | type ; type_list = type_list WS "," WS type | type ;
@@ -41,11 +42,11 @@ identifier = alpha | alpha alphanumeric ;
right_shift = ">" ">" ; right_shift = ">" ">" ;
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ; overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ;
func_identifier = identifier | identifier overloadable_operator ; func_identifier = identifier | identifier overloadable_operator ;
function = template_dec WS type WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS code_block | type WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS code_block ; function = template_dec WS dec_type WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS code_block | dec_type WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS code_block ;
opt_typed_parameter_list = typed_parameter_list | ; opt_typed_parameter_list = typed_parameter_list | ;
typed_parameter_list = typed_parameter_list WS "," WS typed_parameter | typed_parameter ; typed_parameter_list = typed_parameter_list WS "," WS typed_parameter | typed_parameter ;
typed_parameter = type WS identifier ; typed_parameter = dec_type WS identifier ;
opt_parameter_list = parameter_list | ; opt_parameter_list = parameter_list | ;
parameter_list = parameter_list WS "," WS parameter | parameter ; parameter_list = parameter_list WS "," WS parameter | parameter ;
@@ -91,7 +92,7 @@ number = integer | floating_literal ;
access_operation = unarad "." identifier | unarad "->" identifier ; access_operation = unarad "." identifier | unarad "->" identifier ;
assignment_statement = factor WS "=" WS boolean_expression | factor WS "\+=" WS boolean_expression | factor WS "-=" WS boolean_expression | factor WS "\*=" WS boolean_expression | factor WS "/=" WS boolean_expression ; assignment_statement = factor WS "=" WS boolean_expression | factor WS "\+=" WS boolean_expression | factor WS "-=" WS boolean_expression | factor WS "\*=" WS boolean_expression | factor WS "/=" WS boolean_expression ;
declaration_statement = type WS identifier WS "=" WS boolean_expression | type WS identifier | type WS identifier WS "." WS identifier WS "\(" WS opt_parameter_list WS "\)" ; declaration_statement = dec_type WS identifier WS "=" WS boolean_expression | dec_type WS identifier | dec_type WS identifier WS "." WS identifier WS "\(" WS opt_parameter_list WS "\)" ;
alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ; alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ;
hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ; hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ;

View File

@@ -24,6 +24,7 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths) {
removeSymbols.push_back(Symbol("comp_simple_passthrough", true)); removeSymbols.push_back(Symbol("comp_simple_passthrough", true));
removeSymbols.push_back(Symbol("typedef", true)); removeSymbols.push_back(Symbol("typedef", true));
removeSymbols.push_back(Symbol("template", true)); removeSymbols.push_back(Symbol("template", true));
removeSymbols.push_back(Symbol("\\|", true));
collapseSymbols.push_back(Symbol("opt_typed_parameter_list", false)); collapseSymbols.push_back(Symbol("opt_typed_parameter_list", false));
collapseSymbols.push_back(Symbol("opt_parameter_list", false)); collapseSymbols.push_back(Symbol("opt_parameter_list", false));
@@ -38,6 +39,7 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths) {
collapseSymbols.push_back(Symbol("type_list", false)); collapseSymbols.push_back(Symbol("type_list", false));
collapseSymbols.push_back(Symbol("template_param_list", false)); collapseSymbols.push_back(Symbol("template_param_list", false));
collapseSymbols.push_back(Symbol("trait_list", false)); collapseSymbols.push_back(Symbol("trait_list", false));
collapseSymbols.push_back(Symbol("dec_type", false));
} }
Importer::~Importer() { Importer::~Importer() {

View File

@@ -2,11 +2,11 @@ __if_comp__ __C__ __simple_passthrough__ """
#include <stdio.h> #include <stdio.h>
""" """
void println() { |void| println() {
print("\n"); print("\n");
} }
void print(char* toPrint) { |void| print(|char*| toPrint) {
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
printf(toPrint); printf(toPrint);
@@ -15,12 +15,12 @@ void print(char* toPrint) {
return; return;
} }
void println(char* toPrint) { |void| println(|char*| toPrint) {
print(toPrint); print(toPrint);
println(); println();
} }
void print(int toPrint) { |void| print(|int| toPrint) {
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
printf("%d", toPrint); printf("%d", toPrint);
@@ -29,12 +29,12 @@ void print(int toPrint) {
return; return;
} }
void println(int toPrint) { |void| println(|int| toPrint) {
print(toPrint); print(toPrint);
println(); println();
} }
void print(float toPrint) { |void| print(|float| toPrint) {
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
printf("%f", toPrint); printf("%f", toPrint);
@@ -43,7 +43,7 @@ void print(float toPrint) {
return; return;
} }
void print(double toPrint) { |void| print(|double| toPrint) {
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
printf("%f", toPrint); printf("%f", toPrint);
@@ -52,7 +52,7 @@ void print(double toPrint) {
return; return;
} }
void println(float toPrint) { |void| println(|float| toPrint) {
print(toPrint); print(toPrint);
println(); println();
} }

View File

@@ -1,9 +1,9 @@
int NotPi = 3; |int| NotPi = 3;
float Pi = 3.141592654; |float| Pi = 3.141592654;
int fibanacci(int num) { |int| fibanacci(|int| num) {
if (num < 2) if (num < 2)
return 1; return 1;
return fibanacci(num-1) + fibanacci(num-2); return fibanacci(num-1) + fibanacci(num-2);
} }

View File

@@ -4,8 +4,8 @@ __if_comp__ __C__ __simple_passthrough__ """
/* we have a template versions so we don't have to cast (because we don't have that yet) */ /* we have a template versions so we don't have to cast (because we don't have that yet) */
template <T> T* malloc(int size) { template <T> |T*| malloc(|int| size) {
T* memPtr = 0; |T*| memPtr = 0;
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
memPtr = malloc(size); memPtr = malloc(size);
@@ -14,7 +14,7 @@ template <T> T* malloc(int size) {
return memPtr; return memPtr;
} }
template <T> void free(T* memPtr) { template <T> |void| free(|T*| memPtr) {
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
free(memPtr); free(memPtr);
@@ -22,9 +22,9 @@ template <T> void free(T* memPtr) {
} }
} }
template <T> int sizeof() { template <T> |int| sizeof() {
int result = 0; |int| result = 0;
T testObj; |T| testObj;
__if_comp__ __C__ { __if_comp__ __C__ {
__simple_passthrough__ """ __simple_passthrough__ """
result = sizeof(testObj); result = sizeof(testObj);
@@ -33,32 +33,32 @@ template <T> int sizeof() {
return result; return result;
} }
template <T> T* new(int count) { template <T> |T*| new(|int| count) {
return malloc<T>( sizeof<T>() * count ); return malloc<T>( sizeof<T>() * count );
} }
template <T> T* new() { template <T> |T*| new() {
return new<T>(1); return new<T>(1);
} }
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */ /* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
template <T> void delete(T* toDelete, int itemCount) { template <T> |void| delete(|T*| toDelete, |int| itemCount) {
delete<T>(toDelete); delete<T>(toDelete);
} }
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */ /* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
template <T(Destructable)> void delete(T* toDelete, int itemCount) { template <T(Destructable)> |void| delete(|T*| toDelete, |int| itemCount) {
for (int i = 0; i < itemCount; i++;) for (|int| i = 0; i < itemCount; i++;)
toDelete[i].destruct(); toDelete[i].destruct();
delete<T>(toDelete); delete<T>(toDelete);
} }
/* We specilize on the trait Destructable to decide on whether or not the destructor should be called */ /* We specilize on the trait Destructable to decide on whether or not the destructor should be called */
template <T> void delete(T* toDelete) { template <T> |void| delete(|T*| toDelete) {
free(toDelete); free(toDelete);
} }
template <T(Destructable)> void delete(T* toDelete) { template <T(Destructable)> |void| delete(|T*| toDelete) {
toDelete->destruct(); toDelete->destruct();
free(toDelete); free(toDelete);
} }

View File

@@ -1,8 +1,8 @@
import io; import io;
typedef template <T> trivialContainer { typedef template <T> trivialContainer {
T data; |T| data;
void print() { |void| print() {
print(data); print(data);
} }
}; };

View File

@@ -1,11 +1,11 @@
template<T> T greater(T a, T b) { template<T> |T| greater(|T| a, |T| b) {
if (a > b) if (a > b)
return a; return a;
return b; return b;
} }
template<T> T lesser(T a, T b) { template<T> |T| lesser(|T| a, |T| b) {
if (a > b) if (a > b)
return b; return b;
return a; return a;

View File

@@ -3,36 +3,36 @@ import util;
import io; import io;
typedef template<T> vector (Destructable) { typedef template<T> vector (Destructable) {
T *data; |T*| data;
int size; |int| size;
int available; |int| available;
vector<T>* construct() { |vector<T>*| construct() {
size = 0; size = 0;
available = 8; available = 8;
data = new<T>(8); data = new<T>(8);
return this; return this;
} }
void destruct() { |void| destruct() {
delete<T>(data); delete<T>(data);
} }
bool resize(int newSize) { |bool| resize(|int| newSize) {
T* newData = new<T>(newSize); |T*| newData = new<T>(newSize);
if (!newData) if (!newData)
return false; return false;
for (int i = 0; i < lesser<int>(size, newSize); i++;) for (|int| i = 0; i < lesser<int>(size, newSize); i++;)
newData[i] = data[i]; newData[i] = data[i];
delete<T>(data, 0); delete<T>(data, 0);
return true; return true;
} }
T at(int index) { |T| at(|int| index) {
return get(index); return get(index);
} }
T get(int index) { |T| get(|int| index) {
if (index < 0 || index >= size) { if (index < 0 || index >= size) {
println("Vector access out of bounds! Retuning 0th element as sanest option"); println("Vector access out of bounds! Retuning 0th element as sanest option");
return data[0]; return data[0];
@@ -40,12 +40,12 @@ typedef template<T> vector (Destructable) {
return data[index]; return data[index];
} }
void set(int index, T dataIn) { |void| set(|int| index, |T| dataIn) {
if (index < 0 || index >= size) if (index < 0 || index >= size)
return; return;
data[index] = dataIn; data[index] = dataIn;
} }
void addEnd(T dataIn) { |void| addEnd(|T| dataIn) {
if (size < available) if (size < available)
size++; size++;
else else

View File

@@ -1,64 +1,64 @@
import io; import io;
typedef Vec2 { typedef Vec2 {
int x; |int| x;
int y; |int| y;
void print() { |void| print() {
print(x); print(x);
print(" "); print(" ");
print(y); print(y);
} }
Vec2 add(Vec2 other) { |Vec2| add(|Vec2| other) {
Vec2 toReturn; |Vec2| toReturn;
toReturn.x = x + other.x; toReturn.x = x + other.x;
toReturn.y = y + other.y; toReturn.y = y + other.y;
print(); print();
return toReturn; return toReturn;
} }
Vec2 subtract(Vec2 other) { |Vec2| subtract(|Vec2| other) {
Vec2 toReturn; |Vec2| toReturn;
toReturn.x = x - other.x; toReturn.x = x - other.x;
toReturn.y = y - other.y; toReturn.y = y - other.y;
print(); print();
return toReturn; return toReturn;
} }
Vec2 operator+(Vec2 other) { |Vec2| operator+(|Vec2| other) {
return add(other); return add(other);
} }
}; };
Vec2 operator-(Vec2 lhs, Vec2 rhs) { |Vec2| operator-(|Vec2| lhs, |Vec2| rhs) {
return lhs.subtract(rhs); return lhs.subtract(rhs);
} }
int main() { |int| main() {
Vec2 vector1; |Vec2| vector1;
Vec2 vector2; |Vec2| vector2;
vector1.x = 3; vector1.x = 3;
vector1.y = 9; vector1.y = 9;
vector2 = vector1; vector2 = vector1;
/* NOTE COMMENT /* NOTE COMMENT
Vec2 vector3; |Vec2| vector3;
vector3.x = vector1.x + vector2.x; vector3.x = vector1.x + vector2.x;
vector3.y = vector1.y + vector2.y; vector3.y = vector1.y + vector2.y;
vector2.print(); vector2.print();
*/ */
Vec2 addition = vector1 + vector2; |Vec2| addition = vector1 + vector2;
print("\n"); print("\n");
addition.print(); addition.print();
print("\nSubtraction\n"); print("\nSubtraction\n");
vector2.x = 100; vector2.x = 100;
vector2.y = 70; vector2.y = 70;
Vec2 subtraction = vector1 - vector2; |Vec2| subtraction = vector1 - vector2;
print("\n"); print("\n");
print(subtraction.x); print(" "); print(subtraction.y); print(subtraction.x); print(" "); print(subtraction.y);
print("\n"); print("\n");
return 0; return 0;
} }

View File

@@ -1,12 +1,12 @@
import io; import io;
int fibanacci(int num) { |int| fibanacci(|int| num) {
if (num < 2) if (num < 2)
return 1; return 1;
return fibanacci(num-1) + fibanacci(num-2); return fibanacci(num-1) + fibanacci(num-2);
} }
int main() { |int| main() {
print(fibanacci(4)); print(fibanacci(4));
print("\n"); print("\n");
return 0; return 0;

View File

@@ -1,7 +1,7 @@
/* Comment first! */ /* Comment first! */
import io; import io;
int main() { |int| main() {
println(1337); println(1337);
return 0; return 0;
} }

View File

@@ -2,24 +2,24 @@ import io;
import mem; import mem;
typedef ClassWithConstructor { typedef ClassWithConstructor {
int data; |int| data;
ClassWithConstructor* construct(int inData) { |ClassWithConstructor*| construct(|int| inData) {
data = inData; data = inData;
return this; return this;
} }
void printData() { |void| printData() {
println(data); println(data);
} }
}; };
int main() { |int| main() {
ClassWithConstructor object.construct(4); |ClassWithConstructor| object.construct(4);
//ClassWithConstructor object; //ClassWithConstructor object;
//object.construct(4); //object.construct(4);
object.printData(); object.printData();
int a = 8; |int| a = 8;
println(a); println(a);
ClassWithConstructor* objPtr = new<ClassWithConstructor>()->construct(11); |ClassWithConstructor*| objPtr = new<ClassWithConstructor>()->construct(11);
objPtr->printData(); objPtr->printData();
delete<ClassWithConstructor>(objPtr); delete<ClassWithConstructor>(objPtr);
return 0; return 0;

View File

@@ -1,27 +1,27 @@
import io; import io;
typedef DestructorPrint { typedef DestructorPrint {
char* myStr; |char*| myStr;
DestructorPrint* construct(char* str) { |DestructorPrint*| construct(|char*| str) {
myStr = str; myStr = str;
return this; return this;
} }
void destruct() { |void| destruct() {
println(myStr); println(myStr);
} }
}; };
typedef NoDistruction { typedef NoDistruction {
int a; |int| a;
void dummyFunc() {} |void| dummyFunc() {}
}; };
void indirPrint() { |void| indirPrint() {
DestructorPrint testObj.construct("Hello Destructors!"); |DestructorPrint| testObj.construct("Hello Destructors!");
NoDistruction dummy; |NoDistruction| dummy;
} }
int main() { |int| main() {
indirPrint(); indirPrint();
return 0; return 0;
} }

View File

@@ -1,8 +1,8 @@
import io; import io;
void nothing() {} |void| nothing() {}
int main() { |int| main() {
nothing(); nothing();
println("It was nothing"); println("It was nothing");
return 0; return 0;

View File

@@ -1,10 +1,10 @@
import io; import io;
template <T,J> void addAndPrint(T a, J b) { template <T,J> |void| addAndPrint(|T| a, |J| b) {
print(a+b); print(a+b);
} }
int main() { |int| main() {
addAndPrint<int,double>(10,12.14159); addAndPrint<int,double>(10,12.14159);
print("\n"); print("\n");

View File

@@ -1,17 +1,17 @@
import io; import io;
int ret1() { |int| ret1() {
return ret2() / 2; return ret2() / 2;
} }
int main() { |int| main() {
print(ret1()); print(ret1());
print(ret2()); print(ret2());
print("\n"); print("\n");
return 0; return 0;
} }
int ret2() { |int| ret2() {
return 2; return 2;
} }

View File

@@ -1,12 +1,11 @@
import io; import io;
template <T>T addAndPrint(T a, T b) { template <T> |T| addAndPrint(|T| a, |T| b) {
print(a+b); print(a+b);
return a+b; return a+b;
} }
int main() { |int| main() {
addAndPrint<int>(10,12); addAndPrint<int>(10,12);
print("\n"); print("\n");

View File

@@ -2,11 +2,11 @@ import mem;
import io; import io;
typedef AnObject { typedef AnObject {
int a; |int| a;
int b; |int| b;
char* c; |char*| c;
void print() { |void| print() {
print(a+b); print(a+b);
print("\n"); print("\n");
print(c); print(c);
@@ -15,8 +15,8 @@ typedef AnObject {
}; };
int main() { |int| main() {
AnObject* ptr = new<AnObject>(); |AnObject*| ptr = new<AnObject>();
ptr->a = 4; ptr->a = 4;
ptr->b = 7; ptr->b = 7;
ptr->c = "Hello decent memory! Quite a nice feeling\n"; ptr->c = "Hello decent memory! Quite a nice feeling\n";
@@ -24,4 +24,4 @@ int main() {
delete<AnObject>(ptr); delete<AnObject>(ptr);
return 0; return 0;
} }

View File

@@ -1,25 +1,25 @@
import io; import io;
typedef firstObject { typedef firstObject {
int objectNum; |int| objectNum;
int other; |int| other;
void print() { |void| print() {
print(other); print(other);
} }
void printInd() { |void| printInd() {
print(); print();
} }
}; };
typedef Int int; typedef Int int;
Int aliasNum; |Int| aliasNum;
int main() { |int| main() {
firstObject wooObject; |firstObject| wooObject;
wooObject.objectNum = 7; wooObject.objectNum = 7;
print(wooObject.objectNum); print(wooObject.objectNum);
firstObject* objPtr = &wooObject; |firstObject*| objPtr = &wooObject;
objPtr->objectNum = 42; objPtr->objectNum = 42;
print(objPtr->objectNum); print(objPtr->objectNum);
print("\n"); print("\n");

View File

@@ -2,30 +2,30 @@ import io;
import trivial_container; import trivial_container;
typedef RegularObject { typedef RegularObject {
int num; |int| num;
trivialContainer<char*> innerContainer; |trivialContainer<char*>| innerContainer;
void set(char* message, int number) { |void| set(|char*| message, |int| number) {
innerContainer.data = message; innerContainer.data = message;
num = number; num = number;
} }
char* get() { |char*| get() {
return innerContainer.data; return innerContainer.data;
} }
void print() { |void| print() {
print(num); print(num);
innerContainer.print(); innerContainer.print();
} }
}; };
typedef MyIntContainer trivialContainer<int>; typedef MyIntContainer trivialContainer<int>;
MyIntContainer roundabout; |MyIntContainer| roundabout;
RegularObject outsideDec; |RegularObject| outsideDec;
void print(trivialContainer<char*> toPrint) { |void| print(|trivialContainer<char*>| toPrint) {
print(toPrint.data); print(toPrint.data);
} }
int main() { |int| main() {
roundabout.data = 4; roundabout.data = 4;
outsideDec.set("Hello!", 5); outsideDec.set("Hello!", 5);
roundabout.print(); roundabout.print();

View File

@@ -1,24 +1,24 @@
import io; import io;
typedef objectA { typedef objectA {
int a; |int| a;
}; };
typedef BigObject { typedef BigObject {
objectA a; |objectA| a;
objectB b; |objectB| b;
int add() { |int| add() {
return a.a + b.b; return a.a + b.b;
} }
}; };
typedef objectB { typedef objectB {
int b; |int| b;
}; };
int main() { |int| main() {
BigObject c; |BigObject| c;
c.a.a = 4; c.a.a = 4;
c.b.b = 8; c.b.b = 8;
print(c.add()); print(c.add());

View File

@@ -1,14 +1,14 @@
import io; import io;
int addAndPrintInt(int a, int b) { |int| addAndPrintInt(|int| a, |int| b) {
print(a+b); print(a+b);
return a+b; return a+b;
} }
int main() { |int| main() {
print(addAndPrintInt(7,12)); print(addAndPrintInt(7,12));
print("\n"); print("\n");
return 0; return 0;
} }

View File

@@ -2,9 +2,9 @@ import io;
typedef template <T,J> TemplateTest { typedef template <T,J> TemplateTest {
T a; |T| a;
J b; |J| b;
void print() { |void| print() {
print("a: "); print("a: ");
print(a); print(a);
print("\n"); print("\n");
@@ -14,10 +14,10 @@ typedef template <T,J> TemplateTest {
} }
}; };
int main() { |int| main() {
TemplateTest<int, char*> test; |TemplateTest<int, char*>| test;
TemplateTest<char*, char*> test2; |TemplateTest<char*, char*>| test2;
test.a = 24; test.a = 24;
test.b = "Hello World"; test.b = "Hello World";
test2.a = "Pi incoming"; test2.a = "Pi incoming";

View File

@@ -2,9 +2,9 @@ import io;
typedef template <T> TemplateTest { typedef template <T> TemplateTest {
int a; |int| a;
T b; |T| b;
void print() { |void| print() {
print("a: "); print("a: ");
print(a); print(a);
print("\n"); print("\n");
@@ -14,10 +14,10 @@ typedef template <T> TemplateTest {
} }
}; };
int main() { |int| main() {
TemplateTest<int> test; |TemplateTest<int>| test;
TemplateTest<char*> test2; |TemplateTest<char*>| test2;
test.a = 5; test.a = 5;
test.b = 7; test.b = 7;
test2.a = 9; test2.a = 9;

View File

@@ -2,15 +2,15 @@
import io; import io;
typedef FirstObject { typedef FirstObject {
int objectNum; |int| objectNum;
void PrintSelf(int a) { |void| PrintSelf(|int| a) {
print(objectNum); print(objectNum);
print(a); print(a);
} }
}; };
int main() { |int| main() {
FirstObject wooObject; |FirstObject| wooObject;
wooObject.objectNum = 5; wooObject.objectNum = 5;
wooObject.PrintSelf(7); wooObject.PrintSelf(7);
print("\n"); print("\n");

View File

@@ -2,10 +2,10 @@ import io;
import trivial_container; import trivial_container;
typedef template <T> TemplateTest { typedef template <T> TemplateTest {
int a; |int| a;
T b; |T| b;
trivialContainer<T> c; |trivialContainer<T>| c;
void print() { |void| print() {
print("a: "); print("a: ");
print(a); print(a);
print("\n"); print("\n");
@@ -19,17 +19,17 @@ typedef template <T> TemplateTest {
typedef MyInt int; typedef MyInt int;
MyInt c; |MyInt| c;
template <T> T addAndPrint(T a, T b) { template <T> |T| addAndPrint(|T| a, |T| b) {
print(a+b); print(a+b);
return a+b; return a+b;
} }
int main() { |int| main() {
TemplateTest<int> test; |TemplateTest<int>| test;
TemplateTest<char*> test2; |TemplateTest<char*>| test2;
test.a = 5; test.a = 5;
test.b = 7; test.b = 7;
test.c.data = 1337; test.c.data = 1337;
@@ -40,7 +40,7 @@ int main() {
test.print(); test.print();
test2.print(); test2.print();
trivialContainer<char*> testImport; |trivialContainer<char*>| testImport;
testImport.data = "From another file! Whoh!"; testImport.data = "From another file! Whoh!";
testImport.print(); testImport.print();
print("\n"); print("\n");
@@ -49,4 +49,4 @@ int main() {
print("\n"); print("\n");
return 0; return 0;
} }

View File

@@ -1,9 +1,9 @@
import io; import io;
import mem; import mem;
int main() { |int| main() {
int b; |int| b;
int* a = &b; |int*| a = &b;
a [ 0 ] = 7; a [ 0 ] = 7;
print(a [ 0 ] ); print(a [ 0 ] );
print(*a); print(*a);

View File

@@ -7,23 +7,23 @@ typedef Trait2 (SecondTrait) {};
typedef TwoTrait (FirstTrait, SecondTrait) {}; typedef TwoTrait (FirstTrait, SecondTrait) {};
typedef AlreadySpecilized (FirstTrait, SecondTrait) {}; typedef AlreadySpecilized (FirstTrait, SecondTrait) {};
template <T> void OneTwoFunc(T obj) { template <T> |void| OneTwoFunc(|T| obj) {
println("No Traits"); println("No Traits");
} }
template <T(FirstTrait)> void OneTwoFunc(T obj) { template <T(FirstTrait)> |void| OneTwoFunc(|T| obj) {
println("First Trait"); println("First Trait");
} }
template <T(SecondTrait)> void OneTwoFunc(T obj) { template <T(SecondTrait)> |void| OneTwoFunc(|T| obj) {
println("Second Trait"); println("Second Trait");
} }
template <T(FirstTrait, SecondTrait)> void OneTwoFunc(T obj) { template <T(FirstTrait, SecondTrait)> |void| OneTwoFunc(|T| obj) {
println("Both Traits"); println("Both Traits");
} }
/* /*
template <AlreadySpecilized> void OneTwoFunc(AlreadySpecilized obj) { template <AlreadySpecilized> |void| OneTwoFunc(|AlreadySpecilized| obj) {
println("Already Specilized"); println("Already Specilized");
} }
*/ */
@@ -42,12 +42,12 @@ typedef template<T(FirstTrait, SecondTrait)> OneTwoObj {};
*}; *};
*/ */
int main() { |int| main() {
NoTraits a; |NoTraits| a;
Trait1 b; |Trait1| b;
Trait2 c; |Trait2| c;
TwoTrait d; |TwoTrait| d;
AlreadySpecilized e; |AlreadySpecilized| e;
OneTwoFunc<NoTraits>(a); OneTwoFunc<NoTraits>(a);
OneTwoFunc<Trait1>(b); OneTwoFunc<Trait1>(b);
@@ -57,11 +57,11 @@ int main() {
println(); println();
OneTwoObj<NoTraits> alpha; |OneTwoObj<NoTraits>| alpha;
OneTwoObj<Trait1> beta; |OneTwoObj<Trait1>| beta;
OneTwoObj<Trait2> gamma; |OneTwoObj<Trait2>| gamma;
OneTwoObj<TwoTrait> delta; |OneTwoObj<TwoTrait>| delta;
// OneTwoObj<AlreadySpecilized> epsilon; // |OneTwoObj<AlreadySpecilized>| epsilon;
OneTwoFunc<OneTwoObj<NoTraits>>(alpha); OneTwoFunc<OneTwoObj<NoTraits>>(alpha);
OneTwoFunc<OneTwoObj<Trait1>>(beta); OneTwoFunc<OneTwoObj<Trait1>>(beta);

View File

@@ -0,0 +1,2 @@
4
8

20
tests/typeExpr.krak Normal file
View File

@@ -0,0 +1,20 @@
import io;
typedef ClassWithConstructor {
|int| data;
|ClassWithConstructor*| construct(|int| inData) {
data = inData;
return this;
}
|void| printData() {
println(data);
}
};
|int| main() {
|ClassWithConstructor| object.construct(4);
object.printData();
|int| a = 8;
println(a);
return 0;
}

View File

@@ -3,24 +3,24 @@ import mem;
import vector; import vector;
typedef AbleToBeDestroyed (Destructable) { typedef AbleToBeDestroyed (Destructable) {
void destruct() { |void| destruct() {
println("Destroyed!"); println("Destroyed!");
} }
}; };
int main() { |int| main() {
vector<int> intVec.construct(); |vector<int>| intVec.construct();
intVec.addEnd(1); intVec.addEnd(1);
intVec.addEnd(3); intVec.addEnd(3);
intVec.addEnd(3); intVec.addEnd(3);
intVec.addEnd(7); intVec.addEnd(7);
for (int i = 0; i < intVec.size; i++;) for (|int| i = 0; i < intVec.size; i++;)
print(intVec.at(i)); print(intVec.at(i));
println(); println();
vector<AbleToBeDestroyed>* desVec = new<vector<AbleToBeDestroyed>>()->construct(); |vector<AbleToBeDestroyed>*| desVec = new<vector<AbleToBeDestroyed>>()->construct();
AbleToBeDestroyed testDestruct; |AbleToBeDestroyed| testDestruct;
desVec->addEnd(testDestruct); desVec->addEnd(testDestruct);
delete<vector<AbleToBeDestroyed>>(desVec); delete<vector<AbleToBeDestroyed>>(desVec);