
php-snow
2012-04-17, news: The snowball is rolling again. The code and documentation has moved to https://github.com/runekaagaard/snowscript.
Snow is a little language that compiles to PHP. It has its own syntax inspired by Python, Ruby, Go and Scala that strives to be DRY, clean and easy to read as well as write. For those familiar with Coffescript you could say that Snow is to PHP what Coffeescript is to Javascript.
Snow is called "Snow" because it should look "pure as snow", because is has significant WHITEspace and because the winter of 2010 in Copenhagen was extremely cold and snowy.
Snow is still in its implementation phase. Its main features are:
* Is infinitely more dry than PHP.
* Kills the dreaded $
, $this
and self
.
* Has a lot less characters (about 1:1.7) than PHP and much fewer lines (about 1:2).
* Has chainable receiver functions.
* Can contain advanced expressions in class attribute definitions.
* Removes boring repetitive code patterns.
* Does not try to have a Java like syntax.
* Has significant whitespace.
* Can coexist naturally with existing non-Snow PHP code.
* Generates idiomatic, high quality looking PHP.
* Can compile existing PHP to snow.
* Is very well tested with PHPT.
* Makes PHP fun again.
Example
Words, words, words, now show me the code! Well certainly. Here is a short example (or at least the Snow version is short:).
Snow PHP@str title=null
# The title shown above the table.
class SumTable
# Renders a html table of numbers.
A sum column is added to each row and the total sum
of all cells is shown on top.
pri
title_default = t('Untitled')
title = title ?! .title_default
rows = []
sum = 0
@arr row
# List of integers.
[!_->empty: "$row cannot be empty."]
fn add_row(row)
# Append a table row.
n = row->count
for col in row
row[n] += 0 | col | null
.sum += row[n]
.rows[] = row
fn render
# Returns table as html.
for row in .rows
html %= "<table>" | .render_row(row) | "</table>"
<- "<h2>{.title}: {.sum}</h2>" % html
pri fn render_row(row)
for col in row
html %= "<tr>" | "<td>{col}</td>" | "</tr>"
<- html
/**
* Renders a html table of numbers.
*
* A sum column is added to each row and the total sum of
* all cells is shown on top.
*/
class SumTable {
private $title_default;
private $title;
private $rows = array();
private $sum = 0;
/**
* Constructor.
*
* @param string $title
* The title shown above the table.
*/
public function __construct($title=null) {
$this->title_default = t('Untitled');
$this->title = !empty($title)
? $title
: $this->title_default;
}
/**
* Append a table row.
*
* @param array $row
* List of integers.
*/
public function add_row($row) {
if (empty($row)) {
throw new InvalidArgumentException(
'$row cannot be empty');
}
$n = count($row);
$row[$n] = 0;
foreach ($row as $col) {
$row[$n] += $col;
}
$this->sum += $row[$n];
$this->rows[] = $row;
}
/**
* Returns table as html.
*/
public function render() {
$html = '<table>';
foreach ($this->rows as $row) {
$html .= $this->render_row($row);
}
$html .= '</table>';
return "<h2>$this->title: $this->sum</h2>" . $html;
}
private function render_row($row) {
$html = '<tr>';
foreach ($row as $col) {
$html .= "<td>$col</td>";
}
$html .= '</tr>';
return $html;
}
}
Continue reading
There is a lot more cool stuff going on so check out the documentation.
Current status
The specification and accompanied documentation is pretty completed and there is a somewhat working lexer built in PLY. See below example. The yellow lines are Snow, the other are the lexed tokens.
It's soon time to start working on the parser and then the PHP generator.