Skip to content

Commit

Permalink
Add examples for hack lang (joewalnes#337)
Browse files Browse the repository at this point in the history
Thanks @azjezz and @usox
  • Loading branch information
azjezz authored and asergeyev committed Jan 28, 2019
1 parent 9017566 commit 5c11ca2
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/hack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
This examples directory shows some examples written in [Hack](https://hacklang.org).

### Requirements :

- [HHVM](https://github.com/facebook/hhvm) 3.30+
- [HSL ( Hack Standard Library )](https://github.com/hhvm/hsl) 3.30+
- [HSL Experimental](https://github.com/hhvm/hsl-experimental) 3.30+

You can also test the command files by running from the command line :

```
$ hhvm count.hh
1
2
3
4
5
6
7
8
9
10
```
26 changes: 26 additions & 0 deletions examples/hack/count.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/hhvm
<?hh // strict

use namespace HH\Lib\Str;
use function HH\Lib\Experimental\IO\request_output;
use function usleep;

// Simple example script that counts to 10 at ~2Hz, then stops.

<<__EntryPoint>>
async function count_to_ten(): Awaitable<noreturn> {
$output = request_output();
for ($count = 1; $count <= 10; $count++) {
await $output->writeAsync(
Str\format("%d\n",$count)
);

// usleep is builtin, it is not an async builtin - so it also must block the main request thread
usleep(500000);
}

// flush output
await $output->flushAsync();

exit(0);
}
59 changes: 59 additions & 0 deletions examples/hack/dump-env.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/hhvm
<?hh // strict

use namespace HH\Lib\Str;
use function HH\Lib\Experimental\IO\request_output;

<<__EntryPoint>>
async function dumpEnv(): Awaitable<noreturn> {
// Standard CGI(ish) environment variables, as defined in
// http://tools.ietf.org/html/rfc3875
$names = keyset[
'AUTH_TYPE',
'CONTENT_LENGTH',
'CONTENT_TYPE',
'GATEWAY_INTERFACE',
'PATH_INFO',
'PATH_TRANSLATED',
'QUERY_STRING',
'REMOTE_ADDR',
'REMOTE_HOST',
'REMOTE_IDENT',
'REMOTE_PORT',
'REMOTE_USER',
'REQUEST_METHOD',
'REQUEST_URI',
'SCRIPT_NAME',
'SERVER_NAME',
'SERVER_PORT',
'SERVER_PROTOCOL',
'SERVER_SOFTWARE',
'UNIQUE_ID',
'HTTPS'
];

/* HH_IGNORE_ERROR[2050] using global variable */
$server = dict($_SERVER);

$ouput = request_output();

foreach($names as $name) {
await $output->writeAsync(
Str\format("%s = %s\n", $name, $server[$name] ?? '<unset>')
);
}

// Additional HTTP headers
foreach($server as $k => $v) {
if ($k is string && Str\starts_with($k, 'HTTP_')) {
await $output->writeAsync(
Str\format("%s = %s\n", $k, $v as string)
);
}
}

// flush output
await $output->flushAsync();

exit(0);
}
22 changes: 22 additions & 0 deletions examples/hack/greeter.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/hhvm
<?hh // strict

use namespace HH\Lib\Str;
use namespace HH\Lib\Experimental\IO;

<<__EntryPoint>>
async function greeter(): Awaitable<noreturn> {
// For each line FOO received on STDIN, respond with "Hello FOO!".
$input = IO\request_input();
$output = IO\request_output();
while(!$input->isEndOfFile()) {
await $ouput->writeAsync(
Str\format("Hello %s!\n", await $input->readLineAsync())
);
}

// flush output
await $output->flushAsync();

exit(0);
}

0 comments on commit 5c11ca2

Please sign in to comment.