Skip to content

Commit 93a23cf

Browse files
committed
doc update
1 parent b267bde commit 93a23cf

File tree

4 files changed

+75
-37
lines changed

4 files changed

+75
-37
lines changed

FE/Docs/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
## Frontend
22

3-
[Overview](tutorial.md)
3+
[Language Overview](tutorial.md)
44

55
[AST Node Description](ast.md)
66

77
[Macro System](macros.md)
88

99
[Type System](types.md)
1010

11+
[Union Types](union_types.md)
12+
1113
[Casting](casting.md)
1214

15+
[Polymorphism](polymorphism.md)
16+
1317
[Codegen For Conditional Expressions](codegen_for_cond_exprs.md)

FE/Docs/historical.md

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
# Cwerg Language
1+
# Design Choices
22

3-
Cwerg tries to find the right balance between language expressiveness and compiler implementation complexity.
4-
The hope is to reach a sweet spot *above* what C gives us today and make it convenient to write
5-
system software like operating systems and compilers in this language.
6-
7-
8-
Warning: This is still quite experimental. We expexct to gain some
9-
more insights about the design-space by bootstrapping the front- and backend.
10-
11-
### Philosophy
12-
13-
Above all Cwerg is meant to be a **small** language. Since small is subjective we have
14-
set a complexity budget for about 10kLOC for a compiler frontend with basic optimizations.
15-
16-
All control flow and all memory allocation is explicit.
17-
18-
Discouraged practices are possible but require explicit overrides, e.g.:
19-
uninitialized variables, global visibility, mutability, unchecked array accesses,
20-
untagged unions, ...
21-
...
223

234
## Syntax
245

@@ -30,25 +11,9 @@ Comments wil be explicit in the AST and cannot occur in arbitrary places.
3011
Similarly, parenthesis used to group expression will be modelled in the AST.
3112

3213
* [List of S-Expression Nodes](../FrontEndDocs/ast.md)
33-
* [Macros](../FrontEndDocs/macros.md)
34-
* [Casting](../FrontEndDocs/casting.md)
35-
36-
37-
## Code Examples (using S-Expr Syntax)
3814

39-
* [Print Argv](TestData/print_argv.cw)
40-
* [Heap Sort](TestData/heapsort.cw)
41-
* [Fizzbuzz](TestData/fizzbuzz.cw)
42-
* [Sieve](TestData/sieve.cw)
43-
* [Word Count](TestData/wordcount.cw)
4415

45-
## Code Examples (using Concrete Syntax)
4616

47-
* [Print Argv](ConcreteSyntax/TestData/print_argv.cw)
48-
* [Fizzbuzz](ConcreteSyntax/TestData/fizzbuzz.cw)
49-
* [Sieve](ConcreteSyntax/TestData/sieve.cw)
50-
* [Word Count](ConcreteSyntax/TestData/wordcount.cw)
51-
*
5217
## Features Relative to C
5318

5419
Added

FE/Docs/polymorphism.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Polymorphism
2+
3+
Besides parameterized modules, Cwerg supports a limited form of (ad-hoc) polymorphism for functions.
4+
There is no dynamic dispatch as everything is resolved at compile-time.
5+
6+
Example:
7+
```
8+
fun SysRender@(v u8, out span!(u8)) uint:
9+
return FmtDecU8(v, out)
10+
```
11+
12+
**Note the trailing "@" which indicates that a function is polymorphic.**
13+
14+
This example has two effects:
15+
16+
1. It defines a function that render an `u8` value in decimal into the buffer `out` and returns the number of characters written.
17+
2. It registers a family of functions named `SysRender@` that only differ in their first argument.
18+
19+
20+
21+
Functions can be added to this family by re-using the name like so
22+
```
23+
fun SysRender@(v u32, out span!(u8)) uint:
24+
return FmtDecU32(v, out)
25+
```
26+
27+
Note, this function has a different first parameter type for the first argument while the other stay the same.
28+
**Cwerg's polymorphism applieds to the first argument only!**
29+
30+
Printing in hex instead of decimal could be accomplished like so:
31+
32+
```
33+
pub wrapped type u32_hex = u32
34+
35+
fun SysRender@(v u32_hex, out span!(u8)) uint:
36+
return FmtHexU32(unwrap(v), out)
37+
```
38+
39+
Assuming the functions above are defined in a module named `fmt` they can be called like so:
40+
```
41+
...
42+
ref let! buf_raw = [1024]u8{}
43+
let! buf span!(u8) = buf_raw
44+
set buf = IncSpan(buf, fmt::SysRender@(666_u32, buf))
45+
set buf = IncSpan(buf, fmt::SysRender@(66_u8, buf))
46+
set buf = IncSpan(buf, fmt::SysRender@(
47+
wrap_as(666, fmt:: u32_hex), buf))
48+
...
49+
```
50+
51+
Defining SysRender@ for a data type in another module can be accomplished like so:
52+
```
53+
rec Point:
54+
x u32
55+
y u32
56+
57+
fun fmt::SysRender@(v Point, out span!(u8)) uint:
58+
let! buf = out
59+
set buf = IncSpan(buf, fmt::SysRender@(v.x, buf))
60+
set buf = IncSpan(buf, fmt::SysRender@(", ", buf))
61+
set buf = IncSpan(buf, fmt::SysRender@(v.y, buf))
62+
return len(out) - len(buf)
63+
64+
```
65+
66+
Note, that the definition uses a `qualified` name referencing the family of
67+
polymorphic function we want to add to.

FE/Docs/tutorial.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ Functions can only have one result.
564564

565565
Function parameters are not mutable.
566566

567+
Polymorphism is described [here](polymorphism.md)
568+
567569
### Enums, Types (Typedefs) and Recs (Structs)
568570

569571
These were covered in the Type Section above

0 commit comments

Comments
 (0)