mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-05 22:38:16 +00:00
const -> let
This commit is contained in:
parent
3ae6ed079d
commit
189306df26
33 changed files with 406 additions and 285 deletions
|
|
@ -1,18 +1,18 @@
|
|||
decl test-arrays : -> \unit
|
||||
def test-arrays = {
|
||||
var arr1 = ,1 ,2 ,3
|
||||
const arr2 = \int..array: 32
|
||||
let arr2 = \int..array: 32
|
||||
var arr3 = \string..array: 11
|
||||
const arr4 = ''a--''z
|
||||
const n = 100
|
||||
let arr4 = ''a--''z
|
||||
let n = 100
|
||||
var arr5 <- \int..new-array: 10
|
||||
|
||||
var arr6 <- \string..new-array: 10
|
||||
var arr6-reference = ^arr6
|
||||
|
||||
const elem1 = arr1`0
|
||||
let elem1 = arr1`0
|
||||
var elem2 = arr1`2
|
||||
const ref1 = ^arr1`1
|
||||
let ref1 = ^arr1`1
|
||||
var ref2 = ^arr1`3
|
||||
; arr1`1 = 123
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,20 @@
|
|||
decl flow-control-test : -> \unit
|
||||
def flow-control-test = {
|
||||
if (a < b ||. a == b) && (b < c) then \io..print: x
|
||||
elif x < 0 then {
|
||||
if && ( || a < b
|
||||
|| a == b )
|
||||
&& b < c
|
||||
then \io..print: x
|
||||
elif x < 0
|
||||
then {
|
||||
; x += 1
|
||||
; \io..print: y
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
|
||||
while (a > 0) && not: (array..is-empty:) do {
|
||||
while && a > 0
|
||||
&& not: (array..is-empty:)
|
||||
do {
|
||||
; a -= 1
|
||||
; array..pop:
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,15 +30,15 @@ def find-prefix-hashes : str = {
|
|||
|
||||
alias \hash = \acc-hash[char]
|
||||
|
||||
decl find-substring : \string -> \string -> \array[index]
|
||||
decl find-substring : ::str \string -> ::substr \string -> \array[index]
|
||||
def find-substring : str substr = {
|
||||
var result = \array[index]..empty:
|
||||
|
||||
const str-hashes = find-prefix-hashes:[hash] str
|
||||
const substr-hash = \hash..of: substr
|
||||
let str-hashes = find-prefix-hashes:[hash] str
|
||||
let substr-hash = \hash..of: substr
|
||||
|
||||
for i in 0--(str-hashes..size: - substr..size:) do {
|
||||
const part-hash = hash..diff: str-hashes`(i + substr..size:) str-hashes`i
|
||||
let part-hash = hash..diff: str-hashes`(i + substr..size:) str-hashes`i
|
||||
|
||||
if part-hash == substr-hash then {
|
||||
; result..push: i
|
||||
|
|
@ -60,3 +60,9 @@ def mul : x y = x * y
|
|||
|
||||
decl mul-10 : \int -> \int
|
||||
def mul-10 = mul: 10
|
||||
|
||||
exec main {
|
||||
; find-substring:
|
||||
::str "abacaba"
|
||||
::substr "bac"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
decl test-lambdas : -> \unit
|
||||
def test-lambdas = {
|
||||
const lambda1 = \\x -> x * x
|
||||
const lambda2 = \\x -> x..hash:
|
||||
const lambda3 = \\x y -> x + y
|
||||
let lambda1 = \\x -> x * x
|
||||
let lambda2 = \\x -> x..hash:
|
||||
let lambda3 = \\x y -> x + y
|
||||
|
||||
const lambda4 = \\x -> {
|
||||
let lambda4 = \\x -> {
|
||||
; \io..print: x
|
||||
const y = x + x
|
||||
let y = x + x
|
||||
return y
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ def fruit-cost : fruit = {
|
|||
|
||||
decl amount-to-string : \int -> \bool -> \string
|
||||
def amount-to-string : x is-zero-separated = {
|
||||
const ans = match x with
|
||||
let ans = match x with
|
||||
| 0 ?? is-zero-separated -> "Zero"
|
||||
| 0 | 1 | 2 | 3 | 4 -> "Few"
|
||||
| x ?? (5--9)..contains: x -> "Several"
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@ struct \struct-with-ref =
|
|||
|
||||
decl test-memory : -> \unit
|
||||
def test-memory = {
|
||||
const unique-ref1 <- \int..new: 5
|
||||
let unique-ref1 <- \int..new: 5
|
||||
var unique-ref2 <- \array..of: 1 2 3
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,6 @@ namespace var \array {
|
|||
decl something : -> \unit
|
||||
}
|
||||
|
||||
namespace const \array {
|
||||
namespace let \array {
|
||||
decl something : -> \unit
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
test all.dev.syntax.testing {
|
||||
const a = 31
|
||||
let a = 31
|
||||
; do-something: a
|
||||
}
|
||||
|
||||
exec app.exe {
|
||||
const b = true
|
||||
const c = false
|
||||
let b = true
|
||||
let c = false
|
||||
; do-something-different: b b c
|
||||
}
|
||||
|
||||
example func.basic-example {
|
||||
; func: a b c // func executed
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,189 +0,0 @@
|
|||
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
|
||||
|
||||
//
|
||||
|
|
@ -7,32 +7,23 @@ basic \unit[#str #copy]
|
|||
|
||||
//
|
||||
|
||||
namespace io {
|
||||
decl print : \string -> \unit
|
||||
decl scan : -> \string
|
||||
}
|
||||
|
||||
decl random : -> \int // TODO
|
||||
|
||||
decl error : \string -> \unit
|
||||
|
||||
//
|
||||
|
||||
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 =
|
||||
|
|
@ -179,15 +170,6 @@ typeclass \enum =
|
|||
|
||||
//
|
||||
|
||||
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
|
||||
|
|
@ -231,7 +213,7 @@ def ( -- ) : begin end = {
|
|||
decl scan-int : -> \int
|
||||
def scan-int = \int..read: (\io..scan:)
|
||||
|
||||
decl print-int : \int -> \unit
|
||||
decl print-int : ::x \int -> \unit
|
||||
def print-int : x = \io..print: (x..show:)
|
||||
|
||||
decl scan-anything 'a[#read] : -> 'a
|
||||
|
|
@ -277,6 +259,11 @@ def scan-three = & \io..scan: & \io..scan: & \io..scan:
|
|||
|
||||
exec main {
|
||||
var n = scan-anything:[int]
|
||||
|
||||
; print-anything:[bool] (n < 2) // TODO: fix exception (arguments not present ??)
|
||||
|
||||
if n > 1 then print-anything:[string] "aaa" // not work ??
|
||||
|
||||
var x = (for _ in 0--n do scan-int:) // $array[int] & 0
|
||||
|
||||
var k? = if n < 2 then n * 2 +. 3 in
|
||||
|
|
@ -285,6 +272,8 @@ exec main {
|
|||
|
||||
; print-anything:[int] n
|
||||
|
||||
; print-int: ::x 123
|
||||
|
||||
var & a & b & c = scan-three-t:
|
||||
; \io..print: b
|
||||
var & d & e & f = scan-three:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
decl test-tuples : -> \unit
|
||||
def test-tuples = {
|
||||
var tuple1 = & ''a & 2 & "hello"
|
||||
const & t1 & t2 & t3 = f: x
|
||||
let & t1 & t2 & t3 = f: x
|
||||
|
||||
; tuple1`0 = ''b
|
||||
|
||||
|
|
|
|||
|
|
@ -13,3 +13,4 @@ def test-variants = {
|
|||
| "hello" -> "nothing"
|
||||
| 11 -> "nothing"
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue