File size: 3,257 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Dragon — Usage Guide

**Dragon** is a general-purpose programming language with a simple, expressive
syntax. Programs are written in `.dgn` source files and run with the `dragon`
CLI, which can also compile a script into a standalone executable or a shared
library, start an interactive REPL, and open a terminal IDE.

Dragon supports imperative, object-oriented, functional and declarative styles
and runs on Windows, Linux and macOS.

* File extension: `.dgn`
* Executable name: `dragon` (`dragon.exe` on Windows)
* Website & full documentation: <https://www.dragon-lang.org/>

---

## Quick start

```bash
dragon hello.dgn              # run a script
dragon hello.dgn Alice 42     # run a script with arguments (visible as `argv`)
dragon --repl                 # interactive REPL
dragon --version              # print version info
```

Compile a script into a single distributable executable:

```bash
dragon -c input.dgn -o myapp
./myapp
```

---

## Contents of this guide

| Document | What it covers |
| --- | --- |
| [`cli.md`](cli.md) | Every command, flag and output mode of the `dragon` CLI |
| [`language.md`](language.md) | Syntax and language features, with runnable snippets |
| [`standard-library.md`](standard-library.md) | Built-in functions and `import`-able modules |
| [`assets/examples/`](assets/examples/) | Complete examples you can run directly |

### Example programs (`assets/examples/`)

| File | Covers | Run |
| --- | --- | --- |
| `basics.dgn` | Variables, printing, string interpolation, arithmetic, `format()`, conversions | `dragon basics.dgn` |
| `control_flow.dgn` | `if`/`elif`/`else`, `while`, `do ... while`, `for ... in`, `switch`/`case`, ternary | `dragon control_flow.dgn` |
| `functions_and_collections.dgn` | Default & rest parameters, lambdas, lists, maps, string methods | `dragon functions_and_collections.dgn` |
| `classes_and_oop.dgn` | Classes, `init` constructors, inheritance, `super`, method overriding | `dragon classes_and_oop.dgn` |
| `errors_and_modules.dgn` | `try`/`catch`/`finally`, `raise`, and the `math` and `json` modules | `dragon errors_and_modules.dgn` |

---

## A first program

```dragon
import "thread"
import "math"

class Animal {
    func init(name) {
        this.name = name
    }
    func speak() {
        showln(this.name, "speaks")
    }
}

func worker(id) {
    sleep(0.5)
    showln("Task", id, "done")
}

animal = new Animal("Dragon")
animal.speak()

for i in math.range(5) {
    thread.spawn(worker, [i])
}

showln("All tasks started")
```

Save this as `demo.dgn` and run it with `dragon demo.dgn`.

---

## Requirements & notes for use

* **Native compilation (`--native`)** requires a system C compiler: one of
  `gcc`, `clang`, or `cl` (Windows) must be on your `PATH`. Everything else in
  the CLI runs without one.
* **Script arguments** supplied after the file name are exposed to the program
  as `argv`.
* **Bundled executables** are self-contained: the script and everything it
  imports are packaged into the produced file, so the target machine does not
  need Dragon installed.
* **Terminal IDE** (`--ide`) needs a terminal; it is a keyboard-driven text UI.

## License

Dragon is licensed under the MIT License — see [`assets/LICENSE`](assets/LICENSE).