mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-03 12:48:18 +00:00
changes for new grammar, fixes
This commit is contained in:
parent
91f9affadc
commit
3106a64949
35 changed files with 605 additions and 550 deletions
|
|
@ -1,19 +1,19 @@
|
|||
basic (Float : #Ord #Div #Str)
|
||||
basic (Int : #Ord #IDiv #Str)
|
||||
basic (String : #Ord #Str #CharContainer #Copy)
|
||||
basic (Char : #Ord #Str #Copy)
|
||||
basic (Bool : #Ord #Str #Copy)
|
||||
basic (Unit : #Str #Copy)
|
||||
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
|
||||
decl not : \bool -> \bool
|
||||
def not : x =
|
||||
(match x with
|
||||
| true -> false
|
||||
| false -> true)
|
||||
|
||||
decl ( && ) : Bool -> Bool -> Bool
|
||||
decl ( && ) : \bool -> \bool -> \bool
|
||||
def ( && ) : x y =
|
||||
match x with
|
||||
| true -> (
|
||||
|
|
@ -23,7 +23,7 @@ def ( && ) : x y =
|
|||
)
|
||||
| false -> false
|
||||
|
||||
decl ( || ) : Bool -> Bool -> Bool
|
||||
decl ( || ) : \bool -> \bool -> \bool
|
||||
def ( || ) : x y =
|
||||
match x with
|
||||
| true -> true
|
||||
|
|
@ -35,28 +35,28 @@ def ( || ) : x y =
|
|||
|
||||
//
|
||||
|
||||
typeclass CharContainer =
|
||||
& var size : -> Int
|
||||
& var at : Int -> Char
|
||||
typeclass \char-container =
|
||||
& var size : -> \int
|
||||
& var at : \int -> \char
|
||||
|
||||
//
|
||||
|
||||
typeclass Move = // TODO
|
||||
& var ( <- ) : Move -> Unit
|
||||
typeclass \move = // TODO
|
||||
& var ( <- ) : \move -> \unit
|
||||
|
||||
typeclass Copy =
|
||||
& var ( = ) : Copy -> Unit
|
||||
typeclass \copy =
|
||||
& var ( = ) : \copy -> \unit
|
||||
|
||||
//
|
||||
|
||||
typeclass (Sum : #Copy) =
|
||||
& var ( += ) : Sum -> Unit
|
||||
& var ( -= ) : Sum -> Unit
|
||||
& var ( + ) : Sum -> Sum
|
||||
& var ( - ) : Sum -> Sum
|
||||
& zero : -> Sum
|
||||
typeclass \sum[#copy] =
|
||||
& var ( += ) : \sum -> \unit
|
||||
& var ( -= ) : \sum -> \unit
|
||||
& var ( + ) : \sum -> \sum
|
||||
& var ( - ) : \sum -> \sum
|
||||
& zero : -> \sum
|
||||
|
||||
namespace var Sum {
|
||||
namespace var \sum {
|
||||
def ( + ) : x = {
|
||||
var ans = self
|
||||
; ans += x
|
||||
|
|
@ -70,11 +70,11 @@ namespace var Sum {
|
|||
}
|
||||
}
|
||||
|
||||
typeclass (Mult : #Sum) =
|
||||
& var ( *= ) : Mult -> Unit
|
||||
& var ( * ) : Mult -> Mult
|
||||
typeclass \mult[#sum] =
|
||||
& var ( *= ) : \mult -> \unit
|
||||
& var ( * ) : \mult -> \mult
|
||||
|
||||
namespace var Mult {
|
||||
namespace var \mult {
|
||||
def ( * ) : x = {
|
||||
var ans = self
|
||||
; ans *= x
|
||||
|
|
@ -82,19 +82,19 @@ namespace var Mult {
|
|||
}
|
||||
}
|
||||
|
||||
typeclass (IDiv : #Mult) =
|
||||
& var div : IDiv -> IDiv
|
||||
& var mod : IDiv -> IDiv
|
||||
typeclass \idiv[#mult] =
|
||||
& var div : \idiv -> \idiv
|
||||
& var mod : \idiv -> \idiv
|
||||
|
||||
namespace var IDiv {
|
||||
def mod : x = self -. x * self.div: x
|
||||
namespace var \idiv {
|
||||
def mod : x = self -. x * self..div: x
|
||||
}
|
||||
|
||||
typeclass (Div : #Mult) =
|
||||
& var ( /= ) : Div -> Unit
|
||||
& var ( / ) : Div -> Div
|
||||
typeclass \div[#mult] =
|
||||
& var ( /= ) : \div -> \unit
|
||||
& var ( / ) : \div -> \div
|
||||
|
||||
namespace var Div {
|
||||
namespace var \div {
|
||||
def ( / ) : x = {
|
||||
var ans = self
|
||||
; ans /= x
|
||||
|
|
@ -104,39 +104,39 @@ namespace var Div {
|
|||
|
||||
//
|
||||
|
||||
typeclass Eq =
|
||||
& var ( == ) : Eq -> Bool
|
||||
& var ( != ) : Eq -> Bool
|
||||
typeclass \eq =
|
||||
& var ( == ) : \eq -> \bool
|
||||
& var ( != ) : \eq -> \bool
|
||||
|
||||
namespace var Eq {
|
||||
namespace var \eq {
|
||||
def ( != ) : x = not: (self == x)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
struct Order =
|
||||
| EQ
|
||||
| LT
|
||||
| GT
|
||||
struct \order =
|
||||
| $eq
|
||||
| $lt
|
||||
| $gt
|
||||
|
||||
typeclass (Ord : #Eq) =
|
||||
& var compare : Ord -> Order
|
||||
& var ( < ) : Ord -> Bool
|
||||
& var ( >= ) : Ord -> Bool
|
||||
& var ( > ) : Ord -> Bool
|
||||
& var ( <= ) : Ord -> Bool
|
||||
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
|
||||
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
|
||||
decl max 'a[#ord] : 'a -> 'a -> 'a
|
||||
def max : x y = if x < y then y else x
|
||||
|
||||
namespace var Ord {
|
||||
namespace var \ord {
|
||||
def compare : x =
|
||||
if self == x then $EQ
|
||||
elif self < x then $LT
|
||||
else $GT
|
||||
if self == x then $eq
|
||||
elif self < x then $lt
|
||||
else $gt
|
||||
|
||||
def ( >= ) : x = not: (self < x)
|
||||
def ( > ) : x = x < self
|
||||
|
|
@ -145,63 +145,64 @@ namespace var Ord {
|
|||
|
||||
//
|
||||
|
||||
typeclass Show =
|
||||
& var show : -> String
|
||||
typeclass \show =
|
||||
& var show : -> \string
|
||||
|
||||
typeclass Read =
|
||||
& read : String -> Read
|
||||
typeclass \read =
|
||||
& read : \string -> \read
|
||||
|
||||
typeclass (Str : #Show #Read)
|
||||
typeclass \str[#show #read]
|
||||
|
||||
// typeclass DebugShow = // TODO
|
||||
// & debug_show : -> String
|
||||
// typeclass debug-show = // TODO
|
||||
// & debugdshow : -> \string
|
||||
|
||||
//
|
||||
|
||||
typeclass Default =
|
||||
& default : -> Default
|
||||
typeclass \default =
|
||||
& default : -> \default
|
||||
|
||||
//
|
||||
|
||||
typeclass Bounded =
|
||||
& min_bound : -> Bounded
|
||||
& max_bound : -> Bounded
|
||||
& var is_max_bound : -> Bool
|
||||
& var is_min_bound : -> Bool
|
||||
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
|
||||
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
|
||||
namespace io {
|
||||
decl print : \string -> \unit
|
||||
decl scan : -> \string
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
// // 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 random : -> \int // TODO
|
||||
|
||||
//
|
||||
|
||||
decl ( -- ) : Int -> Int -> Int_0
|
||||
// // // 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 {
|
||||
|
|
@ -210,11 +211,11 @@ def ( -- ) : begin end = {
|
|||
})
|
||||
}
|
||||
|
||||
// decl func : String -> Int -> Int
|
||||
// decl func : \string -> \int -> \int
|
||||
// def func : s i = {
|
||||
// ; print: s
|
||||
// var x = s
|
||||
// ; print: (i.show:)
|
||||
// ; print: (i..show:)
|
||||
// return 5
|
||||
// }
|
||||
//
|
||||
|
|
@ -227,60 +228,60 @@ def ( -- ) : begin end = {
|
|||
// })
|
||||
// }
|
||||
|
||||
decl scan_int : -> Int
|
||||
def scan_int = Int.read: (IO.scan:)
|
||||
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 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 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 print-anything 'a[#show] : 'a -> \unit
|
||||
def print-anything : x = \io..print: (x..show:)
|
||||
|
||||
// decl sorted ('A : #Ord #Copy): 'A_0 -> Int -> 'A_0
|
||||
// decl sorted 'a[#ord #copy]: 'a`0 -> \int -> 'a`0
|
||||
// def sorted : a sz = {
|
||||
// var a_copy = a
|
||||
// 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
|
||||
// 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
|
||||
// return a-copy
|
||||
// }
|
||||
//
|
||||
// var center = sz.div: 2
|
||||
// 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
|
||||
// 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
|
||||
// return a-copy
|
||||
// }
|
||||
|
||||
struct Array 'A = & 'A // 'A_0
|
||||
// struct \array 'a = & 'a // 'a`0
|
||||
|
||||
// namespace Array {
|
||||
// decl of : 'A_0 -> (Array 'A)
|
||||
// def of : x = $(Array 'A) & data = x
|
||||
// namespace \array {
|
||||
// decl of : 'a`0 -> \array['a]
|
||||
// def of : x = $array['a] & data = x
|
||||
// }
|
||||
|
||||
struct ThreeTuple = & String & String & String
|
||||
struct \three-tuple = & \string & \string & \string
|
||||
|
||||
decl scan_three_t : -> ThreeTuple
|
||||
def scan_three_t = $ThreeTuple & IO.scan: & IO.scan: & IO.scan:
|
||||
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:
|
||||
decl scan-three : -> (& \string & \string & \string)
|
||||
def scan-three = & \io..scan: & \io..scan: & \io..scan:
|
||||
|
||||
exec main {
|
||||
var n = scan_anything Int:
|
||||
var x = $(Array Int) & 0 // (for _ in 0--n do scan_int:)
|
||||
; print_anything Int: n
|
||||
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
|
||||
var & a & b & c = scan-three-t:
|
||||
; \io..print: b
|
||||
var & d & e & f = scan-three:
|
||||
; \io..print: e
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue