lang_2023/tests/test_code.lang

228 lines
3.7 KiB
Text
Raw Normal View History

basic (Float : #Ord #Div #Show)
basic (Int : #Ord #IDiv #Show)
2023-05-22 21:00:43 +03:00
basic (String : #Ord #Show #CharContainer #Copy)
basic (Char : #Ord #Show #Copy)
basic (Bool : #Ord #Show #Copy)
basic (Unit : #Show #Copy)
//
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
)
2023-05-21 17:00:59 +03:00
//
typeclass CharContainer =
& var size : -> Int
& var at : Int -> Char
//
2023-05-21 17:00:59 +03:00
typeclass Move =
& var ( <- ) : Move -> Unit // TODO
typeclass Copy =
& var ( = ) : Copy -> Unit
//
typeclass (Sum : #Copy) =
& var ( += ) : Sum -> Unit
& var ( -= ) : Sum -> Unit
& var ( + ) : Sum -> Sum
& var ( - ) : Sum -> Sum
& zero : -> Sum
namespace var Sum {
def ( + ) : x = {
var ans = self
; ans += x
return ans
}
def ( - ) : x = {
var ans = self
; ans -= x
return ans
}
}
typeclass (Mult : #Sum) =
& var ( *= ) : Mult -> Unit
& var ( * ) : Mult -> Mult
namespace var Mult {
def ( * ) : x = {
var ans = self
; ans *= x
return ans
}
}
typeclass (IDiv : #Mult) =
& var div : IDiv -> IDiv
& var mod : IDiv -> IDiv
2023-05-21 17:00:59 +03:00
namespace var IDiv {
def mod : x = self -. x * self.div: x
}
typeclass (Div : #Mult) =
& var ( /= ) : Div -> Unit
& var ( / ) : Div -> Div
namespace var Div {
def ( / ) : x = {
var ans = self
; ans /= x
return ans
}
}
//
typeclass Eq =
& var ( == ) : Eq -> Bool
& var ( != ) : Eq -> Bool
2023-05-13 22:40:33 +03:00
namespace var Eq {
def ( != ) : x = not: (self == x)
}
//
struct Order =
| EQ
| LT
| GT
typeclass (Ord : #Eq) =
2023-05-13 22:40:33 +03:00
& var compare : Ord -> Order
& var ( < ) : Ord -> Bool
& var ( >= ) : Ord -> Bool
& var ( > ) : Ord -> Bool
& var ( <= ) : Ord -> Bool
2023-05-13 22:40:33 +03:00
decl min ('A : #Ord) : 'A -> 'A -> 'A
def min : x y = if x < y then x else y
decl max ('A : #Ord) : 'A -> 'A -> 'A
def max : x y = if x < y then y else x
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)
}
//
2023-05-21 14:58:18 +03:00
typeclass Show =
& var show : -> String
// typeclass Read = // TODO
// & var read : String -> Read
// typeclass DebugShow = // TODO
// & debug_show : -> String
//
typeclass Default =
& default : -> Default
//
2023-05-13 14:44:25 +03:00
typeclass Bounded =
& min_bound : -> Bounded
& max_bound : -> Bounded
& var is_max_bound : -> Bool
& var is_min_bound : -> Bool
2023-05-13 14:44:25 +03:00
//
typeclass Enum =
& var succ : -> (Optional Enum)
& var pred : -> (Optional Enum)
& to_enum : Int -> Enum
& var from_enum : -> Int
//
decl print : String -> Unit
decl scan : -> String
decl random : -> Int // TODO
//
// // 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
//
2023-05-22 21:00:43 +03:00
decl ret_one : -> Int
def ret_one = 1
decl ( -- ) : Int -> Int -> Int_0
2023-05-21 14:58:18 +03:00
def ( -- ) : begin end = {
var current = begin
return (while current < end do {
2023-05-22 21:00:43 +03:00
; current += 1
bring current
//bring current - 1
2023-05-21 14:58:18 +03:00
})
}
2023-05-17 18:29:39 +03:00
decl func : String -> Int -> Int
def func : s i = {
; print: s
; print: (i.show:)
return 5
}
exec main {
2023-05-22 21:00:43 +03:00
for i in 0--9 do func: "abacaba" (i - 1)
; print: ({
2023-05-21 14:58:18 +03:00
if true then bring scan: else { ; print: "aaa" }
bring "nothing"
; print: "aaa"
})
}