Skip to content

Instantly share code, notes, and snippets.

@gosukiwi

gosukiwi/demo

Last active Jun 14, 2020
Embed
What would you like to do?
// VARIABLES
// no need to specify type
a = 1.2 // float
name = "Federico" // string
// you can still do it if you want
x as float
x = 1.2
function Double(x as integer)
result = 2 * x
endfunction result
value = Double(2) // the compiler know this is an integer
// Arrays
arr = [1, 2, 3] // literal, in-line definition
other as integer[]
other.insert(1)
// FUNCTIONS
function Greet(name = "Thomas O'Malley") // default argument
greeting = "Hello, ${name}!" // string interpolation, same as "Hello, " + name + "!"
endfunction
// TYPES
type Person
name as string
age as integer
likes as string default "Cats" // default value
endtype
person = new Person("Federico", 30) // built-in constructor for types
person.likes // => "Cats"
// MACROS
// This is where things get crazy. A macro is run at compile-time, and will inline it's contents whenever it's called.
// You can think of it as a function that, when called, will replace itself with it's contents
macro each(items as integer[], callback as func(of integer)) // note that macros can take functions as parameters
for i = 0 to items.length - 1
callback(items[i])
next i
endmacro
each([1, 2, 3], function (num as integer)
Print("Number is ${num}")
endfunction)
// will compile to...
dim _some_internal_array_name[3] as integer = [1, 2, 3]
for i = 0 to _some_internal_array_name.length - 1
num = items[i]
Print("Number is " + Str(num))
next i
// GENERICS
// Macros can use the `any` type, which is a simple form of doing generics
// Note that when called with a real type, all `any` must be the same type (eg: string, integer, etc)
macro map(items as any[], callback as func(of any))
result as any[]
for i = 0 to items.length - 1
result.insert(callback(items[i]))
next i
endmacro result
// Now `map` works with integers...
result = map([1, 2, 3], function (num as integer)
double = num * num
endfunction double)
// As well as strings
result2 = map(["1", "2", "3"], function (num as string)
double = num + num
endfunction double)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment