@fraczak/k 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/codes.coffee CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  are_different = (classes, representatives, name1, name2, codes) ->
3
+ # console.log {classes, representatives, name1, name2, codes}
3
4
  return false if name1 is name2
4
5
  code1 = codes[name1]
5
6
  code2 = codes[name2]
@@ -11,9 +12,11 @@ are_different = (classes, representatives, name1, name2, codes) ->
11
12
  do (arg = code[code.code]) ->
12
13
  Object.keys(arg).reduce (fields, label) ->
13
14
  fields[label] = representatives[arg[label]] ? arg[label]
15
+ fields
14
16
  , {}
15
17
  return true unless Object.keys(fields1).length is Object.keys(fields2).length
16
18
  for field,rep of fields1
19
+ # console.log {field,rep1:rep,rep2:fields2[field]}
17
20
  return true unless fields2[field] is rep
18
21
  when "vector"
19
22
  [arg1,arg2] = [code1, code2]. map ({vector}) ->
@@ -99,7 +102,7 @@ normalizeAll = (codes,representatives) ->
99
102
  normalized[name] = { code: code.code }
100
103
  normalized[name][code.code] = normalize code[code.code], representatives
101
104
  when "vector"
102
- normalized[name] = {code:"vector", vector: representatives[code.vector]}
105
+ normalized[name] = {code:"vector", vector: representatives[code.vector] ? code.vector}
103
106
 
104
107
  normalized
105
108
  , {}
package/codes.js CHANGED
@@ -5,6 +5,7 @@
5
5
  are_different = function(classes, representatives, name1, name2, codes) {
6
6
  var arg1, arg2, code1, code2, field, fields1, fields2, rep;
7
7
  if (name1 === name2) {
8
+ // console.log {classes, representatives, name1, name2, codes}
8
9
  return false;
9
10
  }
10
11
  code1 = codes[name1];
@@ -20,7 +21,8 @@
20
21
  return (function(arg) {
21
22
  return Object.keys(arg).reduce(function(fields, label) {
22
23
  var ref;
23
- return fields[label] = (ref = representatives[arg[label]]) != null ? ref : arg[label];
24
+ fields[label] = (ref = representatives[arg[label]]) != null ? ref : arg[label];
25
+ return fields;
24
26
  }, {});
25
27
  })(code[code.code]);
26
28
  });
@@ -30,6 +32,8 @@
30
32
  for (field in fields1) {
31
33
  rep = fields1[field];
32
34
  if (fields2[field] !== rep) {
35
+
36
+ // console.log {field,rep1:rep,rep2:fields2[field]}
33
37
  return true;
34
38
  }
35
39
  }
@@ -152,6 +156,7 @@
152
156
  return normalized = names.reduce(function(normalized, name) {
153
157
  if (name === representatives[name]) {
154
158
  (function(code) {
159
+ var ref;
155
160
  switch (code.code) {
156
161
  case "union":
157
162
  case "product":
@@ -162,7 +167,7 @@
162
167
  case "vector":
163
168
  return normalized[name] = {
164
169
  code: "vector",
165
- vector: representatives[code.vector]
170
+ vector: (ref = representatives[code.vector]) != null ? ref : code.vector
166
171
  };
167
172
  }
168
173
  })(codes[name]);
package/error.k ADDED
@@ -0,0 +1,8 @@
1
+
2
+ $ var = < int int, bool bool >;
3
+
4
+ <
5
+ $var "VAR",
6
+ _log!
7
+ $ < int i, bool j> " < int i, bool j>"
8
+ >
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "k-language for JSON-like data transformation",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/run.coffee CHANGED
@@ -48,7 +48,9 @@ codes =
48
48
  x is true or x is false
49
49
 
50
50
  verify = (code, value) ->
51
- # console.log {code,value}
51
+ # representatives = run.defs.representatives
52
+ # defCodes = JSON.stringify run.defs.codes
53
+ # console.log {code,value, representatives, defCodes}
52
54
  switch code.code
53
55
  when "vector"
54
56
  value.every (x) ->
package/run.js CHANGED
@@ -111,7 +111,9 @@
111
111
  };
112
112
 
113
113
  verify = function(code, value) {
114
- // console.log {code,value}
114
+ // representatives = run.defs.representatives
115
+ // defCodes = JSON.stringify run.defs.codes
116
+ // console.log {code,value, representatives, defCodes}
115
117
  switch (code.code) {
116
118
  case "vector":
117
119
  return value.every(function(x) {
package/test.coffee CHANGED
@@ -48,3 +48,17 @@ console.log ""
48
48
  console.log "k_fn(#{JSON.stringify(data)});"
49
49
  console.log "// RETURNS: #{JSON.stringify k_fn data}"
50
50
  console.log ""
51
+
52
+ k_expression = "$ < < [ int ] ints, [ bool ] bools > list, string None>"
53
+ k_fn = k.compile k_expression
54
+ console.log "var k_fn = k.compile('#{k_expression}');"
55
+ console.log ""
56
+
57
+ [
58
+ {None: "None"}
59
+ {list: {ints: []}}
60
+ {list: {ints: [1,2,3]}}
61
+ ].map (data) ->
62
+ console.log "k_fn(#{JSON.stringify(data)});"
63
+ console.log "// RETURNS: #{JSON.stringify k_fn data}"
64
+ console.log ""
package/test.js CHANGED
@@ -116,4 +116,34 @@
116
116
  return console.log("");
117
117
  });
118
118
 
119
+ k_expression = "$ < < [ int ] ints, [ bool ] bools > list, string None>";
120
+
121
+ k_fn = k.compile(k_expression);
122
+
123
+ console.log(`var k_fn = k.compile('${k_expression}');`);
124
+
125
+ console.log("");
126
+
127
+ [
128
+ {
129
+ None: "None"
130
+ },
131
+ {
132
+ list: {
133
+ ints: []
134
+ }
135
+ },
136
+ {
137
+ list: {
138
+ ints: [1,
139
+ 2,
140
+ 3]
141
+ }
142
+ }
143
+ ].map(function(data) {
144
+ console.log(`k_fn(${JSON.stringify(data)});`);
145
+ console.log(`// RETURNS: ${JSON.stringify(k_fn(data))}`);
146
+ return console.log("");
147
+ });
148
+
119
149
  }).call(this);
package/typed-fib.k CHANGED
@@ -3,14 +3,14 @@ fib2 =
3
3
  $ { int target, int x, int "fib(x)", int "fib(x-1)" }
4
4
  _log!
5
5
  <
6
- {[.target,.x] GT guard, () input}
7
- .input
8
- {.target target,
9
- .x inc x,
10
- [."fib(x)", ."fib(x-1)"] PLUS "fib(x)",
6
+ {[.target,.x]GT.0 target ,
7
+ .x inc x ,
8
+ [."fib(x)",."fib(x-1)"]PLUS "fib(x)" ,
11
9
  ."fib(x)" "fib(x-1)" }
12
10
  fib2,
13
- ."fib(x)"
11
+
12
+ {[.target,.x] EQ, ."fib(x)" result}
13
+ .result
14
14
  >
15
15
  $ int;
16
16