-
Notifications
You must be signed in to change notification settings - Fork 0
Docs
Magnet is a dynamically (duck-)typed, object-oriented, syntax-driven and potentially awesome programming language that currently builds on top of node.js.
Magnet consists of three main things:
- The interpreter, called Ferrite, incorporating a virtual machine
- The standart library (which currently does not, yet, exist -- simple programs work, though)
- The command line interface for Ferrite (use
magnet filename.mg
)
There are no classes. Furthermore, there are no data types (This is a little tricky to explain here, but read on, read on...). Every value is an object. Every statement and control-structure is an expression (meaning, it will return a value). Everything else is simple, really!
In Magnet, your object hasn't got a class, but an ancestor: its prototype. Even if your object hasn't got any property, it still mirrors those of its ancestor! By setting a property, you only change the property of the current object! As long, as you don't have direct access to an object's prototype, the prototype is immutable.
That's like an unfinished drawing. If you put a slide on it, you can draw whatever comes into your mind, but you never change the original drawing, because it lies under your slide. But if you change the original drawing, you will still see the changes when putting the slide on top of it (unless you already painted over where the changes are).
In Magnet the fork
method resembles putting a slide on top of an object:
object.dog = 'woof'
forkedObject = object.fork()
puts forkedObject.dog // "woof"
object.cat = 'meow'
puts forkedObject.cat // "meow"
Magnet pushes ruby-like duck-typing one step further: Instead of classes, everything is duck-typed! Even primal types are just normal objects (with magic capabilities, admittedly, but you can overwrite those...).
Available duck types are
-
readable
: every object, that has aread
method, which returns a readable, can be used to represent a string (Not a string object, that's important!). -
countable
: every object, that has acount
method, which returns a countable, can be used to represent an integer. -
calculatable
: every object, that has acalc
method, which returns a calculatable, can be used to represent a float. -
decidable
: every object that has adecide
method, that returns a deciable, can be used to represent a boolean. -
iteratable
: every object, that has aneach
method, can be iterated on by passing a callback. -
callable
: every object, that provides acall
property, containing a callable can be called like a function.
Of course, you need some primal types, that you can use to build the above duck types.
So, let me introduce you to the primal types in Magnet:
- Object
- String (the primal readable)
- Integer (the primal countable; also a calculatable)
- Float (the primal calculatable)
- Boolean (the primal decidable)
- Function (the primal callable)
- List (iteratable)
- Nil (nothing)
You can construct an object using
object = [property : "foo", anotherone: "bar"]
You can construct a string using
str = "double quoted"
str = 'single quoted'
You can integrate expressions into such string using
years = 5
str = 'I am #{years} years old'
The above resolves to 'I am 5 years old'
You can construct an integer using
int = 78
You can construct a float using
float = 483.25
You can construct a boolean using
bool = true
bool = false
You can construct a function using
func = {param1 param2 ::
param1+param2
}
Functions automatically return the last calculated expression. If you want to return before that, use return
.
You can also omit the parameters:
func = {
return "Have a string! It's very tasty."
}
You can construct a list using
shoppingList = "shoes", "jeans", "t-shirts", "bananas"
Everything that doesn't exist is nil. But, poor Nil can't do anything, you know, it's just there, living its lonely life. Currently you can't even compare nil to any real object (but, trust me, you'll know, when something's nil -- magnet will eat your dog let you know...).
if string == "foobar" {
puts "TRuE, cattle!"
}
--unimplemented
loop {
"WHAHAHAHA, you can't kill me! I'm immortal."
}