~mna/snow

57cc873b77648d990ca726bcbde9d6b4d8cbd256 — Martin Angers 1 year, 8 months ago 4dd1348
pkg/{parser,semantic,codegen}: more test cases
A doc/rationales/generics_and_traits.md => doc/rationales/generics_and_traits.md +5 -0
@@ 0,0 1,5 @@
# Generics and Traits

See https://doc.rust-lang.org/book/ch19-03-advanced-traits.html for the role
of Rust's trait associated type (like Swift's protocol associated type) vs
just a generic trait.

D exp/syntax/struct_tuple.snow => exp/syntax/struct_tuple.snow +0 -11
@@ 1,11 0,0 @@
# example of struct and tuple type declarations, type literals and value literals

struct Person {
  var name: string
  let dob: date
}

tuple HttpResult {int, string}

let x: struct{
}

A pkg/codegen/testdata/struct_multi_fields_per_decl.snow => pkg/codegen/testdata/struct_multi_fields_per_decl.snow +18 -0
@@ 0,0 1,18 @@
struct S [$T] {
  var p1: $T, p2: $T, count: int
}

fn main() {
  var s: S[string], i: S[int]

  s = S[string](p1: "a", p2: "b", count: 1)
  i = S[int](p1: 1, p2: 2, count: 2)
  println[S[string]](s)
  println[S[int]](i)
}

@extern(import: "fmt", symbol: "Println")
fn println[$T](v: $T)

#={a b 1}
#={1 2 2}

A pkg/codegen/testdata/struct_multi_fields_per_decl.snow.err => pkg/codegen/testdata/struct_multi_fields_per_decl.snow.err +0 -0
A pkg/codegen/testdata/struct_multi_fields_per_decl.snow.want => pkg/codegen/testdata/struct_multi_fields_per_decl.snow.want +70 -0
@@ 0,0 1,70 @@
package main

import "fmt"

type _اSا0 struct {
	_اp1    string
	_اp2    string
	_اcount int
}

func _اSا0اnew(
	s _اSا0,
	m map[string]bool,
) _اSا0 {
	var r _اSا0
	if m["p1"] {
		r._اp1 = s._اp1
	}
	if m["p2"] {
		r._اp2 = s._اp2
	}
	if m["count"] {
		r._اcount = s._اcount
	}
	return r
}

type _اSا1 struct {
	_اp1    int
	_اp2    int
	_اcount int
}

func _اSا1اnew(
	s _اSا1,
	m map[string]bool,
) _اSا1 {
	var r _اSا1
	if m["p1"] {
		r._اp1 = s._اp1
	}
	if m["p2"] {
		r._اp2 = s._اp2
	}
	if m["count"] {
		r._اcount = s._اcount
	}
	return r
}

func _اprintlnا0(
	v _اSا0,
) {
	fmt.Println(v)
}

func _اprintlnا1(
	v _اSا1,
) {
	fmt.Println(v)
}

func main() {
	var s _اSا0
	var i _اSا1
	s = _اSا0اnew(_اSا0{_اp1: "a", _اp2: "b", _اcount: 1}, map[string]bool{"p1": true, "p2": true, "count": true})
	i = _اSا1اnew(_اSا1{_اp1: 1, _اp2: 2, _اcount: 2}, map[string]bool{"p1": true, "p2": true, "count": true})
	_اprintlnا0(s)
	_اprintlnا1(i)
}

A pkg/codegen/testdata/tuple_as_generic.snow => pkg/codegen/testdata/tuple_as_generic.snow +9 -0
@@ 0,0 1,9 @@
@extern(import: "fmt", symbol: "Println") 
fn println[$T](v: $T)

fn main() {
  let t = (1, true, "ok")
  println[(int, bool, string)](t)
}

#={1 true ok}

A pkg/codegen/testdata/tuple_as_generic.snow.err => pkg/codegen/testdata/tuple_as_generic.snow.err +0 -0
A pkg/codegen/testdata/tuple_as_generic.snow.want => pkg/codegen/testdata/tuple_as_generic.snow.want +26 -0
@@ 0,0 1,26 @@
package main

import "fmt"

func _اprintlnا0(
	v struct {
		_ا0 int
		_ا1 bool
		_ا2 string
	},
) {
	fmt.Println(v)
}

func main() {
	var t struct {
		_ا0 int
		_ا1 bool
		_ا2 string
	} = struct {
		_ا0 int
		_ا1 bool
		_ا2 string
	}{1, true, "ok"}
	_اprintlnا0(t)
}

A pkg/codegen/testdata/tuple_as_struct_generic.snow => pkg/codegen/testdata/tuple_as_struct_generic.snow +13 -0
@@ 0,0 1,13 @@
struct S [$T] {
  let t: $T
}

fn main() {
  let s: S[(int, bool, string)] = S[(int, bool, string)] (t: (1, false, "tuple"))
  println(s.t.2)
}

