From 32162ce50cac607d2fa8a8a58d2e076c10268e03 Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 12 Jan 2021 17:49:16 -0600 Subject: [PATCH] Updated examples and README to use proper syntax --- README.md | 6 +++--- examples/increment.cosmo | 17 +++++++++-------- examples/stress.cosmo | 2 +- examples/tostring.cosmo | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 856b930..fafa827 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Cosmo Cosmo is a portable scripting language loosely based off of Lua. Cosmo easily allows the user to extend the language through the use of Proto objects, which describe the behavior of Objects. For example the following is a simple Vector Proto which describes behavior for a Vector-like object. -``` +```lua proto Vector function __init(self) self.vector = [] @@ -24,11 +24,11 @@ end var vector = Vector() for (var i = 0; i < 4; i++) do - vector.push(i) + vector:push(i) end for (var i = 0; i < 4; i++) do - print(vector.pop() .. " : " .. vector[i]) + print(vector:pop() .. " : " .. vector[i]) end ``` diff --git a/examples/increment.cosmo b/examples/increment.cosmo index 908f6f2..6a993f2 100644 --- a/examples/increment.cosmo +++ b/examples/increment.cosmo @@ -17,22 +17,23 @@ proto Vector end function __iter(self) - self.iterIndex = 0 - - return self - end - - function __next(self) - return self.vector[self.iterIndex++] + // you don't *have* to make a new object, i just wanted to show off anonymous functions + return {__next = (function(self) + return self.vector[self.iterIndex++] + end), + iterIndex = 0, + vector = self.vector + } end end var vector = Vector() for (var i = 0; i < 100000; i++) do - vector.push(i) + vector:push(i) end +local total = 0 for i in vector do print(i) end \ No newline at end of file diff --git a/examples/stress.cosmo b/examples/stress.cosmo index d869e05..a45cf3f 100644 --- a/examples/stress.cosmo +++ b/examples/stress.cosmo @@ -11,5 +11,5 @@ end // stressing the GC for (var i = 0; ; i++) do var x = Test("Hello world " .. i) - x.print() + x:print() end \ No newline at end of file diff --git a/examples/tostring.cosmo b/examples/tostring.cosmo index 3a807e2..e6f3859 100644 --- a/examples/tostring.cosmo +++ b/examples/tostring.cosmo @@ -1,6 +1,6 @@ proto test function __init(self, x) - self.setArg(x) + self:setArg(x) end function __tostring(self) @@ -22,7 +22,7 @@ var t = test(1) for (var x = 1; x < 1000; x = x + 1) do for (var i = 1; i < 100; i = i + 1) do - t.setArg(i) + t:setArg(i) print(t) end