From 3b7b6d9a84bfc5f328c2ed78ca72a836a67468ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Patrick=20Sch=C3=B6nberger?= Date: Thu, 12 Aug 2021 08:52:59 +0200 Subject: [PATCH] changed example --- src/main.cpp | 2 +- test/{test2.toc => test.toc} | 67 +++++++++++++++++++++++------------- test/test1.toc | 40 --------------------- 3 files changed, 44 insertions(+), 65 deletions(-) rename test/{test2.toc => test.toc} (51%) delete mode 100644 test/test1.toc diff --git a/src/main.cpp b/src/main.cpp index 6792e83..5b53b10 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ using namespace antlr4; int main(int argc, const char * argv[]) { - std::ifstream ifs("test/test2.toc"); + std::ifstream ifs("test/test.toc"); ANTLRInputStream input(ifs); diff --git a/test/test2.toc b/test/test.toc similarity index 51% rename from test/test2.toc rename to test/test.toc index c65596a..b3ff1ea 100644 --- a/test/test2.toc +++ b/test/test.toc @@ -1,5 +1,39 @@ +// simply declare external functions func puts(str : char*) : void; +// global context can contain functions, +// structs, global variables and namespaces +var global1 : int; +var global2 : double = 123.45; + +func globalFunc() : void { + //puts("Hello\n"); +} + +// structs and functions can be declared generic +// by providing a list of placeholder typenames +struct S1 { + t1: T1; + t2: T1; + + m1() : T2 { + return this->t1 + this->t2; + } +} + +struct S2 { + s: char *; + abc(): S2 { } + xyz(): S2 { } +} + +func generic1(a1 : A, a2 : A) : A { + return a1 + a2; +} + +// namespaces can contain everything that +// the global context can + namespace N1 { var v1 : int; func f1() : void { @@ -8,19 +42,21 @@ namespace N1 { struct S1 { test : char *; } - // hallo + // nested namespaces namespace N2 { - var v1 : int; //buige + var v1 : int; struct S1 { i1 : int; i2 : int; i3 : int; m1(i: int) : int { + // implicit 'this' parameter, pointer to containing struct this->i3 = this->i1 * this->i2; - f1(v1); - N1::f1(N1::v1); + // lookup is done hierarchically + f1(v1); // this is basically N1::N2::f1(N1::N2::v1); + N1::f1(N1::v1); // the rest becomes exactly what they say N2::f1(N2::v1); N1::N2::f1(); @@ -29,6 +65,7 @@ namespace N1 { } func f1() : void { + // these have the same type var s1 : N1::N2::S1; var s2 : S1; @@ -37,25 +74,6 @@ namespace N1 { } } -struct S1 { - t1: T; - t2: T; - - m1() : T { - return this->t1 + this->t2; - } -} - -struct S2 { - s: char *; - abc(): S2 { } - xyz(): S2 { } -} - -func generic1(a1 : A, a2 : A) : A { - return a1 + a2; -} - func main(argc : int, argv : char**) : int { var s1 : N1::N2::S1; var s2 : N1::N2::S1; @@ -66,7 +84,8 @@ func main(argc : int, argv : char**) : int { N1::N2::f1(); - var s4 : S1; + // one 'copy' is compiled for every unique instantiation + var s4 : S1; s4.t1 = 123; s4.t2 = 456; s4.m1(); diff --git a/test/test1.toc b/test/test1.toc deleted file mode 100644 index 1f604c0..0000000 --- a/test/test1.toc +++ /dev/null @@ -1,40 +0,0 @@ -var abc: int; -var def: int; -var ghi: int; - -func f1 (a : int, b : int, cs: int[]) : int { - var i1 : int; - var s1 : S1; - if a != i1 { - var i2 : int = 987; - return i2; - } - print(s1.i1); - print(s1.a1[0]); - return 133; -} - -struct S1 { - i1 : int; - i2 : int; - a1 : int[10]; - a2 : int[10]*; - a2 : int[]; - - m1() : void { - while i1 < i2 { - i1 = m2(i1); - - i2 = i1 + i2 * (i2 - 45) + 4; - - print(12345); - } - } - - m2(i: int) : int { - var j : int = 0; - while j < 10 { - print(a1[j]); - } - } -} \ No newline at end of file -- 2.50.1