mirror of
https://github.com/CPunch/Cosmo.git
synced 2025-11-11 01:50:07 +00:00
syntax: 'var'->'let' 'function'->'func'
- 'var' has some weird scoping connotations with users of JS. better to change it to 'let', which will decide whether to make the variable a local or a global - 'func' looks visually appealing lol - some minor refactoring done in cparse.c
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// just testing continues and breaks
|
||||
|
||||
for (var x = 0; x < 700; x++) do
|
||||
for (var i = 0; true; i++) do
|
||||
var str = i .. "." .. x
|
||||
for (let x = 0; x < 700; x++) do
|
||||
for (let i = 0; true; i++) do
|
||||
let str = i .. "." .. x
|
||||
if (i == 998) then
|
||||
print(i .. " reached")
|
||||
break // exits the loop
|
||||
@@ -13,9 +13,9 @@ for (var x = 0; x < 700; x++) do
|
||||
end
|
||||
|
||||
// same example as the for loop but done manually using a while loop
|
||||
var i = 0
|
||||
let i = 0
|
||||
while true do
|
||||
var str = i .. "." .. x
|
||||
let str = i .. "." .. x
|
||||
if (i++ == 998) then
|
||||
print("done")
|
||||
break
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
var strtable = []
|
||||
var strLen = 4 // length of all strings to generate
|
||||
var AByte = "A":byte() // grabs the ascii value of 'A'
|
||||
let strtable = []
|
||||
let strLen = 4 // length of all strings to generate
|
||||
let AByte = "A":byte() // grabs the ascii value of 'A'
|
||||
|
||||
proto stringBuilder
|
||||
function __init(self, length)
|
||||
func __init(self, length)
|
||||
self.len = length
|
||||
end
|
||||
|
||||
// we are the iterator object lol
|
||||
function __iter(self)
|
||||
func __iter(self)
|
||||
self.x = 0
|
||||
return self
|
||||
end
|
||||
|
||||
function __next(self)
|
||||
var x = self.x++
|
||||
func __next(self)
|
||||
let x = self.x++
|
||||
|
||||
// if we've generated all the possible strings, return nil ending the loop
|
||||
if x >= 26 ^ self.len then
|
||||
@@ -22,8 +22,8 @@ proto stringBuilder
|
||||
end
|
||||
|
||||
// generate the string
|
||||
var str = ""
|
||||
for (var i = 0; i < self.len; i++) do
|
||||
let str = ""
|
||||
for (let i = 0; i < self.len; i++) do
|
||||
str = string.char(AByte + (x % 26)) .. str
|
||||
|
||||
x = math.floor(x / 26)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
local function fib(num)
|
||||
local func fib(num)
|
||||
if num <= 1 then
|
||||
return num
|
||||
else
|
||||
@@ -6,6 +6,6 @@ local function fib(num)
|
||||
end
|
||||
end
|
||||
|
||||
for (var i = 1; i < 40; i++) do
|
||||
for (let i = 1; i < 40; i++) do
|
||||
print("The fib number of " .. i .. " is " .. fib(i))
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
var object = {
|
||||
let object = {
|
||||
__setter = [
|
||||
"field1" = function(self, val)
|
||||
print("setter for field1 called!")
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
proto Vector
|
||||
function __init(self)
|
||||
func __init(self)
|
||||
self.vector = []
|
||||
self.x = 0
|
||||
end
|
||||
|
||||
function push(self, val)
|
||||
func push(self, val)
|
||||
self.vector[self.x++] = val
|
||||
end
|
||||
|
||||
function pop(self)
|
||||
func pop(self)
|
||||
return self.vector[--self.x]
|
||||
end
|
||||
|
||||
function __index(self, key)
|
||||
func __index(self, key)
|
||||
return self.vector[key]
|
||||
end
|
||||
|
||||
function __iter(self)
|
||||
func __iter(self)
|
||||
// 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++]
|
||||
@@ -27,9 +27,9 @@ proto Vector
|
||||
end
|
||||
end
|
||||
|
||||
var vector = Vector()
|
||||
let vector = Vector()
|
||||
|
||||
for (var i = 0; i < 100000; i++) do
|
||||
for (let i = 0; i < 100000; i++) do
|
||||
vector:push(i)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
proto Range
|
||||
function __init(self, x)
|
||||
func __init(self, x)
|
||||
self.max = x
|
||||
end
|
||||
|
||||
function __iter(self)
|
||||
func __iter(self)
|
||||
self.i = 0
|
||||
return self
|
||||
end
|
||||
|
||||
function __next(self)
|
||||
func __next(self)
|
||||
if self.i >= self.max then
|
||||
return nil // exit iterator loop
|
||||
end
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
proto Test
|
||||
function __init(self, x)
|
||||
func __init(self, x)
|
||||
self.x = x
|
||||
end
|
||||
|
||||
function print(self)
|
||||
func print(self)
|
||||
print(self.x)
|
||||
end
|
||||
end
|
||||
|
||||
// stressing the GC
|
||||
for (var i = 0; i < 100000; i++) do
|
||||
var x = Test("Hello world " .. i)
|
||||
for (let i = 0; i < 100000; i++) do
|
||||
let x = Test("Hello world " .. i)
|
||||
x:print()
|
||||
end
|
||||
@@ -1,7 +1,7 @@
|
||||
var words = "hello world! this is a sentence with words separated by space":split(" ")
|
||||
let words = "hello world! this is a sentence with words separated by space":split(" ")
|
||||
|
||||
var str = ""
|
||||
for (var i = 0; i < #words; i++) do
|
||||
let str = ""
|
||||
for (let i = 0; i < #words; i++) do
|
||||
str = str .. words[i]
|
||||
print(words[i])
|
||||
end
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// crafts a dummy proto
|
||||
proto test
|
||||
function __init(self) end
|
||||
func __init(self) end
|
||||
end
|
||||
|
||||
// instance of test
|
||||
var obj = test()
|
||||
let obj = test()
|
||||
|
||||
test.__index = function(self, key)
|
||||
print("__index called!")
|
||||
|
||||
@@ -17,16 +17,16 @@ assert(2 / 5 + 3 / 5 == 1, "PEMDAS check #2 failed!")
|
||||
// iterator test
|
||||
|
||||
proto Range
|
||||
function __init(self, x)
|
||||
func __init(self, x)
|
||||
self.max = x
|
||||
end
|
||||
|
||||
function __iter(self)
|
||||
func __iter(self)
|
||||
self.i = 0
|
||||
return self
|
||||
end
|
||||
|
||||
function __next(self)
|
||||
func __next(self)
|
||||
if self.i >= self.max then
|
||||
return nil // exit iterator loop
|
||||
end
|
||||
@@ -35,7 +35,7 @@ proto Range
|
||||
end
|
||||
end
|
||||
|
||||
var total = 0
|
||||
let total = 0
|
||||
for i in Range(100) do
|
||||
total = total + i
|
||||
end
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
proto test
|
||||
function __init(self, x)
|
||||
func __init(self, x)
|
||||
self:setArg(x)
|
||||
end
|
||||
|
||||
function __tostring(self)
|
||||
var total = 1
|
||||
func __tostring(self)
|
||||
let total = 1
|
||||
|
||||
for (var i = self.x; i > 0; i = i - 1) do
|
||||
for (let i = self.x; i > 0; i = i - 1) do
|
||||
total = total * i;
|
||||
end
|
||||
|
||||
return "The factorial of " .. self.x .. " is " .. total
|
||||
end
|
||||
|
||||
function setArg(self, x)
|
||||
func setArg(self, x)
|
||||
self.x = x
|
||||
end
|
||||
end
|
||||
|
||||
var t = test(1)
|
||||
let t = test(1)
|
||||
|
||||
for (var x = 1; x < 1000; x = x + 1) do
|
||||
for (var i = 1; i < 100; i = i + 1) do
|
||||
for (let x = 1; x < 1000; x = x + 1) do
|
||||
for (let i = 1; i < 100; i = i + 1) do
|
||||
t:setArg(i)
|
||||
|
||||
print(t)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// adds all args passed (expects numbers)
|
||||
function add(start, ...args)
|
||||
func add(start, ...args)
|
||||
// starting at `start`, add up all numbers passed
|
||||
local total = start
|
||||
for val in args do
|
||||
|
||||
Reference in New Issue
Block a user