<?php require 'shared.php'; cache_coffeescript(); $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"
<?php require 'shared.php'; cache_coffeescript(); $coffee = new Macchiato\CoffeeScript(COFFEESCRIPT_PATH); $context = $coffee->createContext(); $result = null; $context->registerFunction('storeResult', function($object) use (&$result) { $result = $object; }); $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 =
<?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);
<?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);
<?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);