Conghao97's picture tcftrees's picture
ProgramGen release
d086025
|
Raw History Blame Contribute Delete
3.26 kB

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.


Quick start

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:

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

Contents of this guide

Document What it covers
cli.md Every command, flag and output mode of the dragon CLI
language.md Syntax and language features, with runnable snippets
standard-library.md Built-in functions and import-able modules
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

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.