2015-08-05 15:23:15 -07:00
|
|
|
# zig lang
|
2015-08-05 17:44:05 -07:00
|
|
|
|
2015-11-01 21:21:33 -08:00
|
|
|
An experiment in writing a low-level programming language with the intent to
|
|
|
|
replace C. Zig intends to be a small language, yet powerful enough to write
|
|
|
|
readable, safe, optimal, and concise code to solve any computing problem.
|
2015-08-05 17:44:05 -07:00
|
|
|
|
2015-11-01 21:21:33 -08:00
|
|
|
## Goals
|
|
|
|
|
|
|
|
* Ability to run arbitrary code at compile time and generate code.
|
|
|
|
* Completely compatible with C libraries with no wrapper necessary.
|
|
|
|
* Creating a C library should be a primary use case. Should be easy to export
|
|
|
|
an auto-generated .h file.
|
|
|
|
* Generics such as containers.
|
2015-11-03 21:31:27 -08:00
|
|
|
* Do not depend on libc unless explicitly imported.
|
2015-11-01 21:21:33 -08:00
|
|
|
* First class error code support.
|
|
|
|
* Include documentation generator.
|
|
|
|
* Eliminate the need for make, cmake, etc.
|
|
|
|
* Friendly toward package maintainers.
|
|
|
|
* Eliminate the need for C headers (when using zig internally).
|
|
|
|
* Ability to declare dependencies as Git URLS with commit locking (can
|
|
|
|
provide a tag or sha1).
|
2015-11-04 16:15:46 -08:00
|
|
|
* Tagged union enum type.
|
2015-11-01 21:21:33 -08:00
|
|
|
* Opinionated when it makes life easier.
|
|
|
|
- Tab character in source code is a compile error.
|
|
|
|
- Whitespace at the end of line is a compile error.
|
|
|
|
* Resilient to parsing errors to make IDE integration work well.
|
|
|
|
* Source code is UTF-8.
|
2015-11-03 21:31:27 -08:00
|
|
|
* Shebang line OK so language can be used for "scripting" as well.
|
2015-11-06 21:11:47 -08:00
|
|
|
* Ability to mark functions as test and automatically run them in test mode.
|
2015-11-27 14:46:06 -08:00
|
|
|
This mode should automatically provide test coverage.
|
2015-11-06 21:11:47 -08:00
|
|
|
* Memory zeroed by default, unless you initialize with "uninitialized".
|
2015-11-01 21:21:33 -08:00
|
|
|
|
2015-11-27 23:40:54 -08:00
|
|
|
### Building
|
|
|
|
|
|
|
|
```
|
|
|
|
mkdir build
|
|
|
|
cd build
|
|
|
|
cmake ..
|
|
|
|
make
|
|
|
|
./run_tests
|
|
|
|
```
|
|
|
|
|
2015-11-01 21:21:33 -08:00
|
|
|
## Roadmap
|
|
|
|
|
2015-11-27 20:24:11 -08:00
|
|
|
* Math expression
|
|
|
|
* Export .so library
|
2015-11-24 19:37:53 -08:00
|
|
|
* Multiple files
|
2015-11-24 22:44:41 -08:00
|
|
|
* inline assembly and syscalls
|
|
|
|
* running code at compile time
|
2015-11-27 17:55:06 -08:00
|
|
|
* print! macro that takes var args
|
|
|
|
* panic! macro that prints a stack trace to stderr in debug mode and calls
|
|
|
|
abort() in release mode
|
|
|
|
* unreachable codegen to panic("unreachable") in debug mode, and nothing in
|
|
|
|
release mode
|
2015-11-24 19:37:53 -08:00
|
|
|
* implement a simple game using SDL2
|
2015-11-01 21:21:33 -08:00
|
|
|
* How should the Widget use case be solved? In Genesis I'm using C++ and inheritance.
|
2015-11-03 21:31:27 -08:00
|
|
|
|
2015-11-04 16:15:46 -08:00
|
|
|
### Primitive Numeric Types:
|
2015-11-03 21:31:27 -08:00
|
|
|
|
2015-11-04 16:15:46 -08:00
|
|
|
zig | C equivalent | Description
|
|
|
|
-------|--------------|-------------------------------
|
2015-11-27 16:07:23 -08:00
|
|
|
bool | bool | unsigned 1-bit integer
|
2015-11-04 16:15:46 -08:00
|
|
|
i8 | int8_t | signed 8-bit integer
|
|
|
|
u8 | uint8_t | unsigned 8-bit integer
|
|
|
|
i16 | int16_t | signed 16-bit integer
|
|
|
|
u16 | uint16_t | unsigned 16-bit integer
|
|
|
|
i32 | int32_t | signed 32-bit integer
|
|
|
|
u32 | uint32_t | unsigned 32-bit integer
|
|
|
|
i64 | int64_t | signed 64-bit integer
|
|
|
|
u64 | uint64_t | unsigned 64-bit integer
|
|
|
|
f32 | float | 32-bit IEE754 floating point
|
|
|
|
f64 | double | 64-bit IEE754 floating point
|
|
|
|
f128 | long double | 128-bit IEE754 floating point
|
2015-11-27 16:07:23 -08:00
|
|
|
isize | intptr_t | signed pointer sized integer
|
|
|
|
usize | uintptr_t | unsigned pointer sized integer
|
2015-11-23 20:30:12 -08:00
|
|
|
|
|
|
|
### Grammar
|
|
|
|
|
|
|
|
```
|
2015-11-27 20:24:11 -08:00
|
|
|
Root : RootExportDecl many(TopLevelDecl) token(EOF)
|
|
|
|
|
|
|
|
RootExportDecl : token(Export) token(Symbol) token(String) token(Semicolon)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-24 01:43:45 -08:00
|
|
|
TopLevelDecl : FnDef | ExternBlock
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-26 00:29:52 -08:00
|
|
|
ExternBlock : many(Directive) token(Extern) token(LBrace) many(FnDecl) token(RBrace)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-27 14:46:06 -08:00
|
|
|
FnProto : many(Directive) option(FnVisibleMod) token(Fn) token(Symbol) ParamDeclList option(token(Arrow) Type)
|
|
|
|
|
|
|
|
FnVisibleMod : token(Pub) | token(Export)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-24 01:43:45 -08:00
|
|
|
FnDecl : FnProto token(Semicolon)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-27 14:46:06 -08:00
|
|
|
FnDef : FnProto Block
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-24 01:43:45 -08:00
|
|
|
ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-24 01:43:45 -08:00
|
|
|
ParamDecl : token(Symbol) token(Colon) Type
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-24 12:37:14 -08:00
|
|
|
Type : token(Symbol) | PointerType | token(Unreachable)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-25 14:44:05 -08:00
|
|
|
PointerType : token(Star) token(Const) Type | token(Star) token(Mut) Type
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-24 01:43:45 -08:00
|
|
|
Block : token(LBrace) many(Statement) token(RBrace)
|
2015-11-23 20:30:12 -08:00
|
|
|
|
2015-11-27 23:40:54 -08:00
|
|
|
Statement : ExpressionStatement
|
2015-11-24 01:43:45 -08:00
|
|
|
|
|
|
|
ExpressionStatement : Expression token(Semicolon)
|
|
|
|
|
2015-11-27 23:40:54 -08:00
|
|
|
Expression : BoolOrExpression | ReturnExpression
|
|
|
|
|
|
|
|
BoolOrExpression : BoolAndExpression token(BoolOr) BoolAndExpression | BoolAndExpression
|
|
|
|
|
|
|
|
ReturnExpression : token(Return) option(Expression)
|
|
|
|
|
|
|
|
BoolAndExpression : ComparisonExpression token(BoolAnd) ComparisonExpression | ComparisonExpression
|
|
|
|
|
|
|
|
ComparisonExpression : BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression
|
|
|
|
|
|
|
|
ComparisonOperator : token(BoolEq) | token(BoolNotEq) | token(BoolLessThan) | token(BoolGreaterThan) | token(BoolLessEqual) | token(BoolGreaterEqual)
|
|
|
|
|
|
|
|
BinaryOrExpression : BinaryXorExpression token(BinOr) BinaryXorExpression | BinaryXorExpression
|
2015-11-24 01:43:45 -08:00
|
|
|
|
2015-11-27 23:40:54 -08:00
|
|
|
BinaryXorExpression : BinaryAndExpression token(BinXor) BinaryAndExpression | BinaryAndExpression
|
|
|
|
|
|
|
|
BinaryAndExpression : BitShiftExpression token(BinAnd) BitShiftExpression | BitShiftExpression
|
|
|
|
|
|
|
|
BitShiftExpression : AdditionExpression BitShiftOperator AdditionExpression | AdditionExpression
|
|
|
|
|
|
|
|
BitShiftOperator : token(BitShiftLeft | token(BitShiftRight)
|
|
|
|
|
|
|
|
AdditionExpression : MultiplyExpression AdditionOperator MultiplyExpression | MultiplyExpression
|
|
|
|
|
|
|
|
AdditionOperator : token(Plus) | token(Minus)
|
|
|
|
|
|
|
|
MultiplyExpression : CastExpression MultiplyOperator CastExpression | CastExpression
|
|
|
|
|
|
|
|
MultiplyOperator : token(Star) | token(Slash) | token(Percent)
|
|
|
|
|
|
|
|
CastExpression : PrimaryExpression token(as) Type | PrimaryExpression
|
|
|
|
|
|
|
|
PrimaryExpression : token(Number) | token(String) | token(Unreachable) | FnCall | GroupedExpression | Block
|
|
|
|
|
|
|
|
GroupedExpression : token(LParen) Expression token(RParen)
|
2015-11-24 01:43:45 -08:00
|
|
|
|
|
|
|
FnCall : token(Symbol) token(LParen) list(Expression, token(Comma)) token(RParen)
|
2015-11-24 22:44:41 -08:00
|
|
|
|
|
|
|
Directive : token(NumberSign) token(Symbol) token(LParen) token(String) token(RParen)
|
2015-11-23 20:30:12 -08:00
|
|
|
```
|
2015-11-26 00:29:52 -08:00
|
|
|
|
2015-11-27 23:40:54 -08:00
|
|
|
### Binary Operator Precedence
|
2015-11-26 00:29:52 -08:00
|
|
|
|
|
|
|
```
|
2015-11-27 23:40:54 -08:00
|
|
|
as
|
|
|
|
* / %
|
|
|
|
+ -
|
|
|
|
<< >>
|
|
|
|
&
|
|
|
|
^
|
|
|
|
|
|
|
|
|
== != < > <= >=
|
|
|
|
&&
|
|
|
|
||
|
|
|
|
=
|
2015-11-26 00:29:52 -08:00
|
|
|
```
|