예제 #1
0
파일: classes.php 프로젝트: nrk/macchiato
$coffee = new Macchiato\CoffeeScript(COFFEESCRIPT_PATH);

$context = $coffee->createContext();
$context->registerFunction('alert', function($msg) { echo "$msg\n"; });

$coffee->execute(
<<<EOC
class Animal
  constructor: (@name) ->

  move: (meters) ->
    alert @name + " moved " + meters + "m."

class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5

class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45

sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"

sam.move()
tom.move()
EOC
, $context);
예제 #2
0
파일: overview.php 프로젝트: nrk/macchiato
$coffee->execute(
<<<EOC
# Assignment:
number   = 42
opposite = true

# Conditions:
number = -42 if opposite

# Functions:
square = (x) -> x * x

# Arrays:
list = [1, 2, 3, 4, 5]

# Objects:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

# Splats:
race = (winner, runners...) ->
  print winner, runners

# Existence:
alert "I knew it!" if elvis?

# Array comprehensions:
cubes = (math.cube num for num in list)
storeResult cubes
EOC
, $context);
예제 #3
0
파일: grade.php 프로젝트: nrk/macchiato
<?php

require 'shared.php';

cache_coffeescript();

$coffee = new Macchiato\CoffeeScript(COFFEESCRIPT_PATH);

$eldest = $coffee->execute(
<<<EOC
grade = (student) ->
  if student.excellentWork
    "A+"
  else if student.okayStuff
    if student.triedHard then "B" else "B-"
  else
    "C"

eldest = if 24 > 21 then "Liz" else "Ike"
EOC
);

var_dump($eldest);
예제 #4
0
파일: reducer.php 프로젝트: nrk/macchiato
<?php

require 'shared.php';

cache_coffeescript();
cache_underscore();

$coffee = new Macchiato\CoffeeScript(COFFEESCRIPT_PATH);

$context = $coffee->createContext();
$context->evaluateFile(UNDERSCOREJS_PATH);
$context->registerFunction('reducer', function($memo, $num) { return $memo+$num; });

$result = $coffee->execute('sum = _.reduce([1, 2, 3], reducer, 0)', $context);

var_dump($result);
예제 #5
0
파일: math_cube.php 프로젝트: nrk/macchiato
<?php

require 'shared.php';

cache_coffeescript();

$coffee = new Macchiato\CoffeeScript(COFFEESCRIPT_PATH);

$result = $coffee->execute(
<<<EOC
square = (x) -> x * x
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x
math.cube(42)
EOC
);

var_dump($result);