Skip to content

Commit

Permalink
update readme, add more snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjunphy committed Jul 9, 2024
1 parent 1bac972 commit 24f1896
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This implementation relies on [Alex](https://github.com/haskell/alex) for lexing
The parser and semantic might not be exactly conformant to the [decaf spec](https://cons.mit.edu/fa18/handout-pdfs/01-decaf-spec.pdf). Rather it should be a superset. All valid decaf programs should be compiled to code with the expected behavior. In addition the implementation should also be able to compile some other intuitively reasonable programs which might be invalid in the spec.


Curently the LLVM IR backend is mostly working. The compiler should be able to compile some simple code snippets. For example a "hellow world":
Curently the LLVM IR backend is mostly working. (Note There are still lots of bugs though). The compiler should be able to compile some simple code snippets. For example a "hellow world":
```
import printf;
Expand All @@ -36,6 +36,57 @@ llc -filetype=obj hello_world.ll -o hello_world.o
clang hello_world.o -o hello_world
```

## Some working examples
Sum from 1 to 50:
```
import printf;
void main() {
int res;
int i;
res = 0;
for (i = 1; i <= 50; i+=1) {
res = res + i;
}
printf("sum(1..50) = %d\n", res);
}
```
Produces:
```
sum(1..50) = 1275
```


Fibonacci series:
```
import printf;
void main() {
int i;
int fib1;
int fib2;
int temp;
i = 0;
fib1 = 1;
fib2 = 1;
temp = 0;
printf("fibonacci:");
printf(" %d", fib1);
printf(" %d", fib2);
for (i = 3; i <= 10; i++) {
temp = fib1 + fib2;
fib1 = fib2;
fib2 = temp;
printf(" %d", temp);
}
printf("\n");
}
```
Produces:
```
fibonacci: 1 1 2 3 5 8 13 21 34 55
```

## Some random comments
Programming in Haskell is an interesting experience. The design of this language is dramatically different from most others. Unfortunately the tooling and the ecosystems are quite lacking. The Haskell language server is OK, however debugging tools are practically non-exist. You can debug simple scripts with an interactive session or `Debug.Trace` to print some information. But as the complexity of your program goes up, tools like GDB are really important to save some headaches. Also it seems many of the Haskell packages are either short of maintainers or having quality issues. The LLVM binding has been stagnant since a few years ago. It is hard to imagine this could happen for a language which was claimed to be good for compiler development. The GraphViz package which I used at some point had bugs when handling escape sequences, because of which I have to write my own graph drawing functions.

Expand Down
22 changes: 22 additions & 0 deletions test/snippets/fibonacci.dcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import printf;

void main() {
int i;
int fib1;
int fib2;
int temp;
i = 0;
fib1 = 1;
fib2 = 1;
temp = 0;
printf("fibonacci:");
printf(" %d", fib1);
printf(" %d", fib2);
for (i = 3; i <= 10; i++) {
temp = fib1 + fib2;
fib1 = fib2;
fib2 = temp;
printf(" %d", temp);
}
printf("\n");
}

0 comments on commit 24f1896

Please sign in to comment.