Skip to content

Commit

Permalink
Std: rename Display to Show; add Trace; println takes `String…
Browse files Browse the repository at this point in the history
…` param
  • Loading branch information
ivanjermakov committed Mar 18, 2024
1 parent eef6e23 commit 1768ccd
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 64 deletions.
10 changes: 8 additions & 2 deletions data/example.no
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ impl Area for Shape {
}
}

fn main() {
pub fn main() {
let shapes: List<Shape> = [
Shape::Rect(width: 4., height: 2.),
Shape::Circle(radius: 12.34),
]
println(shapes.iter().map(Area::area).collect<List<_>>())
println(
shapes
.iter()
.map(|s| { s.area() })
.collect<List<_>>()
.show()
)
}

17 changes: 7 additions & 10 deletions data/features.no
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import
use std::io::Display
use std::io::Show

// sum type
type Vec2(x: Float, y: Float)
Expand All @@ -12,8 +12,8 @@ impl Vec2 {
}

// impl
impl Display for Vec2 {
fn fmt(self): String {
impl Show for Vec2 {
fn show(self): String {
"(" + self.x + ", " + self.y + ")"
}
}
Expand Down Expand Up @@ -72,13 +72,10 @@ fn main() {
println("hello")

// method call
vec.fmt()
vec.show()

// alternative call syntax
Vec2::fmt(vec)

// method call syntax for ambiguous methods
//Vec2::Display::fmt(vec)
Vec2::show(vec)

// if
let d = if true { 4 } else { 2 + 6 }
Expand Down Expand Up @@ -132,6 +129,6 @@ fn main() {
let p = println

// method reference
// equivalent to |v: Vec2| v.fmt()
let r = Vec2::fmt
// equivalent to |v: Vec2| v.show()
let r = Vec2::show
}
4 changes: 2 additions & 2 deletions data/syntax.no
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// comment
use pkg
use std::io::Display
use std::io::Show
use std::iter::{ Iter, peekableIter::PeekableIter }
type Foo
type Bar<T>(v: T)
type V<T>{ A(), B(v: T) }
trait Baz<U> { fn a(self): Unit fn f<T>(self): T {} }
impl <T> Baz<Option<T>> for Bar<T> { fn f(self): T {} }
fn foo<T: std::io::Display + U, U>(b: T): Option<U> {
fn foo<T: std::io::Show + U, U>(b: T): Option<U> {
let a = [1, "str", 'c', true, Option::None(), Option::Some(value: 1), Option::Some(2)]
let Foo::Foo(x) = Foo::Foo(4)
let b = if a.next().isSome() { a } else { [] }
Expand Down
6 changes: 3 additions & 3 deletions src/scope/trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ describe('trait', () => {
expect(formatImplTypes(findSuperRelChains(vidFromString('std::unit::Unit'), ctx))).toEqual([])

expect(formatImplTypes(findSuperRelChains(vidFromString('std::string::String'), ctx))).toEqual([
['std::io::Display'],
['std::io::show::Show'],
['std::eq::Eq'],
['std::iter::Collector<std::string::String>']
])

expect(formatImplTypes(findSuperRelChains(vidFromString('std::list::List'), ctx))).toEqual([
['std::iter::Iterable<T>'],
['std::iter::Collector<T>'],
['std::io::Display'],
['std::io::Display']
['std::io::show::Show'],
['std::io::show::Show']
])

expect(formatImplTypes(findSuperRelChains(vidFromString('std::list::ListIter'), ctx))).toEqual([
Expand Down
2 changes: 1 addition & 1 deletion src/scope/trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const getImplImplRel = (instance: ImplDef, module: Module, ctx: Context): Instan
* Every chain corresponds to a single `impl for` where type is assignable to impl trait
* For all, chain.length > 0
* For example:
* - String -> [Display for String], [Eq for String], ...]
* - String -> [Show for String], [Eq for String], ...]
* - Iter -> [[Iterable for Iter], [PeekableAdapter for Iter], [MapAdapter for Iter], ...]
* - ListIter -> [[Iter for ListIter], [Iter for ListIter, Iterable for Iter], ...]
* TODO: detect cases where impl.forType is a generic, e.g. `impl <T: B> A for T`
Expand Down
2 changes: 1 addition & 1 deletion src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ fn main() {
it('fn named call', () => {
const code = `\
fn main() {
let foo = println(value: 5)
let foo = println(value: "str")
return unit
}`
const ctx = check(code)
Expand Down
2 changes: 1 addition & 1 deletion src/std/bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function notBool(a) {
* @param {Bool} a
* @returns {String}
*/
function fmtBool(a) {
function showBool(a) {
return String.String(a.value.toString())
}

Expand Down
10 changes: 4 additions & 6 deletions src/std/bool.no
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::Display

pub type Bool {
True,
False
Expand All @@ -19,9 +17,9 @@ impl Bool {
}
}

impl Display for Bool {
fn fmt(self): String {
fmtBool(self)
impl Show for Bool {
fn show(self): String {
showBool(self)
}
}

Expand All @@ -43,7 +41,7 @@ fn orBool(a: Bool, b: Bool): Bool

fn notBool(a: Bool): Bool

fn fmtBool(a: Bool): String
fn showBool(a: Bool): String

fn eqBool(a: Bool, b: Bool): Bool

Expand Down
2 changes: 1 addition & 1 deletion src/std/char.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Char.Char = value => ({
* @param {Char} a
* @returns {String}
*/
function fmtChar(a) {
function showChar(a) {
return String.String(a.value)
}

Expand Down
10 changes: 4 additions & 6 deletions src/std/char.no
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::io::Display

pub type Char

impl Display for Char {
fn fmt(self): String {
fmtChar(self)
impl Show for Char {
fn show(self): String {
showChar(self)
}
}

Expand All @@ -20,6 +18,6 @@ impl Copy for Char {
}
}

fn fmtChar(a: Char): String
fn showChar(a: Char): String

fn eqChar(a: Char, b: Char): Bool
2 changes: 1 addition & 1 deletion src/std/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ function eqFloat(a, b) {
* @param {Float} a
* @returns {String}
*/
function fmtFloat(a) {
function showFloat(a) {
return String.String(a.value.toString())
}
10 changes: 4 additions & 6 deletions src/std/float.no
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::Display

pub type Float

impl Num for Float {
Expand Down Expand Up @@ -38,9 +36,9 @@ impl Eq for Float {
}
}

impl Display for Float {
fn fmt(self): String {
fmtFloat(self)
impl Show for Float {
fn show(self): String {
showFloat(self)
}
}

Expand Down Expand Up @@ -68,5 +66,5 @@ fn expFloat(a: Float, b: Float): Float

fn eqFloat(a: Float, b: Float): Bool

fn fmtFloat(a: Float): String
fn showFloat(a: Float): String

2 changes: 1 addition & 1 deletion src/std/int.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ function cmpInt(a, b) {
* @param {Int} a
* @returns {String}
*/
function fmtInt(a) {
function showInt(a) {
return String.String(a.value.toString())
}
10 changes: 4 additions & 6 deletions src/std/int.no
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::Display

pub type Int

impl Int {
Expand Down Expand Up @@ -50,9 +48,9 @@ impl Ord for Int {
}
}

impl Display for Int {
fn fmt(self): String {
fmtInt(self)
impl Show for Int {
fn show(self): String {
showInt(self)
}
}

Expand Down Expand Up @@ -82,5 +80,5 @@ fn eqInt(a: Int, b: Int): Bool

fn cmpInt(a: Int, b: Int): Ordering

fn fmtInt(a: Int): String
fn showInt(a: Int): String

8 changes: 2 additions & 6 deletions src/std/io/mod.no
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
pub trait Display {
fn fmt(self): String
}

pub fn println(display: Display): Unit {
printlnStr(display.fmt())
pub fn println(string: String): Unit {
printlnStr(string)
}

fn printlnStr(string: String): Unit
4 changes: 4 additions & 0 deletions src/std/io/show.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub trait Show {
fn show(self): String
}

4 changes: 4 additions & 0 deletions src/std/io/trace.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub trait Trace {
fn trace(self): String
}

11 changes: 5 additions & 6 deletions src/std/list.no
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::io::Display
use std::iter::{ MapAdapter, IntersperseAdapter }

pub type List<T>
Expand Down Expand Up @@ -32,8 +31,8 @@ impl <T> Collector<T> for List<T> {
}
}

impl Display for List<String> {
fn fmt(self): String {
impl Show for List<String> {
fn show(self): String {
"[".concat(self.iter().intersperse(", ").collect<String>()).concat("]")
}
}
Expand All @@ -44,10 +43,10 @@ impl Copy for Int {
}
}

impl <T: Display> Display for List<T> {
fn fmt(self): String {
impl <T: Show> Show for List<T> {
fn show(self): String {
// TODO: method ref
self.iter().map(|d| { d.fmt() }).collect<List<String>>().fmt()
self.iter().map(|d| { d.show() }).collect<List<String>>().show()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/std/prelude.no
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use std::{
list::List,
never::Never,
panic::{ panic, todo },
io::println,
io::{ println, show::Show, trace::Trace },
option::Option::{ self, Some, None },
result::Result::{ self, Ok, Error },
}
6 changes: 2 additions & 4 deletions src/std/string.no
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::Display

pub type String

impl String {
Expand All @@ -9,8 +7,8 @@ impl String {
}
}

impl Display for String {
fn fmt(self): String {
impl Show for String {
fn show(self): String {
self
}
}
Expand Down

0 comments on commit 1768ccd

Please sign in to comment.