├── arrays.hack ├── while.hack ├── if.hack ├── hello.hack ├── function.hack ├── class.hack ├── for.hack ├── .gitignore ├── switch.hack ├── attributes.hack ├── LICENSE └── README.md /arrays.hack: -------------------------------------------------------------------------------- 1 | $v = vec[2, 1, 2]; 2 | 3 | $k = keyset[2, 1]; 4 | 5 | $d = dict['a' => 1, 'b' => 3]; 6 | -------------------------------------------------------------------------------- /while.hack: -------------------------------------------------------------------------------- 1 | $i = 1; 2 | while ($i <= 10) { 3 | echo "$i\t".($i * $i)."\n"; // output a table of squares 4 | ++$i; 5 | } 6 | -------------------------------------------------------------------------------- /if.hack: -------------------------------------------------------------------------------- 1 | if ($x) { 2 | echo "x is true"; 3 | } 4 | if ($y) { 5 | echo "y is true"; 6 | } else { 7 | echo "y is not true"; 8 | } 9 | -------------------------------------------------------------------------------- /hello.hack: -------------------------------------------------------------------------------- 1 | use namespace HH\Lib\IO; 2 | 3 | <<__EntryPoint>> 4 | async function main(): Awaitable { 5 | await IO\request_output()->writeAllAsync("Hello World!\n"); 6 | } 7 | -------------------------------------------------------------------------------- /function.hack: -------------------------------------------------------------------------------- 1 | function sum_ints(int $val, int ...$vals): int { 2 | $result = $val; 3 | 4 | foreach ($vals as $v) { 5 | $result += $v; 6 | } 7 | return $result; 8 | } 9 | -------------------------------------------------------------------------------- /class.hack: -------------------------------------------------------------------------------- 1 | class Counter { 2 | private int $i = 0; 3 | 4 | public function increment(): void { 5 | $this->i += 1; 6 | } 7 | 8 | public function get(): int { 9 | return $this->i; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /for.hack: -------------------------------------------------------------------------------- 1 | for ($i = 1; $i <= 5; ++$i) { 2 | echo "$i\t".($i * $i)."\n"; // output a table of squares 3 | } 4 | 5 | $i = 1; 6 | for (; $i <= 5; ) { 7 | echo "$i\t".($i * $i)."\n"; // output a table of squares 8 | ++$i; 9 | } 10 | 11 | $i = 1; 12 | for (; ; ) { 13 | if ($i > 5) 14 | break; 15 | echo "$i\t".($i * $i)."\n"; // output a table of squares 16 | ++$i; 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /switch.hack: -------------------------------------------------------------------------------- 1 | enum Bank: int { 2 | DEPOSIT = 1; 3 | WITHDRAWAL = 2; 4 | TRANSFER = 3; 5 | } 6 | 7 | function processTransaction(Transaction $t): void { 8 | $trType = ...; // get the transaction type as an enum value 9 | switch ($trType) { 10 | 11 | case Bank::TRANSFER: 12 | ... 13 | break; 14 | case Bank::DEPOSIT: 15 | ... 16 | break; 17 | case Bank::WITHDRAWAL: 18 | ... 19 | break; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /attributes.hack: -------------------------------------------------------------------------------- 1 | <<__ConsistentConstruct>> 2 | class OtherClass { 3 | <<__Memoize, __Deprecated("Use FooClass methods instead")>> 4 | public function addOne(int $x): int { 5 | return $x + 1; 6 | } 7 | } 8 | 9 | class Contributors implements HH\ClassAttribute { 10 | public function __construct(private string $author, private ?keyset $maintainers = null) {} 11 | public function getAuthor(): string { 12 | return $this->author; 13 | } 14 | public function getMaintainers(): keyset { 15 | return $this->maintainers ?? keyset[$this->author]; 16 | } 17 | } 18 | 19 | <> 20 | class MyClass {} 21 | 22 | <> 23 | class YourClass {} 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Pourya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hack-Programming-Language 2 | 3 | 4 | ## 1. Installation 5 | 6 | The HHVM package includes everything you need to work with Hack, including the runtime and typechecker. 7 | 8 | See the HHVM installation page to find the package relevant for your platform. 9 | 10 | 11 | ## 2. Initialize A Project 12 | 13 | Create a directory with a .hhconfig file in it. This will be the root of your project. 14 | 15 | ``` 16 | $ mkdir my_project 17 | $ cd my_project 18 | $ touch .hhconfig 19 | ``` 20 | 21 | ## 3. Write Your First Hack Program 22 | Create a file called my_project/hello.hack with the following code: 23 | 24 | ``` 25 | use namespace HH\Lib\IO; 26 | 27 | <<__EntryPoint>> 28 | async function main(): Awaitable { 29 | await IO\request_output()->writeAllAsync("Hello World!\n"); 30 | } 31 | ``` 32 | 33 | ## 4. Run The Typechecker 34 | 35 | Normally you'll get type errors and hover information from within your IDE. You can also run the typechecker directly to confirm that you have no type errors in any files in your project. 36 | 37 | ``` 38 | $ cd my_project 39 | $ hh_client 40 | No errors! 41 | ``` 42 | 43 | 44 | ## 5. Run Your Program 45 | 46 | HHVM provides the Hack runtime. You can run your program as follows: 47 | 48 | ``` 49 | $ cd my_project 50 | $ hhvm hello.hack 51 | Hello World! 52 | ``` 53 | 54 | ## 6. Run A Website 55 | 56 | Normally you'll start HHVM as a webserver, and it will automatically pick up any changes to files you make. 57 | 58 | ``` 59 | $ cd my_project 60 | $ hhvm -m server -p 8080 61 | ``` 62 | 63 | You can now visit http://localhost:8080/hello.hack to see "Hello World!" in your browser. 64 | --------------------------------------------------------------------------------