lang_2023/tests/test_code.lang

140 lines
2.2 KiB
Text

// basic Float
// basic Int
// basic String
// basic Char
// basic Bool
// basic Unit
//
decl not : Bool -> Bool
def not : x =
(match x with
| true -> false
| false -> true)
decl ( && ) : Bool -> Bool -> Bool
def ( && ) : x y =
match x with
| true -> (
match y with
| true -> true
| false -> false
)
| false -> false
decl ( || ) : Bool -> Bool -> Bool
def ( || ) : x y =
match x with
| true -> true
| false -> (
match y with
| true -> true
| false -> false
)
// Eq typeclass
typeclass Eq =
& var ( == ) : Eq -> Bool
& var ( != ) : Eq -> Bool
namespace const Eq {
def ( != ) : x = not: (self == x)
}
//
struct Order =
| EQ
| LT
| GT
typeclass (Ord : #Eq) =
& var compare: Ord -> Order
& var ( < ) : Ord -> Bool
& var ( >= ) : Ord -> Bool
& var ( > ) : Ord -> Bool
& var ( <= ) : Ord -> Bool
& var min : Ord -> Ord
& var max : Ord -> Ord
namespace var Ord {
def compare : x =
if self == x then $EQ
elif self < x then $LT
else $GT
def ( >= ) : x = not: (self < x)
def ( > ) : x = x < self
def ( <= ) : x = not: (x < self)
def min : x = if self < x then self else x
def max : x = if self < x then x else self
}
//
typeclass Show =
& var show : -> String
typeclass Read =
& var read : String -> Read
typeclass Debug =
& debug : -> String
//
typeclass Default =
& default : -> Default
//
typeclass Bounded =
& min_bound : -> Bounded
& max_bound : -> Bounded
& var is_max_bound : -> Bool
& var is_min_bound : -> Bool
//
typeclass Enum =
& var succ : -> (Optional Enum)
& var pred : -> (Optional Enum)
& to_enum : Int -> Enum
& var from_enum : -> Int
//
// // bad
// typeclass Functor 'A =
// & fmap 'B ('F : (#Functor 'B)) : ('A -> 'B) -> Functor -> 'F
// typeclass (Iterator : #Eq) =
// & next : -> Unit
// & prev : -> Unit
//
// typeclass Iterable ('Iter : #Iterable) =
// & begin : -> 'Iter
// & end : -> 'Iter
//
decl ( -- ) : Int -> Int -> Int_0
def ( -- ) : begin end = {
var current = begin
return (while current < end do {
; current += 1
return current - 1
})
}
decl func : String -> Int
def func : s = {
; print: s
return 5
}
exec main {
for i in 0--10 do func: "abacaba"
}