mirror of https://github.com/colgm/colgm.git
563617b6d3 | ||
---|---|---|
.. | ||
ast | ||
mir | ||
package | ||
sema | ||
sir | ||
CMakeLists.txt | ||
README.md | ||
colgm.h | ||
lexer.cpp | ||
lexer.h | ||
main.cpp | ||
misc.cpp | ||
parse.cpp | ||
parse.h | ||
report.cpp | ||
report.h |
README.md
Colgm Bootstrap Compiler
This directory stores the source file of colgm bootstrap compiler.
Build
In the top level directory, use these commands:
mkdir build && cd build && cmake ../bootstrap -DCMAKE_BUILD_TYPE=Release && make -j
Usage
Use this command to get usage:
./build/colgm -h
Literal
func main() -> i32 {
var a = 1; // i64
var b = 2.0; // f64
var c = "hello world!"; // i8*
var d = 'd'; // i8
var e = [i8; 128]; // const i8*
var f = true; // bool
return 0 => i32;
}
Operators
Arithmetic Operator
Colgm allows these arithmetic operators:
a + b;
a - b;
a * b;
a / b;
a % b;
Bitwise Operator
Colgm now supports these bitwise operators:
a | b;
a ^ b;
a & b;
Logical Operator
Support basic logical operators: ==
!=
<
<=
>
>=
.
Logical operators for and
/or
all have two types:
1==1 and 2==2 && 3==3;
1==1 or 2==2 || 3==3;
Definition
Colgm allows two kinds of definition.
var variable_name: type = expression; # with type
var variable_name = expression; # without type
Assignment
Colgm allows these assignment operators:
a += b;
a -= b;
a *= b;
a /= b;
a %= b;
a |= b;
a ^= b;
a &= b;
Control Flow
While
while loop is directly translated to mir_loop
in colgm mir.
while (1 == 1) {
...
continue;
break;
}
Condition/Branch
if (1 == 1) {
...
} elsif (1 == 1) {
...
} else if (1 == 1) {
...
} else {
...
}
Match
match (variable) {
EnumType::a => ...
EnumType::b => ...
}
For
for (var i = 0; i < 10; i += 1) {}
=>
var i = 0;
while (i < 10) {
i += 1;
}
Foreach/ForIndex [WIP]
foreach (var i; container) {}
=>
var tmp_i = container.begin();
while (tmp_i != container.end()) {
var i = tmp_i.value();
tmp_i = tmp_i.next();
}
forindex (var i; container) {}
=>
var tmp_i = 0;
while (tmp_i < container.size()) {
var i = tmp_i;
tmp_i += 1;
}
Type Conversion
A bit like rust, using =>
instead of as
:
func main() -> i32 {
return 0 => i32;
}
Struct Definition
struct StructName {
field1: i64,
field2: i64
}
struct StructWithGeneric<T> {
__size: i64,
__data: T*
}
Struct Initializer
func test() {
var s1 = StructName {};
var s2 = StructName { field1: 1 };
var s3 = StructName { field1: 1, field2: 2 };
}
Implementation
impl StructName {
func method(self) -> type {
...
return ...;
}
func static_method() -> type {
...
return ...;
}
}
impl StructWithGeneric<T> {
...
}
Enum Definition
enum EnumExample {
kind_a,
kind_b,
kind_c
}
enum EnumSpecified {
kind_a = 0x1,
kind_b = 0x2,
kind_c = 0x3
}