Skip to content

Instantly share code, notes, and snippets.

@STRd6
Created July 5, 2013 00:10
Show Gist options
  • Select an option

  • Save STRd6/5930901 to your computer and use it in GitHub Desktop.

Select an option

Save STRd6/5930901 to your computer and use it in GitHub Desktop.
_____.___.      .__          _________            .__        __
\__  |   | ____ |  |   ____ /   _____/ ___________|__|______/  |_
 /   |   |/  _ \|  |  /  _ \\_____  \_/ ___\_  __ \  \____ \   __\
 \____   (  <_> )  |_(  <_> )        \  \___|  | \/  |  |_> >  |
 / ______|\____/|____/\____/_______  /\___  >__|  |__|   __/|__|
 \/                                \/     \/         |__|

YoloScript is a stupid little language with functions, objects, and messages.

Math

5 + 3 # => 8

7 - 2 # => 5

" " * 2 # => "  "

Functions

add = (a, b) ->
  a + b

add(3, 5) # => 8

Objects

Objects can literally be declared in your code.

someGuy =
  name: "Daniel X Moore"
  age: 28

puts someGuy.name

Operators

Operators are just messages.

Point = (x, y) ->
  x: x
  y: y
  +: (other) ->
    Point(x + other.x, y + other.y)
  -: (other) ->
    self + (-other)
  -@: ->
    Point(-x, -y)

Point(3, 5) + Point(1, 4) # => Point(4, 9)

Objects and methods

Duder = ->
  name: "Bro Duderman"
  greet: ->
    "Hi, I'm #{name}"

dude = Duder

dude.greet # => "Hi, I'm Bro Duderman"

fn = dude:greet

fn # => "Hi, I'm Bro Duderman"

dude2 =
  name: "Duder Manington"
  greet: fn

dude2.greet # => "Hi, I'm Duder Manington"

[2:05 PM] Daniel X: I'd say it's a function in the current scope/binding
so Duder = -> would be __binding__.send("Duder=", function() {...})
[2:07 PM] Daniel X: and dude = Duder would be:

    _ref = __binding__.send("Duder")
   __binding__.send("dude=", _ref)
or that's my understanding right now
[2:09 PM] Daniel X: and __binding__ can respond to messages ending in "=" by looking up the closure stack and setting the appropriate hash entry

Closures

Lexical scoping!

Accumulator = ->
  value = 0

  (n) ->
    value += n

accumulator = Accumulator

accumulator(5)

DSLs

mainMenu = Menu.create ->
  gamestate "Story", StorySetupState

  gamestate "Versus", MatchSetupState

  submenu "Mini-Games"
    minigame "Whack-A-Mole"
    minigame "Sumo Push"
    minigame "Paint"

  submenu "Options"
    submenu "VS Teams"
      teamChooser "home"
      teamChooser "away"
    volumeChooser
      option: "music"
      source: Music
    volumeChooser
      option: "sfx"
      source: Sound

Method Missing

x =
  doesNotUnderstand: (message, args...) ->
    puts "Received: '#{message}' with #{args}"

x / 3 # => "Received: '/' with [3]"

Destructuring Assignment

a, b = b, a

{name, age} = person

Implementation Thoughts

Send

Object.defineProperty Object::, "__send__",
  value: (message, args...) ->
    if @[message]
      @[message](args...)
    else
      @doesNotUnderstand?(message, args...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment