summary refs log tree commit diff stats
path: root/tests/cpp/tvectorseq.nim
blob: 4d9ffc3d63dc84e085edeae0abaf0cfe88b24e4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
discard """
  targets: "cpp"
  output: '''(x: 1.0)
(x: 0.0)'''
  disabled: "true"
"""

# This cannot work yet because we omit type information for importcpp'ed types.
# Fixing this is not hard, but also requires fixing Urhonimo.

# bug #2536

{.emit: """/*TYPESECTION*/
struct Vector3 {
public:
  Vector3(): x(5) {}
  Vector3(float x_): x(x_) {}
  float x;
};
""".}

type Vector3 {.importcpp: "Vector3", nodecl} = object
  x: cfloat

proc constructVector3(a: cfloat): Vector3 {.importcpp: "Vector3(@)", nodecl}

# hack around another codegen issue: Generics are attached to where they came
# from:
proc `$!`(v:  seq[Vector3]): string = "(x: " & $v[0].x & ")"

proc vec3List*(): seq[Vector3] =
  let s = @[constructVector3(cfloat(1))]
  echo($!s)
  result = s
  echo($!result)

let f = vec3List()
#echo($!f)
span>put *result, data:offset, s ] def rewind in:address:stream:_elem -> in:address:stream:_elem [ local-scope load-ingredients *in <- put *in, index:offset, 0 ] def read in:address:stream:_elem -> result:_elem, empty?:boolean, in:address:stream:_elem [ local-scope load-ingredients empty? <- copy 0/false idx:number <- get *in, index:offset s:address:array:_elem <- get *in, data:offset len:number <- length *s at-end?:boolean <- greater-or-equal idx len { break-unless at-end? empty-result:address:_elem <- new _elem:type return *empty-result, 1/true } result <- index *s, idx idx <- add idx, 1 *in <- put *in, index:offset, idx ] def peek in:address:stream:_elem -> result:_elem, empty?:boolean [ local-scope load-ingredients empty?:boolean <- copy 0/false idx:number <- get *in, index:offset s:address:array:_elem <- get *in, data:offset len:number <- length *s at-end?:boolean <- greater-or-equal idx len { break-unless at-end? empty-result:address:_elem <- new _elem:type return *empty-result, 1/true } result <- index *s, idx ] def read-line in:address:stream:character -> result:address:array:character, in:address:stream:character [ local-scope load-ingredients idx:number <- get *in, index:offset s:text <- get *in, data:offset next-idx:number <- find-next s, 10/newline, idx result <- copy-range s, idx, next-idx idx <- add next-idx, 1 # skip newline # write back *in <- put *in, index:offset, idx ] def end-of-stream? in:address:stream:_elem -> result:boolean [ local-scope load-ingredients idx:number <- get *in, index:offset s:address:array:_elem <- get *in, data:offset len:number <- length *s result <- greater-or-equal idx, len ]