Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

print arrays nicely

  • Loading branch information...
commit d4bcee54489955c1e5882b57d39bb6c2198a6ce9 1 parent fd4843f
Josep M. Bach authored
2  compiler/examples/vectors.rb
... ... @@ -1,2 +1,2 @@
1 1 a = ['hello world',2,3]
2   -puts a[0]
  2 +puts a
11 examples/vectors.tvm
... ... @@ -1,10 +1,8 @@
1 1 _main
2   -:6:23
  2 +:4:18
3 3 3
4 4 2
5 5 "hello world
6   -0
7   -"[]
8 6 "puts
9 7 17 PUSH
10 8 0
@@ -19,13 +17,8 @@ _main
19 17 16 PUSHSELF
20 18 32 PUSHLOCAL
21 19 0
22   -17 PUSH
23   -3
24 20 128 SEND
25   -4
26   -1
27   -128 SEND
28   -5
  21 +3
29 22 1
30 23 20 PUSHNIL
31 24 144 RET
17 src/terror/value.c
@@ -70,7 +70,7 @@ __Value_print(VALUE o)
70 70 break;
71 71 }
72 72 case StringType: {
73   - printf("\"%s\"", VAL2STR(o));
  73 + printf("%s", VAL2STR(o));
74 74 break;
75 75 }
76 76 case TrueType: {
@@ -90,19 +90,28 @@ __Value_print(VALUE o)
90 90 break;
91 91 }
92 92 case VectorType: {
93   - printf("#<Vector %p>", o);
  93 + printf("[");
  94 + DArray *array = VAL2ARY(o);
  95 + int count = DArray_count(array);
  96 + for(int i=0; i < count; i++) {
  97 + Value_print((VALUE)DArray_at(array, i));
  98 + if(i+1 < count) {
  99 + printf(", ");
  100 + }
  101 + }
  102 + printf("]");
94 103 break;
95 104 }
96 105 case MapType: {
97 106 printf("{");
98   - Hashmap_traverse(o->table, Hashmap_print_cb);
  107 + Hashmap_traverse(VAL2HASH(o), Hashmap_print_cb);
99 108 printf("}");
100 109 break;
101 110 }
102 111 default: {
103 112 printf("#<Object %p ", o);
104 113 printf("{");
105   - Hashmap_traverse(o->table, Hashmap_print_cb);
  114 + Hashmap_traverse(VAL2HASH(o), Hashmap_print_cb);
106 115 printf("}>");
107 116 break;
108 117 }
2  src/terror/value.h
@@ -42,7 +42,7 @@ VALUE Vector_new(DArray *array);
42 42 #define VAL2ARY(o) ((DArray*)(o->data.as_data))
43 43
44 44 VALUE Map_new(DArray *array);
45   -#define VAL2HASH(o) ((Hashmap*)(o->table))
  45 +#define VAL2HASH(o) (o->table)
46 46
47 47 void Value_set(VALUE receiver, char *key, VALUE value);
48 48 VALUE Value_get(VALUE receiver, char *key);

0 comments on commit d4bcee5

Please sign in to comment.
Something went wrong with that request. Please try again.