qianxi-lang
a lang interpreter written in go
Try
Download
git clone https://github.com/qianxi0410/qianxi-lang
cd qianxi-lang
with REPL
go run main.go
with File Interpreter
go run .\main.go filepath
Documetion
Summery
- C-like syntax
- variable bindings
- numbers and booleans
- a string data structure
- an array data structure
- a hash data structure
- arithmetic expressions
- built-in functions
- first-class and higher-order functions • closures
- logic operator
Keywords
let
、 fn
、 if
、 else
、 elif
、 return
、 true
、 false
Examples
let
let <identifier> = <value>;
let a = 1;
let b = 2.2;
let c = "hello world";
let d = true;
fn
let fibonacci = fn(x) {
if (x <= 1) {
x
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
};
if
let hello = if (x == 1) {
"hello world"
} elif (x == 2) {
"hello qianxi"
} else {
"just hello"
}
return
let identity = fn(x) {
return x;
};
identity("qianxi");
operators
1 + 2 + (3.3 * 4) - (10 / 5);
!true;
!false;
+10;
-5;
"Hello" + " " + "World";
1 && 2;
1 || 2;
1 << 2;
2 >> 1;
1 & 2;
1 | 2;
1 ^ 2;
~1;
Built-in Structs
array
let arr = [<expression>, <expression>, ...];
let arr = [1, 2, 3 + 3, fn(x) { x }, add(2, 2), true];
arr[0];
arr[1];
hash
let h = { <expression>: <expression>, <expression>: <expression>, ... };
let hash = {
"name": "qianxi",
"age": 21,
true: "a boolean",
99: "an integer"
};
hash["name"];
hash["a" + "ge"];
hash[true];
hash[99];
hash[100 - 1];
Built-in Functions
qianxi lang
provide you some built-in functions
len
len(<array | string>): number;
len([1, 2, 3]); // 3
len("hello"); // 5
first
first(<array>): any;
first(["hello", 2, 3]); // "hello"
first([1, 2, 3]); // 1
last
last(<array>): any;
last(["hello", 2, 3]); // 3
last([1, 2, "qianxi"]); // "qianxi"
rest
rest(<array>): array;
rest([]) // NULL
rest([1]) // []
rest([1, 2, 3]) // [2, 3]
push
push(<array>): array
push([], 1) // [1]
puts
puts(<any>): NULL
puts("hello world") // print : hello world