@extern(import: "fmt", symbol: "Println")
fn println(v: string)

#=tuple

A pkg/codegen/testdata/tuple_as_struct_generic.snow.err => pkg/codegen/testdata/tuple_as_struct_generic.snow.err +0 -0
A pkg/codegen/testdata/tuple_as_struct_generic.snow.want => pkg/codegen/testdata/tuple_as_struct_generic.snow.want +37 -0
@@ 0,0 1,37 @@
package main

import "fmt"

type _اSا0 struct {
	_اt struct {
		_ا0 int
		_ا1 bool
		_ا2 string
	}
}

func _اSا0اnew(
	s _اSا0,
	m map[string]bool,
) _اSا0 {
	var r _اSا0
	if m["t"] {
		r._اt = s._اt
	}
	return r
}

func main() {
	var s _اSا0 = _اSا0اnew(_اSا0{_اt: struct {
		_ا0 int
		_ا1 bool
		_ا2 string
	}{1, false, "tuple"}}, map[string]bool{"t": true})
	_اprintln(s._اt._ا2)
}

func _اprintln(
	v string,
) {
	fmt.Println(v)
}

A pkg/parser/testdata/invalid_generic_type.snow => pkg/parser/testdata/invalid_generic_type.snow +7 -0
@@ 0,0 1,7 @@
fn choose[T, U] (v1: T, v2: U) -> T {
  return v1
}

struct S [T] {
  let t: T
}

A pkg/parser/testdata/invalid_generic_type.snow.err => pkg/parser/testdata/invalid_generic_type.snow.err +17 -0
@@ 0,0 1,17 @@
testdata/invalid_generic_type.snow:1:11: expected '$', found T
testdata/invalid_generic_type.snow:1:12: expected 'ident', found ','
testdata/invalid_generic_type.snow:1:14: expected ']', found U
testdata/invalid_generic_type.snow:1:15: expected '(', found ']'
testdata/invalid_generic_type.snow:1:17: expected 'ident', found '('
testdata/invalid_generic_type.snow:1:18: expected ':', found v1
testdata/invalid_generic_type.snow:1:20: expected type, found ':'
testdata/invalid_generic_type.snow:1:20: expected ')', found ':'
testdata/invalid_generic_type.snow:1:22: expected ';', found T
testdata/invalid_generic_type.snow:2:3: expected top-level declaration, found 'return'
testdata/invalid_generic_type.snow:5:11: expected '$', found T
testdata/invalid_generic_type.snow:5:12: expected 'ident', found ']'
testdata/invalid_generic_type.snow:5:14: expected ']', found '{'
testdata/invalid_generic_type.snow:6:3: expected '{', found 'let'
testdata/invalid_generic_type.snow:6:7: expected declaration, found t
testdata/invalid_generic_type.snow:7:3: expected '}', found 'eof'
testdata/invalid_generic_type.snow:7:3: expected ';', found 'eof'

