Skip to content

Instantly share code, notes, and snippets.

@creationix
Created July 21, 2016 03:51
Show Gist options
  • Save creationix/601b307b34cabf1f40dfd17565cba47f to your computer and use it in GitHub Desktop.
Save creationix/601b307b34cabf1f40dfd17565cba47f to your computer and use it in GitHub Desktop.

Objects in this language are defined as types. They have properties with default values that can be overridden in the constructor literal syntax.

There are no methods, but instead have conditional message handlers. The handlers are scoped by the state of the internal properties. Depending on the state the value is in, different messages can be responded to in different ways.

Also values can actively delete themselves causing all references to them to be deleted and replaces with a null type that doesn't respond to anything. (thus freeing up memory)

type Ball:
  life = 100
  x = 0
  y = 0

  when life > 0:
    on draw(canvas):
      canvas.circle(x, y, 10, :red)

    on update(ms):
      life -= 1
      x += ms
  else:
    delete

type Canvas:
  on circle(x,y,r,c):
    ...

canvas = <Canvas>
ball = <Ball x=64 y=64>
ball.update(10)
ball.draw(canvas)
@creationix
Copy link
Author

Actually the else: delete isn't needed since the runtime can prove that no state will ever change (unless we allow outside mutability beyond the initial setup). So if the end of a block ends in a state with no listeners, it can safely be replaces by the null type.

@cadorn
Copy link

cadorn commented Jul 21, 2016

This could fit perfectly into my ccJSON parser. I have been looking for micro languages to embed that can deal with object property manipulation in response to messages.

What would be the easiest way to compile a syntax such as this to pure JS?

@creationix
Copy link
Author

@cadorn, what do you mean object property manipulation?

This language is still in early design, not even sure I'll make it.

But writing a compiler that emits plain JS shouldn't be too hard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment