lang_2023/tests/test_code.lang

292 lines
5.3 KiB
Text
Raw Normal View History

2023-06-03 19:01:03 +03:00
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]
//
2023-07-03 19:05:50 +03:00
namespace io {
decl print : \string -> \unit
decl scan : -> \string
}
decl random : -> \int // TODO
decl error : \string -> \unit
decl some 'a : 'a -> 'a?
decl none 'a : 'a -> 'a?
2023-07-03 19:05:50 +03:00
//
2023-06-03 19:01:03 +03:00
decl not : \bool -> \bool
2023-07-06 23:23:19 +03:00
def not : x =
(match x with
| true -> false
| false -> true)
2023-05-21 17:00:59 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \char-container =
& var size : -> \int
& var at : \int -> \char
//
2023-06-03 19:01:03 +03:00
typeclass \move = // TODO
& var ( <- ) : \move -> \unit
2023-05-21 17:00:59 +03:00
2023-06-03 19:01:03 +03:00
typeclass \copy =
& var ( = ) : \copy -> \unit
2023-05-21 17:00:59 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \sum[#copy] =
& var ( += ) : \sum -> \unit
& var ( -= ) : \sum -> \unit
& var ( + ) : \sum -> \sum
& var ( - ) : \sum -> \sum
& zero : -> \sum
2023-05-21 17:00:59 +03:00
2023-06-03 19:01:03 +03:00
namespace var \sum {
2023-05-21 17:00:59 +03:00
def ( + ) : x = {
var ans = self
; ans += x
return ans
}
def ( - ) : x = {
var ans = self
; ans -= x
return ans
}
}
2023-06-03 19:01:03 +03:00
typeclass \mult[#sum] =
& var ( *= ) : \mult -> \unit
& var ( * ) : \mult -> \mult
2023-05-21 17:00:59 +03:00
2023-06-03 19:01:03 +03:00
namespace var \mult {
2023-05-21 17:00:59 +03:00
def ( * ) : x = {
var ans = self
; ans *= x
return ans
}
}
2023-06-03 19:01:03 +03:00
typeclass \idiv[#mult] =
& var div : \idiv -> \idiv
& var mod : \idiv -> \idiv
2023-05-21 17:00:59 +03:00
2023-06-03 19:01:03 +03:00
namespace var \idiv {
def mod : x = self -. x * self..div: x
2023-05-21 17:00:59 +03:00
}
2023-06-03 19:01:03 +03:00
typeclass \div[#mult] =
& var ( /= ) : \div -> \unit
& var ( / ) : \div -> \div
2023-05-21 17:00:59 +03:00
2023-06-03 19:01:03 +03:00
namespace var \div {
2023-05-21 17:00:59 +03:00
def ( / ) : x = {
var ans = self
; ans /= x
return ans
}
}
//
2023-06-03 19:01:03 +03:00
typeclass \eq =
& var ( == ) : \eq -> \bool
& var ( <> ) : \eq -> \bool
2023-06-03 19:01:03 +03:00
namespace var \eq {
def ( <> ) : x = not: (self == x)
}
//
2023-06-03 19:01:03 +03:00
struct \order =
| $eq
| $lt
| $gt
2023-06-03 19:01:03 +03:00
typeclass \ord[#eq] =
& var compare : \ord -> \order
& var ( < ) : \ord -> \bool
& var ( >= ) : \ord -> \bool
& var ( > ) : \ord -> \bool
& var ( <= ) : \ord -> \bool
2023-05-13 22:40:33 +03:00
2023-06-03 19:01:03 +03:00
decl min 'a[#ord] : 'a -> 'a -> 'a
2023-05-13 22:40:33 +03:00
def min : x y = if x < y then x else y
2023-06-03 19:01:03 +03:00
decl max 'a[#ord] : 'a -> 'a -> 'a
2023-05-13 22:40:33 +03:00
def max : x y = if x < y then y else x
2023-06-03 19:01:03 +03:00
namespace var \ord {
def compare : x =
2023-06-03 19:01:03 +03:00
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-06-03 19:01:03 +03:00
typeclass \show =
& var show : -> \string
2023-05-21 14:58:18 +03:00
2023-06-03 19:01:03 +03:00
typeclass \read =
& read : \string -> \read
2023-05-22 22:02:31 +03:00
2023-06-03 19:01:03 +03:00
typeclass \str[#show #read]
2023-06-03 19:01:03 +03:00
// typeclass debug-show = // TODO
// & debugdshow : -> \string
//
2023-06-03 19:01:03 +03:00
typeclass \default =
& default : -> \default
//
2023-05-13 14:44:25 +03:00
2023-06-03 19:01:03 +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
//
2023-06-03 19:01:03 +03:00
typeclass \enum =
& var succ : -> \optional[enum]
& var pred : -> \optional[enum]
& to-enum : \int -> \enum
& var from-enum : -> \int
//
2023-06-03 19:01:03 +03:00
// // // bad
// // typeclass \functor 'a =
// // & fmap 'b ('f[#functor['b]]) : ('a -> 'b) -> \functor -> 'f
//
// // typeclass \iterator[#eq] =
// // & next : -> \unit
// // & prev : -> \unit
//
2023-06-03 19:01:03 +03:00
// // typeclass \iterable 'iter[#iterable] =
// // & begin : -> 'iter
// // & end : -> 'iter
//
2023-06-03 19:01:03 +03:00
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
2023-05-22 22:02:31 +03:00
bring current - 1
2023-05-21 14:58:18 +03:00
})
}
2023-05-17 18:29:39 +03:00
2023-06-03 19:01:03 +03:00
// decl func : \string -> \int -> \int
2023-05-23 00:51:51 +03:00
// def func : s i = {
// ; print: s
// var x = s
2023-06-03 19:01:03 +03:00
// ; print: (i..show:)
2023-05-23 00:51:51 +03:00
// 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"
// })
// }
2023-06-03 19:01:03 +03:00
decl scan-int : -> \int
def scan-int = \int..read: (\io..scan:)
2023-05-23 00:51:51 +03:00
2023-07-07 17:59:32 +03:00
decl print-int : \int -> \unit
2023-06-03 19:01:03 +03:00
def print-int : x = \io..print: (x..show:)
2023-05-23 00:51:51 +03:00
2023-07-07 17:59:32 +03:00
decl print-int-with-comment : ::i \int -> \string? -> \unit
def print-int-with-comment : i maybe-comment = {
; \io..print: (i..show:)
var comment? = maybe-comment in \io..print: comment
}
2023-06-03 19:01:03 +03:00
decl scan-anything 'a[#read] : -> 'a
def scan-anything = 'a..read: (\io..scan:)
2023-05-23 00:51:51 +03:00
2023-06-03 19:01:03 +03:00
decl print-anything 'a[#show] : 'a -> \unit
def print-anything : x = \io..print: (x..show:)
2023-05-23 00:51:51 +03:00
2023-06-03 19:01:03 +03:00
// decl sorted 'a[#ord #copy]: 'a`0 -> \int -> 'a`0
2023-05-23 11:54:15 +03:00
// def sorted : a sz = {
2023-06-03 19:01:03 +03:00
// var a-copy = a
2023-05-23 11:54:15 +03:00
// if sz == 2 then {
2023-06-03 19:01:03 +03:00
// if a-copy`0 > a-copy`1 then {
// var x = a-copy`0
// a-copy`0 = a-copy`1
// a-copy`1 = x
2023-05-23 11:54:15 +03:00
// }
2023-06-03 19:01:03 +03:00
// return a-copy
2023-05-23 11:54:15 +03:00
// }
//
2023-06-03 19:01:03 +03:00
// var center = sz..div: 2
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
// var a-left = for i in 0--center do a`i
// var a-right = for i in center-sz do a`i
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
// return a-copy
2023-05-23 11:54:15 +03:00
// }
2023-05-23 00:51:51 +03:00
2023-07-06 23:23:19 +03:00
struct \array 'a = & 'a`0
2023-07-06 23:23:19 +03:00
namespace \array {
decl of : 'a`0 -> \array['a]
def of : x = $array['a] & x
}
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
struct \three-tuple = & \string & \string & \string
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
decl scan-three-t : -> \three-tuple
def scan-three-t = $three-tuple & \io..scan: & \io..scan: & \io..scan:
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
decl scan-three : -> (& \string & \string & \string)
def scan-three = & \io..scan: & \io..scan: & \io..scan:
2023-05-23 11:54:15 +03:00
exec main {
// var n = \int..read: (\io..scan:)
//
// if n <= 0 then error: "n can't be less then 1"
//
// // var x = (for _ in 0--n do scan-int:)
// var x = \array[int]..of: (for _ in 0--n do scan-int:)
//
//
// var k? = if n < 2 then n * 2 +. 3 in
// , print-anything:[string] "n < 2"
// , print-anything:[int] k
//
// ; print-anything:[int] n
2023-07-06 23:23:19 +03:00
; print-int-with-comment: ::i 123 (some: "comment")
2023-07-06 23:23:19 +03:00
// var & a & b & c = scan-three-t:
// ; \io..print: b
// var & d & e & f = scan-three:
// ; \io..print: e
}