mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
fixes, new examples
This commit is contained in:
parent
823fa30fa8
commit
7f4266821c
15 changed files with 322 additions and 74 deletions
|
|
@ -9,13 +9,13 @@ def test_arrays = {
|
|||
|
||||
var arr6 <- String._new_array: 10
|
||||
var arr6_reference = ^arr6
|
||||
|
||||
const elem1 = arr1`0
|
||||
var elem2 = arr1`2
|
||||
const ref1 = ^arr1`1
|
||||
var ref2 = ^arr1`3
|
||||
; arr1`1 = 123
|
||||
|
||||
; ~ref1 = arr1`2 // set value
|
||||
; ref1 = ref2 // set pointer / reference
|
||||
//
|
||||
// const elem1 = arr1`0
|
||||
// var elem2 = arr1`2
|
||||
// const ref1 = ^arr1`1
|
||||
// var ref2 = ^arr1`3
|
||||
// ; arr1`1 = 123
|
||||
//
|
||||
// ; ~ref1 = arr1`2 // set value
|
||||
// ; ref1 = ref2 // set pointer / reference
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,188 @@
|
|||
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)
|
||||
|
||||
//
|
||||
|
||||
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 CharContainer =
|
||||
& 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 DebugShow = // TODO
|
||||
// & debug_show : -> 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
|
||||
}
|
||||
|
||||
//
|
||||
|
|
@ -41,8 +41,8 @@ typeclass CharContainer =
|
|||
|
||||
//
|
||||
|
||||
typeclass Move =
|
||||
& var ( <- ) : Move -> Unit // TODO
|
||||
typeclass Move = // TODO
|
||||
& var ( <- ) : Move -> Unit
|
||||
|
||||
typeclass Copy =
|
||||
& var ( = ) : Copy -> Unit
|
||||
|
|
@ -201,9 +201,6 @@ namespace IO {
|
|||
|
||||
//
|
||||
|
||||
decl ret_one : -> Int
|
||||
def ret_one = 1
|
||||
|
||||
decl ( -- ) : Int -> Int -> Int_0
|
||||
def ( -- ) : begin end = {
|
||||
var current = begin
|
||||
|
|
@ -242,17 +239,48 @@ 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 = & data : 'A_0
|
||||
|
||||
namespace Array {
|
||||
decl construct : 'A_0 -> Array
|
||||
def construct: x = $(Array 'A) & data = x
|
||||
} // TODO: construct decl + default def -> segfault
|
||||
*/
|
||||
decl of : 'A_0 -> Array
|
||||
def of: x = $(Array 'A) & data = x
|
||||
}*/
|
||||
|
||||
struct ThreeTuple = & String & String & String
|
||||
|
||||
decl scan_three_t : -> ThreeTuple
|
||||
def scan_three_t = $ThreeTuple & IO.scan: & IO.scan: & IO.scan:
|
||||
|
||||
decl scan_three : -> (& String & String & String)
|
||||
def scan_three = & IO.scan: & IO.scan: & IO.scan:
|
||||
|
||||
// var n = scan_anything Int:
|
||||
// var a = $(Array Int) & data = (for _ in 0--n do scan_int:)
|
||||
// ; print_anything Int: n
|
||||
|
||||
exec main {
|
||||
var n = scan_anything Int:
|
||||
var a = for _ in 0--n do scan_int:
|
||||
; print_anything Int: n
|
||||
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