osdc.tw - gutscript for php haters

Post on 11-May-2015

945 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

GutscriptA new language for PHP haters

@c9s

PHP

說到 PHP,你有什麼感覺?

PHP

哩來!哩來!來來來!來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來來

《獨家直擊》來來哥的⼀一天【胖⻁虎黨PHP】

先談 PHP

function foo() {累贅的函數表⽰示法

namespace Foo\Bar;

醜到歪七扭⼋八的 namespace

if (…) { } else { }foreach($list as $item) {

太多括號

array("key" => “value","foo" => "bar");

[“key" => “value”,"foo" => “bar”];

煩⼈人的 “=>”

array array_map ( callable $callback , array $array1 [, array $... ] )

array array_filter ( array $array = array() [, callable $callback = Function() ] )

不⼀一致的函數原形 / 不直覺的呼叫⽅方式

難道沒有優點嗎?

Dynamic

Easy To Deploy

Plenty Open Source Library

Easy To Learn & Development

就算是升天的 Haskeller 也是可以被抓回來寫 PHP

程式語⾔言設計的嘗試

⼀一直找不到理由

CoffeeScript by Jeremy Ashkenas

Facebook HipHop / HHVM

HHVM 改善了執⾏行效能

HHVM Benchmark & Compatibility

http://www.hhvm.com/blog/2813/we-are-the-98-5-and-the-16

Requests per second, middle response The higher the better

http://blog.liip.ch/archive/2013/10/29/hhvm-and-symfony2.html

PHP VM 改善了,但語⾔言還是⼀一樣爛

A new language beyond PHP

For Fun

拋棄 PHP 的向後相容

重新設計

The Language

• Concise

• Easy to learn, Easy to write, Readability

• Brings benefits from Ruby and Perl

The Generated Code

• Can reuse existing PHP libraries

• Compatible with PHP 5.4

• Generate PHPDoc format comments automatically.

PHP

Gutscript

HHVM Zend VM

HipHop Compiler

C++

Compile to PHP and run on HHVM or ZendVM

OptimizationPHP 5.4

PHP 5.3

Javascript

PHP C Extension

Gutscript

Gutscript (Future)

Synopsis

class Person # Print the name say :: (name) -> "Hello #{name}, Good morning"! getPhone :: -> "12345678"! setName :: (string name) -> @name = name!if str =~ /[a-z]/ say "matched!"

<?phpclass Person { /** * Print the name * * @param mixed $name */ function say($name) { return "Hello " . $name . ', Good morning'; }! function getPhone() { return "12345678"; }! /** * @param string $name */ function setName($name) { $this->name = $name; }}if ( preg_match('[a-z]',$str) ) { echo "matched!";}

Expression

a = 3 + 5b = 3.1415c = "Hello" ++ "World"

a = 3 + 5b = 3.1415c = "Hello" ++ "World"

<?php$a = 3 + 5;$b = 3.1415;$c = "Hello" . "World"

Control Flow

say "Hello" if a > 10

say "Hello" if a > 10

<?phpif ( $a > 10 ) { echo “Hello";}

say i for i in [ 1..10 ]

say i for i in [ 1..10 ]

<?phpfor ( $i = 1; $i < 10 ; $i++ ) { echo $i;}

Function

area :: (x,y) -> x * y

area :: (x,y) -> x * y

<?phpfunction area($x, $y) { return $x * $y;}

Class

class Person getName :: () -> "John" getPhone :: () -> "12345678"

class Person getName :: () -> "John" getPhone :: () -> "12345678"

<?phpclass Person { function getName() { return "John"; } function getPhone() { return "12345678"; }}

Class Inheritance

Bring ideas from Perl 6

class Person is Object does ArrayIterator getName :: () -> "name"

class Person is Object does ArrayIterator getName :: () -> "name"

<?phpclass Person extends Object implements ArrayIterator{ function getName() { return "name"; }}

Regular Expression

say $1 if foo =~ /([a-z])+/

<?phpif ( preg_match('/([a-z])+/', $foo, $regs) ) { echo $regs[1];}

Map & Grep

phones = map (x) -> { x.phone } contacts

<?php$phones = array_map(function($x) { return $x->phone;}, $contacts);

sort (a,b) -> { a <=> b } list

<?phpfunction __sort1($a, $b) { if ( $a == $b ) { return 0; } return ( $a < $b ) ? -1 : 1;}sort($list, “__sort1”);

Auto-generated PHPDoc

# method description# @param name contact’s namesetName :: (string name) -> @name = name

<?php/** * method description * @param string $name contact’s name */function setName($name) { $this->name = $name;}

Optimization

for Fun

Inline Expansion

Function calls are pretty slow in PHP

Refactoring produces more functions

function foo($a, $b) { return $a + $b;}$ret = foo(1,2) + foo(3,4);

$ret = (1+2) + (3+4);

Dead Code Elimination

function foo($a, $b) { return $a + $b;}function bar() { … }$ret = foo(1,2) + foo(3,4);

function foo($a, $b) { return $a + $b;}$ret = foo(1,2) + foo(3,4);

Useful when using only small part functions of

large libraries.

Constant Folding

$ret = (1+2) + (3+4);

$ret = 10;

Code Minifying

compress all PHP source files => phar

Implementation

Implemented in Go

pre-compiled and no dependency

pre-compiled

static linking

No dependency

Concurrency Support

• hand-written lexer in Go

• pretty simple go yacc - LALR parser derived from Inferno's utils/iyacc/yacc.c

Parsing with concurrency

• Using Go channel to send tokens from Tokenizer

• Parser reads tokens from Go channel asynchronously.

Tokenizer Parser

Async token through channel

ParserTokenizer ASTFile

File

File

File

File

Go Channel

ParserTokenizer AST

Worker (Go Routine)

Worker (Go Routine)

.......

.......

Result

Setup

# fork this project…

# fork this project…$ git clone git@github.com:you/gutscript.git

# fork this project…$ git clone git@github.com:you/gutscript.git$ cd gutscript

# fork this project…$ git clone git@github.com:you/gutscript.git$ cd gutscript$ source goenv

# fork this project…$ git clone git@github.com:you/gutscript.git$ cd gutscript$ source goenv$ make

Join Us!https://github.com/c9s/gutscript

top related