-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples of planned features (#401)
* Add examples for planned features * Make example syntax more consistent * Add gadts examples with functional dependencies * Make example syntax more consistent * Chmod examples for the interpreter * Add shebangs * Add instances examples
- Loading branch information
Showing
10 changed files
with
148 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env tako | ||
|
||
test() = { | ||
allocator = import("allocator") | ||
ptr = allocator.allocate(sizeof(Char[100])) | ||
if (ptr == nullptr) { | ||
print("Success") | ||
exit(0) | ||
} else { | ||
print("Failure") | ||
} | ||
} | ||
|
||
main() = { | ||
allocator = import("allocator") | ||
logger = import("logger") | ||
|
||
test_logger = logger.new_logger() | ||
with { | ||
allocator.allocate = allocator.report_oom, | ||
system.out = test_logger, | ||
system.error = test_logger, | ||
run = test | ||
} | ||
|
||
print(test_logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env tako | ||
|
||
boolean = Enum.new( | ||
true=1, | ||
false=0, | ||
) | ||
|
||
boolean.import_literals() | ||
|
||
color = Enum | ||
.based_on( | ||
byte[3] | ||
) | ||
.open( | ||
red=#ff0000, | ||
green=#00ff00, | ||
blue=#0000ff, | ||
) | ||
|
||
color.add_values( | ||
purple=#ff00ff, | ||
cyan=#00ffff, | ||
) | ||
|
||
main() = { | ||
print("Color {red} is red") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
fib<T: Nat>(n: T): T=if(n<=1, 1, fib(n-1)+fib(n-2)); | ||
#!/usr/bin/env tako | ||
|
||
fib(n: T, {T: Nat}): T=if(n<=1, 1, fib(n-1)+fib(n-2)) | ||
|
||
print(fib(80)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env tako | ||
|
||
fib_helper(n: T, a: T, b: T, {T: Nat}): T=if(n==0, a, fib_helper(n-1, b, a+b)) | ||
|
||
fib(n: T, {T: Nat}): T=fib_helper(n, 0, 1) | ||
|
||
print(fib(80)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env tako | ||
|
||
// A simple, untyped implementation of S expressions. | ||
SExpr: Type = struct.new( | ||
func: (args: Value[n]), | ||
args: SExpr[n], | ||
{n: Int}, | ||
) | ||
|
||
// A well typed, but little, language using GADTs. | ||
Expr( | ||
SubExpr(R: AllowedType): Reference(Expr(SubExpr, R))=Expr, | ||
R: AllowedType, | ||
with {AllowedType = Integer|Boolean} | ||
) = gadt.new( | ||
|
||
// Control flow: | ||
if( | ||
cond: SubExpr(R=Boolean), | ||
then: SubExpr(R=T), | ||
else: SubExpr(R=T), | ||
{T: AllowedType} | ||
): Expr(SubExpr, R=T), | ||
|
||
// Binary operations: | ||
or (left: SubExpr(R=Boolean), right: SubExpr(R=Boolean)): Expr(SubExpr, R=Boolean), | ||
and (left: SubExpr(R=Boolean), right: SubExpr(R=Boolean)): Expr(SubExpr, R=Boolean), | ||
add (left: SubExpr(R=Integer), right: SubExpr(R=Integer)): Expr(SubExpr, R=Integer), | ||
mul (left: SubExpr(R=Integer), right: SubExpr(R=Integer)): Expr(SubExpr, R=Integer), | ||
eq (left: SubExpr(R=Integer), right: SubExpr(R=Integer)): Expr(SubExpr, R=Boolean), | ||
|
||
// Constants: | ||
integer(value: Integer): Expr(SubExpr, R=Integer), | ||
boolean(value: Boolean): Expr(SubExpr, R=Boolean), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Bounded = Enum | ||
.open() | ||
.based_on({ | ||
min: (T: Type): T, | ||
max: (T: Type): T, | ||
}) | ||
|
||
Bounded.add_values( | ||
boolean = { | ||
min(T)=false, | ||
max(T)=true, | ||
} | ||
integer = { | ||
min(T)=0, | ||
max(T)=2**64-1, | ||
} | ||
) | ||
|
||
Showable = Enum.based_on({ | ||
to_string: (t: Type): String | ||
}).open() | ||
|
||
print(t: Type, {end: String="\n"}): String = system.out.write(t.to_string()+end) | ||
|
||
to_base(value: integer, {base: integer=10}) = { | ||
out: Char[math.log(Integer.max, base)]="0"; | ||
i: integer = 0; | ||
while( | ||
cond = t > 0, | ||
loop = { | ||
out[-i] = value%10, | ||
value=value/10, | ||
i=i+1 | ||
}, | ||
result = out[-i..], | ||
) | ||
} | ||
|
||
Showable.add_values( | ||
char(t: Char)=String.from_chars([t]), | ||
chars(t: Char[n], {n: Integer})=String.from_chars(t), | ||
string(t: String)=t, | ||
integer(t: Integer)=t.to_base(base=10) | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#!/usr/bin/env tako | ||
|
||
main = { | ||
x3by2 = Vector.new([ | ||
[ 0, 1 ], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
main = { | ||
#!/usr/bin/env tako | ||
|
||
main() = { | ||
x3by2 = Vector.new([ | ||
[ 0, 1 ], | ||
[ 2, 3 ], | ||
|