Updated examples and README to use proper syntax

This commit is contained in:
CPunch 2021-01-12 17:49:16 -06:00
parent ab86e19cfe
commit 32162ce50c
4 changed files with 15 additions and 14 deletions

View File

@ -1,7 +1,7 @@
# Cosmo # 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. 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 proto Vector
function __init(self) function __init(self)
self.vector = [] self.vector = []
@ -24,11 +24,11 @@ end
var vector = Vector() var vector = Vector()
for (var i = 0; i < 4; i++) do for (var i = 0; i < 4; i++) do
vector.push(i) vector:push(i)
end end
for (var i = 0; i < 4; i++) do for (var i = 0; i < 4; i++) do
print(vector.pop() .. " : " .. vector[i]) print(vector:pop() .. " : " .. vector[i])
end end
``` ```

View File

@ -17,22 +17,23 @@ proto Vector
end end
function __iter(self) function __iter(self)
self.iterIndex = 0 // you don't *have* to make a new object, i just wanted to show off anonymous functions
return {__next = (function(self)
return self return self.vector[self.iterIndex++]
end end),
iterIndex = 0,
function __next(self) vector = self.vector
return self.vector[self.iterIndex++] }
end end
end end
var vector = Vector() var vector = Vector()
for (var i = 0; i < 100000; i++) do for (var i = 0; i < 100000; i++) do
vector.push(i) vector:push(i)
end end
local total = 0
for i in vector do for i in vector do
print(i) print(i)
end end

View File

@ -11,5 +11,5 @@ end
// stressing the GC // stressing the GC
for (var i = 0; ; i++) do for (var i = 0; ; i++) do
var x = Test("Hello world " .. i) var x = Test("Hello world " .. i)
x.print() x:print()
end end

View File

@ -1,6 +1,6 @@
proto test proto test
function __init(self, x) function __init(self, x)
self.setArg(x) self:setArg(x)
end end
function __tostring(self) function __tostring(self)
@ -22,7 +22,7 @@ var t = test(1)
for (var x = 1; x < 1000; x = x + 1) do for (var x = 1; x < 1000; x = x + 1) do
for (var i = 1; i < 100; i = i + 1) do for (var i = 1; i < 100; i = i + 1) do
t.setArg(i) t:setArg(i)
print(t) print(t)
end end