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 ";" ;
type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | identifier | identifier WS template_inst ;
dec_type = "\|" WS type WS "\|" ;
template_inst = "<" WS type_list WS ">" ;
type_list = type_list WS "," WS type | type ;
@@ -41,11 +42,11 @@ identifier = alpha | alpha alphanumeric ;
right_shift = ">" ">" ;
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ;
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 | ;
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 | ;
parameter_list = parameter_list WS "," WS parameter | parameter ;
@@ -91,7 +92,7 @@ number = integer | floating_literal ;
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 ;
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 ;
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("typedef", 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_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("template_param_list", false));
collapseSymbols.push_back(Symbol("trait_list", false));
collapseSymbols.push_back(Symbol("dec_type", false));
}
Importer::~Importer() {

View File

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

View File

@@ -1,8 +1,8 @@
int NotPi = 3;
float Pi = 3.141592654;
|int| NotPi = 3;
|float| Pi = 3.141592654;
int fibanacci(int num) {
|int| fibanacci(|int| num) {
if (num < 2)
return 1;
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) */
template <T> T* malloc(int size) {
T* memPtr = 0;
template <T> |T*| malloc(|int| size) {
|T*| memPtr = 0;
__if_comp__ __C__ {
__simple_passthrough__ """
memPtr = malloc(size);
@@ -14,7 +14,7 @@ template <T> T* malloc(int size) {
return memPtr;
}
template <T> void free(T* memPtr) {
template <T> |void| free(|T*| memPtr) {
__if_comp__ __C__ {
__simple_passthrough__ """
free(memPtr);
@@ -22,9 +22,9 @@ template <T> void free(T* memPtr) {
}
}
template <T> int sizeof() {
int result = 0;
T testObj;
template <T> |int| sizeof() {
|int| result = 0;
|T| testObj;
__if_comp__ __C__ {
__simple_passthrough__ """
result = sizeof(testObj);
@@ -33,32 +33,32 @@ template <T> int sizeof() {
return result;
}
template <T> T* new(int count) {
template <T> |T*| new(|int| count) {
return malloc<T>( sizeof<T>() * count );
}
template <T> T* new() {
template <T> |T*| new() {
return new<T>(1);
}
/* 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);
}
/* Calling this with itemCount = 0 allows you to delete destructable objects without calling their destructors. */
template <T(Destructable)> void delete(T* toDelete, int itemCount) {
for (int i = 0; i < itemCount; i++;)
template <T(Destructable)> |void| delete(|T*| toDelete, |int| itemCount) {
for (|int| i = 0; i < itemCount; i++;)
toDelete[i].destruct();
delete<T>(toDelete);
}
/* 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);
}
template <T(Destructable)> void delete(T* toDelete) {
template <T(Destructable)> |void| delete(|T*| toDelete) {
toDelete->destruct();
free(toDelete);
}

View File

@@ -1,8 +1,8 @@
import io;
typedef template <T> trivialContainer {
T data;
void print() {
|T| data;
|void| print() {
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)
return a;
return b;
}
template<T> T lesser(T a, T b) {
template<T> |T| lesser(|T| a, |T| b) {
if (a > b)
return b;
return a;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -7,23 +7,23 @@ typedef Trait2 (SecondTrait) {};
typedef TwoTrait (FirstTrait, SecondTrait) {};
typedef AlreadySpecilized (FirstTrait, SecondTrait) {};
template <T> void OneTwoFunc(T obj) {
template <T> |void| OneTwoFunc(|T| obj) {
println("No Traits");
}
template <T(FirstTrait)> void OneTwoFunc(T obj) {
template <T(FirstTrait)> |void| OneTwoFunc(|T| obj) {
println("First Trait");
}
template <T(SecondTrait)> void OneTwoFunc(T obj) {
template <T(SecondTrait)> |void| OneTwoFunc(|T| obj) {
println("Second Trait");
}
template <T(FirstTrait, SecondTrait)> void OneTwoFunc(T obj) {
template <T(FirstTrait, SecondTrait)> |void| OneTwoFunc(|T| obj) {
println("Both Traits");
}
/*
template <AlreadySpecilized> void OneTwoFunc(AlreadySpecilized obj) {
template <AlreadySpecilized> |void| OneTwoFunc(|AlreadySpecilized| obj) {
println("Already Specilized");
}
*/
@@ -42,12 +42,12 @@ typedef template<T(FirstTrait, SecondTrait)> OneTwoObj {};
*};
*/
int main() {
NoTraits a;
Trait1 b;
Trait2 c;
TwoTrait d;
AlreadySpecilized e;
|int| main() {
|NoTraits| a;
|Trait1| b;
|Trait2| c;
|TwoTrait| d;
|AlreadySpecilized| e;
OneTwoFunc<NoTraits>(a);
OneTwoFunc<Trait1>(b);
@@ -57,11 +57,11 @@ int main() {
println();
OneTwoObj<NoTraits> alpha;
OneTwoObj<Trait1> beta;
OneTwoObj<Trait2> gamma;
OneTwoObj<TwoTrait> delta;
// OneTwoObj<AlreadySpecilized> epsilon;
|OneTwoObj<NoTraits>| alpha;
|OneTwoObj<Trait1>| beta;
|OneTwoObj<Trait2>| gamma;
|OneTwoObj<TwoTrait>| delta;
// |OneTwoObj<AlreadySpecilized>| epsilon;
OneTwoFunc<OneTwoObj<NoTraits>>(alpha);
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;
typedef AbleToBeDestroyed (Destructable) {
void destruct() {
|void| destruct() {
println("Destroyed!");
}
};
int main() {
vector<int> intVec.construct();
|int| main() {
|vector<int>| intVec.construct();
intVec.addEnd(1);
intVec.addEnd(3);
intVec.addEnd(3);
intVec.addEnd(7);
for (int i = 0; i < intVec.size; i++;)
for (|int| i = 0; i < intVec.size; i++;)
print(intVec.at(i));
println();
vector<AbleToBeDestroyed>* desVec = new<vector<AbleToBeDestroyed>>()->construct();
AbleToBeDestroyed testDestruct;
|vector<AbleToBeDestroyed>*| desVec = new<vector<AbleToBeDestroyed>>()->construct();
|AbleToBeDestroyed| testDestruct;
desVec->addEnd(testDestruct);
delete<vector<AbleToBeDestroyed>>(desVec);