A pkg/parser/testdata/invalid_generic_type.snow.want => pkg/parser/testdata/invalid_generic_type.snow.want +17 -0
@@ 0,0 1,17 @@
file [3, #0] [0:81]
  fn [0:20]
    ident [choose] [3:9]
    generic [9:14]
      param [10:11]
        ident [$] [10:11]
    sig [1->0] [14:20]
      param [16:19]
        ident [] [16:16]
        bad expr [19:19]
  bad stmt [40:53]
  struct [1] [53:81]
    ident [S] [60:61]
    generic [62:67]
      param [63:64]
        ident [$] [63:64]
    bad stmt [74:81]

A pkg/semantic/testdata/check/tuple_as_struct_generic.snow.err => pkg/semantic/testdata/check/tuple_as_struct_generic.snow.err +0 -0
A pkg/semantic/testdata/check/tuple_as_struct_generic.snow.want => pkg/semantic/testdata/check/tuple_as_struct_generic.snow.want +40 -0
@@ 0,0 1,40 @@
file testdata/tuple_as_struct_generic.snow [0, 2, 1]
  fn main [let: () -> void]
    block [2]
      let:= s [let: struct S[(int, bool, string)]]
        generic inst [1] [type: struct S[(int, bool, string)]]
          ident S [type: struct S]
          tuple type [3] [type: (int, bool, string)]
            ident int [type: int]
            ident bool [type: bool]
            ident string [type: string]
        call [t] [1] [value: struct S[(int, bool, string)]]
          generic inst [1] [type: struct S[(int, bool, string)]]
            ident S [type: struct S]
            tuple type [3] [type: (int, bool, string)]
              ident int [type: int]
              ident bool [type: bool]
              ident string [type: string]
          tuple value [3] [value: (int, bool, string)]
            int [1] [const: int]
            ident false [let: bool]
            string ["tuple"] [const: string]
      expr
        call [1] [value: void]
          ident println [let: (string) -> void]
          select [let: string]
            select [let: (int, bool, string)]
              ident s [let: struct S[(int, bool, string)]]
              ident t [let: (int, bool, string)]
            ident 2 [let: string]
  fn println [let: (string) -> void]
    @ [import, symbol] [2] [value: struct extern]
      ident extern [type: struct extern]
      string ["fmt"] [const: string]
      string ["Println"] [const: string]
    let: v [let: string]
      ident string [type: string]
  struct S [1, 0, 0, $1] [type: struct S]
    generic type $T [type: $T]
    let: t [let: $T]
      ident $T [type: $T]

A pkg/semantic/testdata/scopes/tuple_as_struct_generic.snow.err => pkg/semantic/testdata/scopes/tuple_as_struct_generic.snow.err +0 -0
A pkg/semantic/testdata/scopes/tuple_as_struct_generic.snow.want => pkg/semantic/testdata/scopes/tuple_as_struct_generic.snow.want +41 -0
@@ 0,0 1,41 @@
1 *semantic.Unit {
.  bool
.  extern
.  f32
.  f64
.  false
.  float
.  i16
.  i32
.  i64
.  i8
.  int
.  string
.  true
.  u16
.  u32
.  u64
.  u8
.  uint
.  void
.  2 *semantic.Struct {
.  .  import
.  .  pkg
.  .  symbol
.  }
.  3 *semantic.File {
.  .  S
.  .  main
.  .  println
.  .  4 *semantic.Struct {
.  .  .  $T
.  .  .  t
.  .  }
.  .  5 *semantic.Fn {
.  .  .  s
.  .  }
.  .  6 *semantic.Fn {
.  .  .  v
.  .  }
.  }
}

A pkg/semantic/testdata/static/tuple_as_struct_generic.snow.err => pkg/semantic/testdata/static/tuple_as_struct_generic.snow.err +0 -0
A pkg/semantic/testdata/static/tuple_as_struct_generic.snow.want => pkg/semantic/testdata/static/tuple_as_struct_generic.snow.want +8 -0
@@ 0,0 1,8 @@
testdata/tuple_as_struct_generic.snow:6:35: S
testdata/tuple_as_struct_generic.snow:6:38: int
testdata/tuple_as_struct_generic.snow:6:43: bool
testdata/tuple_as_struct_generic.snow:6:49: string
testdata/tuple_as_struct_generic.snow:6:66: false
testdata/tuple_as_struct_generic.snow:7:3: println
testdata/tuple_as_struct_generic.snow:7:11: s
testdata/tuple_as_struct_generic.snow:7:13: t

A pkg/semantic/testdata/tuple_as_struct_generic.snow => pkg/semantic/testdata/tuple_as_struct_generic.snow +12 -0
@@ 0,0 1,12 @@
struct S [$T] {
  let t: $T
}

fn main() {
  let s: S[(int, bool, string)] = S[(int, bool, string)] (t: (1, false, "tuple"))
  println(s.t.2)
}

@extern(import: "fmt", symbol: "Println")
fn println(v: string)


A pkg/semantic/testdata/types/tuple_as_struct_generic.snow.err => pkg/semantic/testdata/types/tuple_as_struct_generic.snow.err +0 -0
A pkg/semantic/testdata/types/tuple_as_struct_generic.snow.want => pkg/semantic/testdata/types/tuple_as_struct_generic.snow.want +40 -0
@@ 0,0 1,40 @@
file testdata/tuple_as_struct_generic.snow [0, 2, 1]
  fn main [let: () -> void]
    block [2]
      let:= s [let: struct S[(int, bool, string)]]
        generic inst [1] [type: struct S[(int, bool, string)]]
          ident S [type: struct S]
          tuple type [3] [type: (int, bool, string)]
            ident int [type: int]
            ident bool [type: bool]
            ident string [type: string]
        call [t] [1] [value: struct S[(int, bool, string)]]
          generic inst [1] [type: struct S[(int, bool, string)]]
            ident S [type: struct S]
            tuple type [3] [type: (int, bool, string)]
              ident int [type: int]
              ident bool [type: bool]
              ident string [type: string]
          tuple value [3] [value: (int, bool, string)]
            int [1] [const: int]
            ident false [let: bool]
            string ["tuple"] [const: string]
      expr
        call [1] [value: void]
          ident println [let: (string) -> void]
          select [let: string]
            select [let: (int, bool, string)]
              ident s [let: struct S[(int, bool, string)]]
              ident t [let: (int, bool, string)]
            ident 2 [let: string]
  fn println [let: (string) -> void]
    @ [import, symbol] [2] [value: struct extern]
      ident extern [type: struct extern]
      string ["fmt"] [const: string]
      string ["Println"] [const: string]
    let: v [let: string]
      ident string [type: string]
  struct S [1, 0, 0, $1] [type: struct S]
    generic type $T [type: $T]
    let: t [let: $T]
      ident $T [type: $T]