bell
所属分类:编程语言基础
开发工具:GO
文件大小:0KB
下载次数:0
上传日期:2020-11-18 17:41:00
上 传 者:
sh-1993
说明: 一种编程语言
(A programming language)
文件列表:
LICENSE (11345, 2020-11-18)
Makefile (3115, 2020-11-18)
ast/ (0, 2020-11-18)
ast/ast.go (8557, 2020-11-18)
cmd/ (0, 2020-11-18)
cmd/bell/ (0, 2020-11-18)
cmd/bell/main.go (871, 2020-11-18)
cmd/repl/ (0, 2020-11-18)
cmd/repl/main.go (2779, 2020-11-18)
evaluator/ (0, 2020-11-18)
evaluator/builtins.go (2402, 2020-11-18)
evaluator/evaluator.go (13754, 2020-11-18)
go.mod (214, 2020-11-18)
go.sum (8335, 2020-11-18)
lexer/ (0, 2020-11-18)
lexer/lexer.go (3588, 2020-11-18)
lexer/lexer_test.go (1452, 2020-11-18)
object/ (0, 2020-11-18)
object/environment.go (565, 2020-11-18)
object/object.go (2349, 2020-11-18)
parser/ (0, 2020-11-18)
parser/parser.go (13307, 2020-11-18)
parser/parser_test.go (6274, 2020-11-18)
stdlib/ (0, 2020-11-18)
stdlib/collection.bell (218, 2020-11-18)
stdlib/string.bell (279, 2020-11-18)
test/ (0, 2020-11-18)
test/e2e/ (0, 2020-11-18)
test/e2e/e2e_test.go (1971, 2020-11-18)
test/e2e/features/ (0, 2020-11-18)
test/e2e/features/divide_operation.feature (1207, 2020-11-18)
test/e2e/features/errors_for_operations.feature (2089, 2020-11-18)
test/e2e/features/if_expression.feature (2270, 2020-11-18)
test/e2e/features/let_expression.feature (3574, 2020-11-18)
test/e2e/features/list.feature (2166, 2020-11-18)
test/e2e/features/logical_and_relational_operation.feature (2540, 2020-11-18)
test/e2e/features/modulo_operator.feature (384, 2020-11-18)
test/e2e/features/multiply_operation.feature (1223, 2020-11-18)
... ...
# Bell
A programming language based on Lisp
## Basic operators
`(operator expression1 expression2 expressionn)`
### Operators
Example of area calculation
```
(let area [length width] (* length width))
(writeln (area 3 4))
```
Reverse word:
```
(let reverse [word]
(if (> (size word) 0)
(+ (reverse (tail word)) (head word))))
(writeln (reverse "hello"))
```
Fold left implementation:
```
(let foldL [init lst func]
(if (not= nil lst)
(func (head lst) (foldL init (tail lst) func))
init))
(writeln "Sum:" (foldL 0 (list 1 2 3 4 5) (let _ [a b] (+ a b))))
(writeln "Product:" (foldL 1 (list 1 2 3 4 5) (let _ [a b] (* a b))))
```
Map implementation with invocation which takes a function
that multiplies list members with 2:
```
(let map [lst func]
(if (not= nil lst)
(list ((func (head lst)) (map (tail lst) func)))))
(let lst (list 1 2 3 5 6))
(writeln (map lst (let _ [x] (* x 2))))
```
#### Arithmetic operators
| Operator | Description | Example |
| :------: | ------------------------------------------------------------- | ---------------------------- |
| `+` | Adds expressions | (+ 1 2 3) will evaluate to 6 |
| `-` | Subtracts expressions | (- 3 1) will evaluate to 2 |
| `*` | Multiplies expressions | (\* 3 4) will evaluate to 12 |
| `/` | Divides expressions | (/ 6 2) will evaluate to 3 |
| `%` | Modulo division (produces a remainder of an integer division) | (% 6 2) will evaluate to 0 |
| `^` | Value raised to the power of the second argument | (^ 2 3) will evaluate to 8 |
#### Relational operators
| Operator | Description | Example |
| :------: | --------------------------------------------------------------------------------------- | ---------------------------------- |
| `=` | Checks equality between expressions (all values have to be equal to evaluate to true) | (= 2 2 3) will evaluate to false |
| `not=` | Checks the difference between expressions (one value has to differ to evaluate to true) | (not= 2 2 3) will evaluate to true |
| `<` | Checks whether the left value is less than the right value | (< 2 3) will evaluate to true |
| `<=` | Checks whether the left value is less than or equal to the right value | (<= 3 3) will evaluate to true |
| `>` | Checks whether the left value is greater than the right value | (> 2 3) will evaluate to false |
| `>=` | Checks whether the left value is greater than or equal to the right value | (>= 3 3) will evaluate to true |
#### Logical operators
| Operator | Description | Example |
| :------: | ------------------------------------------------------------------------------------------------ | -------------------------------------------- |
| `and` | Logical "and" operator | (and true true false) will evaluate to false |
| `or` | Logical "or" operator | (or true false false) will evaluate to true |
| `not` | Logical "or" operator. It can have only a single expression. Otherwise, it will return an error. | (not true) will evaluate to false |
#### Types and type rules
Bell supports following types:
- 64-bit signed integers
- booleans - `true` or `false`
- nil - a null value
- strings
- lists - a sequence which can contain all previous values
Arithmetic operations can only accept numbers. Meaning, following expression:
`(+ 3 true)` will give an error `Operation (+ 3 true) cannot be performed for types: INTEGER and BOOLEAN`.
On the other hand, logical operations can only accept booleans. Expression: `(and true 1)` will give an error
`Operation (and true 1) cannot be performed for types: BOOLEAN and INTEGER`
Relational operations produce boolean values and can be mixed with logical operations.
Expression `(and true (> 4 5))` will evaluate to `false`.
#### Value assignment
`(let x 3)`
`let` is a keyword
`x` is a variable identifier
`3` is an assigned value
More complex expressions can be also assigned
`(let x (* 3 6))`
#### Conditional expressions
If expression contains three parts.
- Condition - any expression which evaluates to BOOLEAN type.
Expressions which evaluate to any other type will result in an error.
- Then expression - when the condition is true (4 in the following example)
- Optional, else expression - when the condition is false (3 in the following example)
`(if (> 4 3) 4 3)`
#### Function assignment
Similarly to value assignment, `let` keyword is being used for a function assignment.
Function contains a name, list of parameters (it can be empty) and a body.
`(let double [number] (* number 2))`
To call the function:
`(double 6)` which will produce `12`.
Higher order functions are also supported. Which means that we can pass a function
as an argument:
`(let map [arg func] (func arg))`
Call the function with a function as the argument
`(map 10 (let _ [a] (* a 2)))`
Gives: `20`.
##### Builtin functions
`head` - returns a first element in a list or a string.
Expression `(head (list 1 2 3))` will produce `1`.
Expression `(head "hello")` will give `h`.
`tail` - returns all elements in a list or a string except of the first element.
Expression `(tail (list 1 2 3))` will produce `2 3`.
Expression `(tail "hello")` will produce `ello`.
`size` - returns a size of a list or a length of a string.
Expression `(size (list 1 2 3 4))` will produce `4`.
Expression `(size "hello")` will produce `5`.
#### Open files
Use `open` to import variables and functions from another file relative to bell executable file.
```
(open "stdlib/collection")
```
近期下载者:
相关文件:
收藏者: