Skip to content

Commit

Permalink
define generalised string concatination operator
Browse files Browse the repository at this point in the history
  • Loading branch information
simvux committed Nov 28, 2024
1 parent 95c4ee5 commit 36fa636
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/hello-world/src/main.lm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std:io

fn main =
io:println ("Hello" <++> " " <++> "World!")
io:println ("Hello" <> " " <> "World!")
2 changes: 1 addition & 1 deletion examples/maybe-implementation/src/main.lm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ToString for Maybe a
fn show m as self -> string =
match m
| Nothing -> "Nothing"
| Just a -> "Just " <++> show a
| Just a -> "Just " <> show a

fn main =
Just 5
Expand Down
4 changes: 2 additions & 2 deletions examples/modules/src/main.lm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use other_dir [world] // using a directory will refer to the `lib.lm` file
use other_dir:file [Color [..], Direction [Right, Down]]

fn main =
do file:print (hello <++> world)
then file:print (other_file:hello <++> project:other_dir:world)
do file:print (hello <> world)
then file:print (other_file:hello <> project:other_dir:world)

fn allowed as (Color, Direction) = (Green, Down)

Expand Down
2 changes: 1 addition & 1 deletion examples/tuples/src/main.lm
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ fn main =
print_user user

fn print_user (name, age) as (string, int) -> () =
io:println (name <++> " " <++> show age)
io:println (name <> " " <> show age)
2 changes: 1 addition & 1 deletion luminapath/std/io/lib.lm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn print str as s -> () =
@[platform "linux"]
when s can ToString
pub fn println str as s -> () =
let {ptr, len} = (show str <++> "\n") . toByteVec in
let {ptr, len} = (str <> "\n") . toByteVec in
do libc:write stdout ptr (len as int)
then ()

Expand Down
6 changes: 3 additions & 3 deletions luminapath/std/list/lib.lm
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ pub fn split_at at list as uint, List a -> (List a, List a) =
when a can ToString
impl ToString for [a]
fn show list as [a] -> string =
"[" <++> show_ list
"[" <> show_ list
where
fn show_ list as [a] -> string =
match list
| [] -> "]"
| [last] -> show last <++> "]"
| [x : xs] -> show x <++> ", " <++> show_ xs
| [last] -> show last <> "]"
| [x : xs] -> show x <> ", " <> show_ xs


// TODO: Considder splitting equality out from Compare
Expand Down
6 changes: 3 additions & 3 deletions luminapath/std/list/slice.lm
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ pub fn to_list slice as Slice a -> List a =
when a can ToString
impl ToString for Slice a
fn show slice as self -> string =
"[" <++> show_ slice
"[" <> show_ slice
where
fn show_ slice as Slice a -> string =
match split slice
| Nothing -> "]"
| Just (x, xs) ->
if xs.len == 0
then show x <++> "]"
else show x <++> ", " <++> show_ xs
then show x <> "]"
else show x <> ", " <> show_ xs
4 changes: 2 additions & 2 deletions luminapath/std/list/vec.lm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std:list [Listable]
use std:list:slice [Slice]
use std:math [Compare [>=]]
use std:io [crash]
use std:string [<++>]
use std:string [<>]

