charly-lang / charly

🐈 The Charly Programming Language | Written by @KCreate
https://charly-lang.github.io/charly/
MIT License
199 stars 10 forks source link

Make the primitive types classes #41

Closed KCreate closed 7 years ago

KCreate commented 7 years ago

When doing a member expression or index expression on a non-object or non-array, the interpreter should look for a function on classes defined in primitives.charly.

This could work like this:


class Numeric {
  func times(callback) {
    let i = 0;

    # self automatically set to the number
    # can't be overwritten (see issue #31)
    while (i < self) {
        callback(i);
        i = i + 1;
    };
  };
};

"test".reverse(); # "tset"
25.times(func(i) {}); # Just like in ruby

Every class function or object function will get a pointer called self that will always point to the current object the function runs in. In this case it will be pointed to a primitive value. The interpreter redirects these calls at runtime.

KCreate commented 7 years ago

Method names for the overloaded operators should be (for now)

KCreate commented 7 years ago

The ++ and -- operators will be added in a seperate PR

KCreate commented 7 years ago

Implemented via #58