Crates.io | erg |
lib.rs | erg |
version | |
source | src |
created_at | 2022-08-11 02:29:19.337189 |
updated_at | 2024-12-12 16:49:44.509706 |
description | The Erg programming language |
homepage | https://erg-lang.org/ |
repository | https://github.com/erg-lang/erg |
max_upload_size | |
id | 643052 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This is the main source code repository for Erg. This contains the compiler and documentation.
Some features are not yet implemented. Please see TODO.md for implementation status.
Robustness
Erg has a smart & powerful type system. For example, Erg can do null checking (Option type), division by zero, and out-of-range addresses in arrays at compile time.
rand = pyimport "random"
l = [1, 2, 3]
assert l in [Nat; 3] # type checking
assert l in [1..3; 3] # more detailed
l2 = l.push(rand.choice! 0..10)
assert l2 in [0..10; 4]
assert l2 + [3, 5, 7] in [0..10; 7]
# This causes an IndexError, Erg can detect it at compile time
l2[10] # IndexError: `l2` has 7 elements but was accessed the 11th element
2.times! do!:
print! "hello, ", end := ""
# => hello, hello,
-2.times! do!:
print! "hello, ", end := ""
# TypeError: `.times!` is a method of `Nat` (0 or more Int), not `Int`
{Meter; Sec; meter; kg; sec} = import "unit"
velocity x: Meter, t: Sec = x / t
v = velocity 3kg, 2sec # TypeError: the type of `x` was mismatched: expect `Meter`, found `Kg`
v = velocity 3meter, 2sec # v == 1.5 m/s
Simplicity
Erg consists of a very simple syntax, which can significantly reduce the amount of code compared to other languages. However, its functionality is not inferior to them.
Since the type inference system is powerful, you can code like a dynamically typed language.
fib 0 = 0
fib 1 = 1
fib n = fib(n - 1) + fib(n - 2)
assert fib(10) == 55
Even for and while expressions are just one of the subroutines, so this is possible.
loop! block! = while! do! True, block!
# equals to `while! do(True), do! print! "hello"`
loop! do!:
print! "hello"
Functional & Object-oriented
Erg is a pure object-oriented language. Everything is an object; types, functions, and operators are all objects. On the other hand, Erg is also a functional language. Erg requires some kinds of markers to be placed on code that causes side effects or changes internal state, which can localize the complexity of code. This will greatly improve the maintainability of your code.
# Functional style (immutable), same as `sorted(list)` in Python
immut_arr = [1, 3, 2]
assert immut_arr.sort() == [1, 2, 3]
# Object-oriented style (mutable)
mut_arr = ![1, 3, 2]
mut_arr.sort!()
assert mut_arr == [1, 2, 3]
i = !1
i.update! old -> old + 1
assert i == 2
# Functions cannot cause side effects
inc i: Int! =
i.update! old -> old + 1
# SyntaxError: cannot call a procedural method in a function
# hint: only methods of mutable types can change the state of objects
# Code that uses a lot of side effects is redundant, so you will naturally write pure code
Counter! = Inherit Int!
Counter!.
new i: Int = Counter! !i
inc! ref! self =
self.update! old -> old + 1
c = Counter!.new 1
c.inc!()
assert c == 2
Interoperability
Erg is internally compatible with Python and can import the Python API at zero cost.
# using built-in Python modules
math, time = pyimport "math", "time"
{sin; pi} = math
# using an external Python module
tqdm = pyimport "tqdm"
print! sin pi # 1.2246467991473532e-16
for! tqdm.tqdm(0..99), i =>
time.sleep! 0.01 * i
Readable Error Messages
Erg emphasizes the readability of error messages; Erg is a programmer-friendly language, unlike C++.
proc! x =
l = [1, 2, 3]
l.push!(x)
l
Error[#12]: File example.er, line 3, in <module>::proc!
2│ l = [1, 2, 3]
3│ l.push!(x)
^^^^^
AttributeError: List object has no attribute `.push!`
hint: to update the internal state of an object, make it mutable by using `!` operator
hint: `List` has `push`, see https://erg-lang.github.io/docs/prelude/List/##push for more information
hint: `List!` has `push!`, see https://erg-lang.github.io/docs/prelude/List!/##push! for more information
A Python3 (3.7~3.11) interpreter is required. If it is already installed on your machine, no setup is required.
cargo install erg
Building from source code requires the Rust toolchain.
git clone https://github.com/erg-lang/erg.git
cd erg
cargo build --release
If you've been installed Nix, the following command will be generate binary into result/bin/erg
under the project.
git clone https://github.com/erg-lang/erg.git
cd erg
nix-build
If you've been enabled Nix Flakes.
git clone https://github.com/erg-lang/erg.git
cd erg
nix build
By enabling the --features
flag, you can customize the installation and build.
--features {language}
--features japanese
--features simplified_chinese
--features traditional_chinese
And more languages will be added (we are looking for translators. Please join the Translation Project).
Other flags:
--features els
--features debug
--features full-repl
--features unicode
and --features pretty
--features full
Contributions are always welcome! To get started with contributions, please look CONTRIBUTING.md.
If you have any questions, please feel free to ask them on the Discord channel.
All files in the assets and doc folders are licensed with CC-BY-4.0. The rest of the files in this repository are licensed with Apache License 2.0 + MIT License.
For credits about third party crates, see THIRD_PARTY_CREDITS.md.