项目作者: oshima

项目描述 :
A toy language
高级语言: Go
项目地址: git://github.com/oshima/lang.git
创建时间: 2018-10-21T08:58:46Z
项目社区:https://github.com/oshima/lang

开源协议:

下载


lang

lang is a toy language compiled into assembly code targeting x86_64-linux.

Syntax

Literals

lang has six types of values: int, bool, string, range, array and function.\
Each type has the literal to represent its value.

  1. // int (signed 64-bit)
  2. 42
  3. -7
  4. // bool
  5. true
  6. false
  7. // string
  8. "foo"
  9. // range
  10. 0..100
  11. // array
  12. ["apple", "banana", "orange"] // [3]string
  13. // function
  14. (a: int, b: int) -> int { return a + b; } // (int, int) -> int

Operators

lang has the following operators:

  1. // arithmetic operators
  2. 5 + 2
  3. 5 - 2
  4. 5 * 2
  5. 5 / 2
  6. 5 % 2
  7. -(5)
  8. // comparison operators
  9. 5 == 2
  10. 5 != 2
  11. 5 < 2
  12. 5 <= 2
  13. 5 > 2
  14. 5 >= 2
  15. // logical operators
  16. true && false
  17. true || false
  18. !true
  19. // `in` operator
  20. 2 in 0..5 // => true
  21. 5 in 0..5 // => false
  22. 2 in [0, 1, 2, 3, 4] // => true
  23. 5 in [0, 1, 2, 3, 4] // => false

Operator priority is similar to other languages.

  1. 1 + 2 * 3 // => 7
  2. (1 + 2) * 3 // => 9

Variables

Using var statement, we can declare a variable that holds a value.

  1. var num: int = 3 + 5;

Variable type annotation can be omitted.\
In which case, it is inferred by the type of initial value.

  1. var name = "foo", age = 20; // name: string, age: int
  2. var primes = [2, 3, 5, 7, 11]; // primes: [5]int

A value of variable can be reassigned.

  1. var num = 0;
  2. num = 3;
  3. num += 7;
  4. printf("%d\n", num) // => 10

Functions

Using func statement, we can declare a named function.\
It is required to annotate the types of parameters and return value.

  1. func mul(a: int, b: int) -> int {
  2. return a * b;
  3. }
  4. printf("%d\n", mul(3, 5)); // => 15

Function literal generates an anonymous function.

  1. (name: string) -> {
  2. printf("Hello, %s\n", name);
  3. }("foo");
  4. // => Hello, foo

Anonymous function can be stored in a variable and called by its name.

  1. var fib = (n: int) -> int {
  2. if n < 2 {
  3. return n;
  4. }
  5. return fib(n - 2) + fib(n - 1); // recursive call
  6. }
  7. printf("%d\n", fib(10)); // => 55

Flow control

lang has if, while and for statements like other languages.

  1. if true {
  2. puts("foo");
  3. }
  4. // => foo
  5. var n = 30;
  6. if n in 0..10 {
  7. puts("small");
  8. } else if n in 10..20 {
  9. puts("medium");
  10. } else {
  11. puts("large");
  12. }
  13. // => large
  1. var n = 0;
  2. while n < 10 {
  3. if n % 2 == 0 {
  4. continue;
  5. }
  6. printf("%d ", n);
  7. n += 1
  8. }
  9. // => 1 3 5 7 9
  10. var n = 0;
  11. while true {
  12. printf("%d ", n);
  13. n += 1;
  14. if n >= 5 {
  15. break;
  16. }
  17. }
  18. // => 0 1 2 3 4
  1. for n in 0..5 {
  2. printf("%d ", n);
  3. }
  4. // => 0 1 2 3 4
  5. for s in ["a", "b", "c"] {
  6. printf("%s ", s);
  7. }
  8. // => a b c

References