Si-lang
Intro
Si (Si-lang) is a strongly-typed, C-like programming language. It is written in Go and uses LLVM as a target backend.
tests
make test
compile
make compile f="example/fib.si"
Examples
Si can link to standard C libraries and call functions from them. Here is an example of a program that calls the printf function from the standard C library.
Reversed LinkedList
i64 printf(i8 *fmt,... );
i8* malloc(i64 size);
type struct {
i64 data,
Node *next,
} Node;
Node* reverseList(Node* head) {
Node* prev = (Node*)NULL;
Node* current = head;
Node* next = (Node*)NULL;
while (current != (Node*)NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
i64 printList(Node* node) {
while (node != (Node*)NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
return 0;
}
Node* newNode(i64 data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = (Node*)NULL;
return node;
}
i64 main() {
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
printf("Original list: ");
printList(head);
head = reverseList(head);
printf("Reversed list: ");
printList(head);
return 0;
}
Architecture
- alecthomas/participle is used in the
parser package to parse the source code into an AST.
- Transform() called on the AST and returns a new AST that is easier to work with in the next (code generation step) step.
- In the
ast package llir/llvm is used for code generation. It is a Go package that generates easy to read, plain text LLVM IR.
- We call
llc to compile the LLVM IR into machine code.
Useful Resources
Code Generation
- LLVM IR and Go This is a good introduction to LLVM IR and how to use the llir/llvm package to generate LLVM IR in Go. This inspired me to start writing this compiler.
C syntax
LLVM IR