Files
boring-lang/README.md

236 lines
6.9 KiB
Markdown
Raw Normal View History

2019-11-13 00:05:04 -07:00
# Boring Lang Proposal
2019-11-04 22:12:38 -07:00
2019-11-05 21:35:44 -07:00
**NOTE: This repo is a work in progress as I learn compiler writing, I may abandon this.**
2019-11-04 22:12:38 -07:00
The Boring Programming Language (Boring-Lang) is an attempt to create an easy, productive, general purpose programming language that makes as few interesting choices as possible while still being in line with modern concepts in programming languages.
2019-11-13 00:05:04 -07:00
The language (wish list):
2019-12-30 20:58:27 -07:00
* is compiled with a run-time (llvm for convenience + c/rust compatibility)
2019-11-04 22:12:38 -07:00
* is garbage collected
* uses async-await for all IO, with a built-in multi-core scheduler
2019-11-13 00:05:04 -07:00
* supports algebraic data types (Result type for errors, Maybe/Optional type for nullables)
2019-12-30 20:58:27 -07:00
* supports parametric polymorphism (generics)
2019-11-04 22:12:38 -07:00
* uses struct+traits, rather than classes or stuct+interfaces
* has a rich standard library (http server, actor model)
2019-11-13 00:05:04 -07:00
* is immutable by default
2019-11-04 22:12:38 -07:00
It's basically a middle-ground of Rust, Golang, Swift, Typescript, and Python.
2019-11-04 22:12:38 -07:00
## Http Server Example
```
import net.http as http
import logging as log
import json as json
struct ExampleResponse {
id: Int32
name: Str
email: Str
}
2019-11-13 00:05:04 -07:00
async func handle(req: http.Request, resp: mut http.Response) {
2019-11-04 22:12:38 -07:00
log.info("request: ", req.body)
let response_data = ExampleResponse{id: 4, name: "Steven", email: "swerbenjagermanjensen@example.com"}
2019-11-13 00:05:04 -07:00
await resp.set_status(200)
await resp.write(json.encode<ExampleResponse>(response_data))
2019-11-04 22:12:38 -07:00
}
2019-11-13 00:05:04 -07:00
async func main(args: Array<Str>) Int32 {
let router = http.Router("").add_route("/myroute", handle)
http_server = http.Server("localhost", 8080, router)
2019-11-13 00:05:04 -07:00
let err := await http_server.server_forever()
2019-11-04 22:12:38 -07:00
await log.info("error serving: ", err)
return 1
}
```
2019-11-13 00:05:04 -07:00
## Mutability
2019-12-30 20:58:27 -07:00
All variables are immutable by default, to make them mutable use the `mut` keyword. Once a variable becomes immutable it cannot become mutable again. If you need it to become mutable, either implement the `clone` trait, or simply create a new one with the same data.
2019-11-13 00:05:04 -07:00
```
2019-12-30 20:58:27 -07:00
let mut foo = Dict<String, Int32>() // constructors always return a mutable reference
foo.insert("eggs", 12)
foo.insert("bananas", 2)
foo.insert("grapes", 2)
2019-11-13 00:05:04 -07:00
let bar = foo // bar is not mutable
2019-12-30 20:58:27 -07:00
bar.insert("apples", 4) // fails with compiler error
2019-11-13 00:05:04 -07:00
2019-12-30 20:58:27 -07:00
let mut baz = bar.clone()
baz.insert("apples", 4) // fine
2019-11-13 00:05:04 -07:00
```
Methods on a struct must specify if they mutate the struct.
```
impl Dict<Key: Hashable, Value> {
func insert(self: mut Dict, key: Key, value: Value) {
// mutate self here
}
2019-12-30 20:58:27 -07:00
func get(self: Dict, key: Key) Optional<Value> {
2019-11-13 00:05:04 -07:00
// no need for `mut`
}
}
```
## Context
Context is an exceptionally useful feature in golang, but a common complaint is that:
1. Because it works as an arbitrary map, it can be used to pass arguments into a function that aren't explicitly stated.
2. It is used for both passing context parameters and cancellation, two fundamentally different tasks that have no reason to be in the same object.
The boring standard library solves this by using parametric polymorphism. Context is by default an empty object passed through the chain, and each function/set of context parameters is an additional trait condition applied at compile time.
```
2019-11-14 23:22:35 -07:00
type HTTPRequest<Ctx: Context> = async func(Ctx, http.Request, mut http.Response)
2019-12-30 20:58:27 -07:00
pub func tracing_middleware<Ctx: Tracing>(handler: HTTPRequest<Ctx>) HTTPRequest {
2019-11-13 00:05:04 -07:00
return async func(ctx: Ctx, req: http.Request, resp: mut http.Response) {
2019-12-30 20:58:27 -07:00
with tracing.Span(ctx, "request_duration") {
2019-11-13 00:05:04 -07:00
await handler(ctx, req, resp)
}
}
}
2019-12-30 20:58:27 -07:00
pub func auth_middleware<Ctx: Auth>(handler: HTTPRequest<Ctx>, scope: Str) HTTPRequest {
2019-11-13 00:05:04 -07:00
return async func(ctx: Ctx, req: http.Request, resp: mut http.Response) {
if ctx.has_scope(scope) {
await handler(ctx, req, resp)
}
await resp.set_status(403)
2019-12-30 20:58:27 -07:00
await resp.write("missing scope")
2019-11-13 00:05:04 -07:00
}
}
2019-12-30 20:58:27 -07:00
pub func cancel_middleware<Ctx: Cancel>(handler: HTTPRequest<Ctx>) HTTPRequest {
2019-11-13 00:05:04 -07:00
return async func(ctx: Ctx, req: http.Request, resp: mut http.Response) {
if !(await ctx.is_cancelled()) { // check cancel token
await handler(ctx, req, resp)
}
await resp.set_status(400)
2019-12-30 20:58:27 -07:00
await resp.write("cancelled")
2019-11-13 00:05:04 -07:00
}
}
```
for the above examples, you would pass a context type that implements all three traits.
## Monadic function modifiers
2019-12-30 20:58:27 -07:00
Boring uses function modifiers to implement functionality like `async/await` and `coroutines`. These function by [rewriting the code into a state machine](https://tmandry.gitlab.io/blog/posts/optimizing-await-1/) prior to compilation. The table below describes the modifiers currently available.
2019-11-13 00:05:04 -07:00
|Type|Change To Return Type|Introduces to Scope|
|---|---|---|
2019-12-30 20:58:27 -07:00
|`async`|`Promise<ReturnType>`|`await`|
|`coroutine`|`FirstReturnType,func(Next,Params)...`|`yield`|
|`error<ErrorType>`|`Result<ReturnType,ErrorType>`|`?`|
2019-11-13 00:05:04 -07:00
## Import System
2019-12-30 20:58:27 -07:00
Similar to python, folders/files represent the `.` seperated import path, but relative imports are *not* supported. Exported values must be marked with `pub`. All imports take the form:
2019-11-13 00:05:04 -07:00
```
import package.path as local_name
2019-12-30 20:58:27 -07:00
pub struct MyStruct {
2020-01-24 21:14:17 -07:00
id: Int32
}
```
## Basic Statements
### `if`
`if` is an expression in boring-lang, with the last expression in a block being the return value. If the block ends in a statement rather than an expression, Optional None is returned.
```
let a = if true {
4
} else {
2
}
// a == 4
let b = if false {
2
}
// b is an Optional<Int32> with value None.
```
Conditions do not require parenthesis and *must* evaluate to the Boolean type.
### Loops
Boring-lang supports `for` and `while` loops, with `for` having an `async` variant. `while` loops require an expression of Boolean type, while `for` loops require an expression that implements the `Iter` or `AIter` traits.
```
let mut i = 0
while i < 100 {
i = i + 1
// do something here
}
for i in range(100) {
// do something here
}
async for result in paginated_list {
// do something with result
2019-12-30 20:58:27 -07:00
}
2019-11-13 00:05:04 -07:00
```
2020-01-24 21:14:17 -07:00
`continue` and `break` work similar to other languages.
```
while true {
break // do nothing
}
for i in range(100) {
continue // do nothing
}
```
### `with`
`with` and `async with` blocks are similar to the python statement with the same name. But unlike the python version, `with` blocks are expressions. `with` blocks take in an expression that implements the `With` or `AWith` trait, and execute a block that *may* return a result (non-result returns are assumed success).
```
// commits on success, aborts on error.
// transation.aexit may just return an error as a pass-through after aborting,
// but it may also transform it into another error adding context.
return async with db.transation(ctx) as t {
await t.insert(ctx, record) // returns result type
}
```
### `return`
`return` statements exit a function early, returning the given value. They are purely optional as the last expression in a function will automatically return its value.
### `match`
`match` expressions provide pattern matching, similar to a `C` switch statement.
```
let number = 3
let result = match number {
1 => 'foo',
3 => 'bar',
_ => 'baz',
}
// result = 'bar'
```
TODO: yield, lambdas,