Skip to content
marcelklehr edited this page May 30, 2012 · 8 revisions

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)

Things to consider

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!

Prototypes

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"

Data types

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 a read method, which returns a readable, can be used to represent a string (Not a string object, that's important!).
  • countable: every object, that has a count method, which returns a countable, can be used to represent an integer.
  • calculatable: every object, that has a calc method, which returns a calculatable, can be used to represent a float.
  • decidable: every object that has a decide method, that returns a deciable, can be used to represent a boolean.
  • iteratable: every object, that has an each method, can be iterated on by passing a callback.
  • callable: every object, that provides a call property, containing a callable can be called like a function.

Primal types

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)

Object

You can construct an object using

object = [property : "foo", anotherone: "bar"]

String

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'

Integer

You can construct an integer using

int = 78

Float

You can construct a float using

float = 483.25

Boolean

You can construct a boolean using

bool = true
bool = false

Function

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."
}

List

You can construct a list using

shoppingList = "shoes", "jeans", "t-shirts", "bananas"

Nil

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...).

Control structures

if

if string == "foobar" {
  puts "TRuE, cattle!"
}

loop

--unimplemented

loop {
  "WHAHAHAHA, you can't kill me! I'm immortal."
}
Clone this wiki locally