lang_2023/tests/test_code.lang
2023-06-08 16:43:23 +03:00

287 lines
5.1 KiB
Text

basic \float[#ord #div #str]
basic \int[#ord #idiv #str]
basic \string[#ord #str #char-container #copy]
basic \char[#ord #str #copy]
basic \bool[#ord #str #copy]
basic \unit[#str #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
)
//
typeclass \char-container =
& var size : -> \int
& var at : \int -> \char
//
typeclass \move = // TODO
& var ( <- ) : \move -> \unit
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
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
namespace var \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
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)
}
//
typeclass \show =
& var show : -> \string
typeclass \read =
& read : \string -> \read
typeclass \str[#show #read]
// typeclass debug-show = // TODO
// & debugdshow : -> \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
//
namespace io {
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
//
decl ( -- ) : \int -> \int -> \int`0
def ( -- ) : begin end = {
var current = begin
return (while current < end do {
; current += 1
bring current - 1
})
}
// decl func : \string -> \int -> \int
// def func : s i = {
// ; print: s
// var x = s
// ; print: (i..show:)
// return 5
// }
//
// exec main {
// for i in 0--19 do func: "a" (i * 2 +. 3)
// ; print: ({
// if true then bring scan: else { ; print: "aaa" }
// bring "nothing"
// ; print: "aaa"
// })
// }
decl scan-int : -> \int
def scan-int = \int..read: (\io..scan:)
decl print-int : \int -> \unit
def print-int : x = \io..print: (x..show:)
decl scan-anything 'a[#read] : -> 'a
def scan-anything = 'a..read: (\io..scan:)
decl print-anything 'a[#show] : 'a -> \unit
def print-anything : x = \io..print: (x..show:)
// decl sorted 'a[#ord #copy]: 'a`0 -> \int -> 'a`0
// def sorted : a sz = {
// var a-copy = a
// if sz == 2 then {
// if a-copy`0 > a-copy`1 then {
// var x = a-copy`0
// a-copy`0 = a-copy`1
// a-copy`1 = x
// }
// return a-copy
// }
//
// var center = sz..div: 2
//
// var a-left = for i in 0--center do a`i
// var a-right = for i in center-sz do a`i
//
// return a-copy
// }
// struct \array 'a = & 'a // 'a`0
// namespace \array {
// decl of : 'a`0 -> \array['a]
// def of : x = $array['a] & data = x
// }
struct \three-tuple = & \string & \string & \string
decl scan-three-t : -> \three-tuple
def scan-three-t = $three-tuple & \io..scan: & \io..scan: & \io..scan:
decl scan-three : -> (& \string & \string & \string)
def scan-three = & \io..scan: & \io..scan: & \io..scan:
exec main {
var n = scan-anything:[int]
var x = (for _ in 0--n do scan-int:) // $array[int] & 0
; print-anything:[int] n
var & a & b & c = scan-three-t:
; \io..print: b
var & d & e & f = scan-three:
; \io..print: e
}