// An immutable heap-allocated array
pub type Vec a {
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn unsafe_set i v {ptr, len} as uint, a, Vec a -> () =

pub fn from_range (start, end) f as (uint, uint), fn(uint -> a) -> Vec a =
if start > end then
crash ("error: from_range end larger than start " <++> show end <++> " > " <++> show start)
crash ("error: from_range end larger than start " <> end <> " > " <> start)
else
let count = (end - start) in
let vec = capacity count in
Expand Down
5 changes: 3 additions & 2 deletions luminapath/std/math/lib.lm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use non_zero [NonZero]
use std:string [<+>]
use std:io [crash]

when n can Num + Negate + Compare
Expand Down Expand Up @@ -59,13 +60,13 @@ pub trait Compare
when a can ToString
fn or_overflow_error char lhs rhs (n, overflown) as u8, a, a, (a, bool) -> a =
if overflown
then crash ("integer overflow " <++> show lhs <++> " " <+> char <++> " " <++> show rhs)
then crash ("integer overflow " <> lhs <> " " <+> char <> " " <> show rhs)
else n

when a can ToString
fn or_underflow_error char lhs rhs (n, underflown) as u8, a, a, (a, bool) -> a =
if underflown
then crash ("integer underflow " <++> show lhs <++> " " <+> char <++> " " <++> show rhs)
then crash ("integer underflow " <> lhs <> " " <+> char <> " " <> rhs)
else n

impl Num for u8
Expand Down
2 changes: 1 addition & 1 deletion luminapath/std/prelude/lib.lm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use std:math:i32
pub use std:math:i64
pub use std:maybe [Maybe [..]]
pub use std:result [Result [..]]
pub use std:string [string, ToString [show], <:>, <++>, <+>]
pub use std:string [string, ToString [show], <>]
pub use std:list [Listable [:], List, ++]
use std:string [Stringable]
pub use std:nothing [nothing]
Expand Down
2 changes: 1 addition & 1 deletion luminapath/std/result/lib.lm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ when e can ToString
pub fn or_crash msg r as string, Result a e -> a =
match r
| Ok v -> v
| Err err -> io:crash (msg <++> ": " <++> show err)
| Err err -> io:crash (msg <> ": " <> show err)

pub fn or_crash_ msg r as string, Result a e -> a =
match r
Expand Down
6 changes: 6 additions & 0 deletions luminapath/std/string/lib.lm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ pub fn to_c_str str as string -> *u8 =
pub trait ToString
fn show as self -> string

when
a can ToString
b can ToString
pub fn <> left right as a, b -> string =
show left <++> show right

pub fn <:> left right as u8, string -> string =
{ string | inner = left : right.inner }

Expand Down
2 changes: 2 additions & 0 deletions luminapath/std/string/num.lm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std:string [<:>, <+>]

impl ToString for i64
fn show n =
let str = show ((std:math:i64:abs n) as u64) in
Expand Down
14 changes: 9 additions & 5 deletions luminapath/std/tuple/lib.lm
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use std:string [<++>]

// TODO: using `<>` instead of `<++>` here causes bools to display as ints.
// That does not make any sense, and must be some sort of problem with our trait solver?
when
a can ToString
b can ToString
impl ToString for (a, b)
fn show (a, b) as (a, b) -> string =
"(" <++> show a <++> ", " <++> show b <++> ")"
"(" <++> show a <++> show ", " <++> show b <++> show ")"

when
a can ToString
b can ToString
c can ToString
impl ToString for (a, b, c)
fn show (a, b, c) as (a, b, c) -> string =
"(" <++> show a <++> ", " <++> show b <++> ", " <++> show c <++> ")"
"(" <++> show a <++> show ", " <++> show b <++> show ", " <++> show c <++> show ")"

when
a can ToString
Expand All @@ -20,7 +24,7 @@ when
d can ToString
impl ToString for (a, b, c, d)
fn show (a, b, c, d) as (a, b, c, d) -> string =
"(" <++> show a <++> ", " <++> show b <++> ", " <++> show c <++> ", " <++> show d <++> ")"
"(" <++> show a <++> show ", " <++> show b <++> show ", " <++> show c <++> show ", " <++> show d <++> show ")"

when
a can ToString
Expand All @@ -30,7 +34,7 @@ when
e can ToString
impl ToString for (a, b, c, d, e)
fn show (a, b, c, d, e) as (a, b, c, d, e) -> string =
"(" <++> show a <++> ", " <++> show b <++> ", " <++> show c <++> ", " <++> show d <++> ", " <++> show e <++> ")"
"(" <++> show a <++> show ", " <++> show b <++> show ", " <++> show c <++> show ", " <++> show d <++> show ", " <++> show e <++> show ")"

when
a can ToString
Expand All @@ -41,4 +45,4 @@ when
f can ToString
impl ToString for (a, b, c, d, e, f)
fn show (a, b, c, d, e, f) as (a, b, c, d, e, f) -> string =
"(" <++> show a <++> ", " <++> show b <++> ", " <++> show c <++> ", " <++> show d <++> ", " <++> show e <++> ", " <++> show f <++> ")"
"(" <++> show a <++> show ", " <++> show b <++> show ", " <++> show c <++> show ", " <++> show d <++> show ", " <++> show e <++> show ", " <++> show f <++> show ")"

0 comments on commit 36fa636

Please sign in to comment.