Examples in Mogemoge.

hello world!

The simplest example in Mogemoge. In Mogemoge, any statements must be terminated by a ';' symbol.

print "hello world!";

assignment statement

An assignment statement assigns an value to a variable which is at the left side of a operator '='.

An assignemnt statement has also the function to create a variable if the variable has not been assigned any value.

a = 1; # a new a variable assigned an integer
b = "hoge"; # a new b variable assigned a string
a = a + b; # a value of a replaced by a string "1hoge"
c = d; # error! d has not been assigned any values

sum from 1 to 10

The while loop example:

n = 1;
s = 0;
while (n <= 10) {
  s = s + n;
  n = n + 1;
}
print " sum = " + s;

method definition? (method object)

In Mogemoge, a method is also a first value generated by a method() {...} clause. If the value is assigned to a variable, the method can be invoked by ordinary style in other C or Java style programming languages.

sum = method(num) { # a method is assigned to 'sum'
  n = 0;
  s = 0;
  while (n <= num) {
    s = s + n;
    n = n + 1;
  }
  result s;
};

print "sum 10 " + sum(10); # invoke 'sum' method

hanoi

The following is an example of solving hanoi problem. It is available to use recursive call in Mogemoge.

hanoi = method(from, to, n) {
  if (n > 0) {
    m = 6 - from - to;
    hanoi(from, m, n - 1);
    print "move " + from + " -> " + to;
    hanoi(m, to, n - 1);
  }
};

hanoi(1, 3, 3); # move 3 dishes from 1 to 3.

a bank account sample

Mogemoge has no 'class' because it is prototype-based language as in Javascript/Self. You can create an object by the object {...} clause, and duplicate it by new operator.

Account = object {
  v = 0;
  deposit = method(add) { v = v + add; };
  withdraw = method(get) { v = v - get; };
};

a = new Account; # create an account 'a'
b = new Account; # create an account 'b'
a.deposit(100);
b.deposit(200)
a.withdraw(50)
b.withdraw(40)
print "a outstanding : " + a.v; # '50'
print "b outstanding : " + b.v; # '160'

composition

Mogemoge does not have inheritance or mix-in mechanism as in other OO languages. Instead of such a feature, Mogemoge has an operation to 'compose' two objects by the operator '+'. Composited object has all variables that original two object have.

A = object {
  foo = method { print "foo!"; };
};
B = object {
  bar = method { print "bar!"; };
};

a = A + B; # composite A and B
a.foo(); # calls the method 'foo' of 'A'
a.bar(); # calls the method 'bar' of 'B'

delegate

Mogemoge has delegate mechanism like C#. A method value of an object can be used to invoke the method which receiver is that object. A method value is always related to an object.

Dog = object {
  bark = method() { print "bow wow"; };
};

m = Dog.bark; # m is the method object that receiver is a 'Dog' object
m(); # bow wow!

inject a new method

A variable of an object can be added or replaced by a new values, so you can add or replace a method outside of 'object {...}' clause.

An assigned method will be related to a new receiver object. For example, though the following 'Cat.bark' method has an receiver 'Cat', the 'Dog.mimic_cat' has an receiver 'Dog'. This is a little difference between Mogemoge and other prototype-based languages as in Javascript.

Cat = object {
  bark = method() { print "mew mew"; };
};

Dog.mimic_cat = Cat.bark; # assigns Cat.bark method to Dog.mimic_cat
Dog.mimic_cat(); # mew mew! an receiver is 'Dog'!