rsms / move

A simple, functional-biased, prototypal and powerful programming language that runs on any ES3 (or better) JavaScript platform, aimed toward people new to programming
303 stars 23 forks source link

Implementing Slices #4

Closed holtwick closed 13 years ago

holtwick commented 13 years ago

You wrote about implementation of Slices:

"Slices would indeed be neat. Any suggestion on how this could be implemented? If you want to experiment, you want to create a new AST mutator (copy-paste one in lib/ast-mutators.js), enable it in the "compile" function (lib/index.js) and then hack in the support into the AST mutator. This provides a very flexible workflow as you can inspect the AST without, while and after your mutator applied."

I had a look at it and found out that we first need to modify the parser.js to get an AST with the needed information. I think you know the code better than I do, but I think we need something here:

function subscripts(expr, allow_calls) {
    // ...
    if (is("punc", "[")) {
      next();
      return subscripts(as("sub", expr, prog1(expression, curry(expect, "]"))), allow_calls);
    }

After having the parser recognizing these cases:

object[expr:expr]
object[expr:]
object[:expr]
object[:]

or at least the first one, I could try to create the AST mutator. Maybe you could give me some help here? Thanks.

rsms commented 13 years ago

I've now implemented this in the core as well as provided implementation for Array types. Check out and test https://github.com/rsms/move/commit/d47f880c4b7c6b29709a60fbb3a1edd98b3ddba8 (latest master as of writing this)

rsms commented 13 years ago

Well, reading/extracting slices from text is also implemented in a way, although we got it "for free". E.g.

print "hello"[1:3]  # --> "el"
rsms commented 13 years ago

Implemented, tested and documented.

holtwick commented 13 years ago

Awesome. Thanks! Here a little addition https://github.com/holtwick/move/commit/9e117dcfb4e130a71c1395ae201ac90f907a900a