gettc 1.9 → 1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65b3906b3f1f4b5bda986b7134f8cb8e5ca12206
4
- data.tar.gz: 92ae49546cf3188d71ec91ace589a7f854ffa38c
3
+ metadata.gz: be356b29f63fbb870ee7ffca99f1bcc3d0bcd52e
4
+ data.tar.gz: 47c2abcafbe216880d20d01a6d11c0731bda6c66
5
5
  SHA512:
6
- metadata.gz: af342b43c1c2638f604f11a65c8450808fabb7ea187a9807d7f1d5465607f2f5848324a2a23ed69a4b04118b6aa534cf0dac325f0c2b39ba440658dcefbafd77
7
- data.tar.gz: 72621691d2e5f6c22d7c3eeb9d951748d3463a15715e4135b8867c9e96850b3ae556a5dfd3e912868fe7b56b482385864f0427de9c4286eecc05b5a1519b47fd
6
+ metadata.gz: 2e057827784adaa9d9f2e8b55c299f0a3e67aa72945409d5540fd610a38168819c2e960af5f6a3d9c6065c9947e377b2e58fcd98419498cd686ae8bf2b5b2f4f
7
+ data.tar.gz: 7c2bcb3dd3f338056b204f2d4f3d0505b949b7f58616a1bb5adbec67c1c21a2ef9e290b090658f6494feb1a04356c555fcce82b6fcea7d38db5e496d1b2d433b
@@ -1,6 +1,6 @@
1
1
  module Gettc
2
2
  TypeError = Class.new StandardError
3
- class UnrecognizedType < TypeError
3
+ class UnsupportedType < TypeError
4
4
  attr_accessor :type
5
5
  def initialize type = nil, msg = "Not a valid TopCoder type"
6
6
  @type = type
@@ -49,7 +49,7 @@ module Gettc
49
49
  class TArray < Type
50
50
  attr_accessor :subtype
51
51
  def initialize subtype
52
- raise UnrecognizedType.new subtype unless subtype.is_a? Type
52
+ raise UnsupportedType.new subtype unless subtype.is_a? Type
53
53
  @subtype = subtype
54
54
  end
55
55
  def == ary
@@ -78,6 +78,6 @@ module Gettc
78
78
  when "String"
79
79
  return TString
80
80
  end
81
- raise UnrecognizedType.new str
81
+ raise UnsupportedType.new str
82
82
  end
83
83
  end
data/core/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gettc
2
- VERSION = "1.9"
2
+ VERSION = "1.10"
3
3
  end
@@ -7,7 +7,7 @@ class SignatureTest < Test::Unit::TestCase
7
7
  assert_raise CannotParseSignature do
8
8
  parse_signature "invalid_signature"
9
9
  end
10
- assert_raise UnrecognizedType do
10
+ assert_raise UnsupportedType do
11
11
  parse_signature "strange_type name"
12
12
  end
13
13
  assert_raise InvalidVariableName do
@@ -4,12 +4,12 @@ include Gettc
4
4
 
5
5
  class TypesTest < Test::Unit::TestCase
6
6
  def test_parse_type
7
- assert_raise UnrecognizedType do array = TArray.new 123 end
8
- assert_raise UnrecognizedType do type = parse_type "" end
9
- assert_raise UnrecognizedType do type = parse_type " " end
10
- assert_raise UnrecognizedType do type = parse_type "[]" end
11
- assert_raise UnrecognizedType do type = parse_type "vector" end
12
- assert_raise UnrecognizedType do type = parse_type "vector[]" end
7
+ assert_raise UnsupportedType do array = TArray.new 123 end
8
+ assert_raise UnsupportedType do type = parse_type "" end
9
+ assert_raise UnsupportedType do type = parse_type " " end
10
+ assert_raise UnsupportedType do type = parse_type "[]" end
11
+ assert_raise UnsupportedType do type = parse_type "vector" end
12
+ assert_raise UnsupportedType do type = parse_type "vector[]" end
13
13
  assert_equal TBoolean, parse_type("boolean")
14
14
  assert_equal TInt, parse_type("int")
15
15
  assert_equal TLong, parse_type("long")
data/dist/config.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  username: gettc
2
2
  password: algorithm
3
- version: 1.9
3
+ version: 1.10
@@ -0,0 +1,38 @@
1
+ require "gettc/types"
2
+
3
+ module Gettc
4
+ class Type
5
+ def dumb_javascript
6
+ if is_a? TArray then
7
+ return "[]"
8
+ end
9
+
10
+ case self
11
+ when TInt, TLong, TFloat, TDouble
12
+ return "0"
13
+ when TChar
14
+ return "'$'"
15
+ when TString
16
+ return '"$"'
17
+ when TBoolean
18
+ return "true"
19
+ end
20
+
21
+ return "null"
22
+ end
23
+ end
24
+ class JavascriptEngine
25
+ attr_reader :arglist, :input
26
+ def initialize func, vars
27
+ temp = vars.map do |var|
28
+ var.name
29
+ end
30
+ @arglist = temp.join ", "
31
+
32
+ temp = vars.map do |var|
33
+ "var " + var.name + ' = reader.next("' + var.type.to_s + '");'
34
+ end
35
+ @input = temp.join " reader.next();\n"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ var errors = require("./topcoder/errors");
2
+
3
+ module.exports = {
4
+ Reader : require("./topcoder/reader"),
5
+ Writer : require("./topcoder/writer"),
6
+ UnsupportedType : errors.UnsupportedType
7
+ };
@@ -0,0 +1,7 @@
1
+ exports.UnsupportedType = function(type) {
2
+ this.type = type;
3
+ this.toString = function() {
4
+ return "UnsupportedType: " +
5
+ type.toString() + " is not a valid TopCoder type";
6
+ };
7
+ }
@@ -0,0 +1,239 @@
1
+ var tc = require("./errors");
2
+
3
+ function ParseError(text, pos, info) {
4
+ if (pos < text.length && pos >= 0) {
5
+ text = text.substr(0, pos) + "|" +
6
+ text.substr(pos, 1) + "|" +
7
+ text.substr(pos + 1);
8
+ }
9
+ this.message = "<" + text + ">";
10
+ if (typeof(info) === "string") {
11
+ this.message += " (" + info + ")";
12
+ }
13
+
14
+ this.toString = function() {
15
+ return "ParseError: " + this.message;
16
+ };
17
+ }
18
+
19
+
20
+ function Reader(text) {
21
+ this.text = text;
22
+ this.pos = 0;
23
+ this.len = text.length;
24
+ }
25
+
26
+ Reader.prototype.next = function(type) {
27
+ if (typeof(type) === "undefined") {
28
+ spaces.call(this);
29
+ expect.call(this, ',');
30
+ return ;
31
+ }
32
+ if (type.substr(type.length - 2, 2) === "[]") {
33
+ return nextArray.call(this, type.substr(0, type.length - 2));
34
+ }
35
+ switch (type) {
36
+ case "boolean":
37
+ return nextBoolean.call(this);
38
+ case "int":
39
+ return nextInt32.call(this);
40
+ case "long":
41
+ return nextInt64.call(this);
42
+ case "float":
43
+ case "double":
44
+ return nextFloat.call(this);
45
+ case "char":
46
+ return nextChar.call(this);
47
+ case "String":
48
+ return nextString.call(this);
49
+ default:
50
+ throw new tc.UnsupportedType(type);
51
+ }
52
+ };
53
+
54
+
55
+ function isWhiteSpace() {
56
+ var c = this.text.charAt(this.pos);
57
+ return c === ' ' || c === '\t' || c === '\n';
58
+ }
59
+
60
+ function isDigit(c) {
61
+ var c = this.text.charAt(this.pos);
62
+ return c >= '0' && c <= '9';
63
+ }
64
+
65
+
66
+ function raiseHere(message) {
67
+ throw new ParseError(this.text, this.pos, message);
68
+ }
69
+
70
+ function checkPos() {
71
+ if (this.pos >= this.len) {
72
+ raiseHere.call(this, "unexpected end of input");
73
+ }
74
+ }
75
+
76
+
77
+ function token() {
78
+ checkPos.call(this);
79
+ return this.text.charAt(this.pos);
80
+ }
81
+
82
+ function spaces() {
83
+ while (this.pos < this.len && isWhiteSpace.call(this)) {
84
+ this.pos += 1;
85
+ }
86
+ }
87
+
88
+ function expect(character) {
89
+ if (token.call(this) === character) {
90
+ this.pos += 1;
91
+ } else {
92
+ raiseHere(this, "expecting <" + character + ">");
93
+ }
94
+ }
95
+
96
+
97
+ function nextBoolean() {
98
+ spaces.call(this);
99
+ if (this.text.substr(this.pos, 4).toUpperCase() === "TRUE") {
100
+ this.pos += 4;
101
+ return true;
102
+ } else if (this.text.substr(this.pos, 5).toUpperCase() === "FALSE") {
103
+ this.pos += 5;
104
+ return false;
105
+ }
106
+ raiseHere.call(this, "expecting either true or false");
107
+ }
108
+
109
+
110
+ function nextDigits() {
111
+ checkPos.call(this);
112
+ if (!isDigit.call(this)) {
113
+ raiseHere.call(this, "expecting a digit");
114
+ }
115
+ var start = this.pos;
116
+ while (true) {
117
+ this.pos += 1;
118
+ if (this.pos === this.len || !isDigit.call(this)) {
119
+ break;
120
+ }
121
+ }
122
+ return this.text.substr(start, this.pos - start);
123
+ }
124
+
125
+
126
+ function nextNaturalNumber(parser) {
127
+ spaces.call(this);
128
+ if (token.call(this) === "-") {
129
+ this.pos += 1;
130
+ return -parser(nextDigits.call(this));
131
+ }
132
+ return parser(nextDigits.call(this));
133
+ }
134
+
135
+ function nextInt32() {
136
+ return nextNaturalNumber.call(this, function (str) {
137
+ return parseInt(str, 10);
138
+ });
139
+ }
140
+
141
+ function nextInt64() {
142
+ var Int64 = require("long");
143
+ return nextNaturalNumber.call(this, function (str) {
144
+ return Int64.fromString(str);
145
+ });
146
+ }
147
+
148
+
149
+ function nextPositiveFloat() {
150
+ var str = nextDigits.call(this);
151
+ if (this.pos < this.len) {
152
+ if (this.text.charAt(this.pos) === '.') {
153
+ this.pos += 1;
154
+ str += "." + nextDigits.call(this);
155
+ }
156
+ }
157
+ return parseFloat(str);
158
+ }
159
+
160
+ function nextFloat() {
161
+ spaces.call(this);
162
+ if (token.call(this) === '-') {
163
+ this.pos += 1;
164
+ return -nextPositiveFloat.call(this);
165
+ }
166
+ return nextPositiveFloat.call(this);
167
+ }
168
+
169
+
170
+ function nextChar() {
171
+ spaces.call(this);
172
+ var c = token.call(this);
173
+ if (c === '\'') {
174
+ this.pos += 1;
175
+ var ret = token.call(this);
176
+ this.pos += 1;
177
+ expect.call(this, '\'');
178
+ return ret;
179
+ }
180
+ this.pos += 1;
181
+ return c;
182
+ }
183
+
184
+
185
+ function nextString() {
186
+ spaces.call(this);
187
+ expect.call(this, '"');
188
+ var start = this.pos;
189
+ while (true) {
190
+ if (this.pos >= this.len) {
191
+ raiseHere.call(this, "expecting a closing quote");
192
+ }
193
+ if (token.call(this) === '"') {
194
+ this.pos += 1;
195
+ var saved = this.pos;
196
+ spaces.call(this);
197
+ if (this.pos === this.len ||
198
+ this.text.charAt(this.pos) === ',' ||
199
+ this.text.charAt(this.pos) === ']') {
200
+ this.pos = saved;
201
+ return this.text.substr(start, this.pos - 1 - start);
202
+ }
203
+ } else {
204
+ this.pos += 1;
205
+ }
206
+ }
207
+ }
208
+
209
+
210
+ function nextElems(elemType, arr) {
211
+ spaces.call(this);
212
+ var c = token.call(this);
213
+ if (c === ']') {
214
+ this.pos += 1;
215
+ return ;
216
+ } else if (c === ',') {
217
+ this.pos += 1;
218
+ arr.push(this.next(elemType));
219
+ nextElems.call(this, elemType, arr);
220
+ return ;
221
+ }
222
+ raiseHere("expecting either <,> or <]>");
223
+ }
224
+
225
+ function nextArray(elemType) {
226
+ var arr = [];
227
+ spaces.call(this);
228
+ expect.call(this, '[');
229
+ spaces.call(this);
230
+ if (token.call(this) === ']') {
231
+ this.pos += 1;
232
+ return arr;
233
+ }
234
+ arr.push(this.next(elemType));
235
+ nextElems.call(this, elemType, arr);
236
+ return arr;
237
+ }
238
+
239
+ module.exports = Reader;
@@ -0,0 +1,46 @@
1
+ var tc = require("./errors");
2
+
3
+ function Writer() {
4
+ this.text = "";
5
+ }
6
+
7
+ Writer.prototype.next = function(value, type) {
8
+ if (typeof(value) === "undefined") {
9
+ this.text += ", "
10
+ return this;
11
+ }
12
+ if (type.substr(type.length - 2, 2) === "[]") {
13
+ var elemType = type.substr(0, type.length - 2);
14
+ this.text += '[';
15
+ for (var i = 0; i < value.length; ++i) {
16
+ this.next(value[i], elemType);
17
+ if (i < value.length - 1) {
18
+ this.next();
19
+ }
20
+ }
21
+ this.text += ']';
22
+ return this;
23
+ }
24
+ switch (type) {
25
+ case "boolean":
26
+ case "int":
27
+ case "long":
28
+ case "float":
29
+ case "double":
30
+ this.text += value.toString();
31
+ return this;
32
+ case "char":
33
+ this.text += "'" + value + "'";
34
+ return this;
35
+ case "String":
36
+ this.text += '"' + value + '"';
37
+ return this;
38
+ }
39
+ throw new tc.UnsupportedType(type);
40
+ };
41
+
42
+ Writer.prototype.toString = function() {
43
+ return this.text;
44
+ };
45
+
46
+ module.exports = Writer;
@@ -3,7 +3,7 @@ require "gettc/types"
3
3
  module Gettc
4
4
  class Type
5
5
  def dumb_python
6
- if self.is_a? TArray then
6
+ if is_a? TArray then
7
7
  return "[]"
8
8
  end
9
9
 
@@ -1,3 +1,3 @@
1
- from topcoder.errors import UnsupportedTypeError
2
- from topcoder.reader import ReadError, Reader
3
- from topcoder.writer import write
1
+ from topcoder.errors import UnsupportedType
2
+ from topcoder.reader import ParseError, Reader
3
+ from topcoder.writer import write
@@ -1,4 +1,4 @@
1
- class UnsupportedTypeError(Exception):
1
+ class UnsupportedType(Exception):
2
2
  def __init__(self, type):
3
3
  message = type + " is not a valid TopCoder type"
4
4
  Exception.__init__(self, message)
@@ -1,6 +1,6 @@
1
- from topcoder.errors import UnsupportedTypeError
1
+ from topcoder.errors import UnsupportedType
2
2
 
3
- class ReadError(Exception):
3
+ class ParseError(Exception):
4
4
  def __init__(self, text, pos, info = ""):
5
5
  if (pos < len(text)) and (pos >= 0):
6
6
  text = text[:pos] + "|" + text[pos] + "|" + text[(pos + 1):]
@@ -41,10 +41,10 @@ class ReaderInternal(object):
41
41
  if type.endswith("[]"):
42
42
  return self.next_array(type[:-2])
43
43
 
44
- raise UnsupportedTypeError(type)
44
+ raise UnsupportedType(type)
45
45
 
46
46
  def raise_here(self, message):
47
- raise ReadError(self.text, self.pos, message)
47
+ raise ParseError(self.text, self.pos, message)
48
48
 
49
49
  def check_pos(self):
50
50
  if self.pos >= self.len:
@@ -1,4 +1,4 @@
1
- from topcoder.errors import UnsupportedTypeError
1
+ from topcoder.errors import UnsupportedType
2
2
 
3
3
  def write(value, type):
4
4
  if type in ["int", "long", "float", "double", "boolean"]:
@@ -10,4 +10,4 @@ def write(value, type):
10
10
  elif type.endswith("[]"):
11
11
  elem_type = type[:-2]
12
12
  return "[" + ", ".join(map(lambda elem: write(elem, elem_type), value)) + "]"
13
- raise UnsupportedTypeError(type)
13
+ raise UnsupportedType(type)
@@ -6,7 +6,7 @@ module TopCoder
6
6
  def initialize text, pos, info = ""
7
7
  message = text
8
8
  if pos < text.size && pos >= 0
9
- message = text[0..pos]
9
+ message = pos == 0 ? "" : text[0..(pos - 1)]
10
10
  message << "|"
11
11
  message << text[pos]
12
12
  message << "|"
@@ -53,7 +53,7 @@ module TopCoder
53
53
  end
54
54
  end
55
55
 
56
- raise UnrecognizedType.new typ
56
+ raise UnsupportedType.new typ
57
57
  end
58
58
 
59
59
  private
@@ -7,11 +7,23 @@ module TopCoder
7
7
  @text = ""
8
8
  end
9
9
  def next value = nil, typ = nil
10
- if value.nil? && typ.nil?
10
+ if value.nil? || typ.nil?
11
11
  @text << ", "
12
- return
12
+ return self
13
13
  end
14
14
 
15
+ if typ.is_a? TArray
16
+ @text << ?[
17
+ value.each_index do |i|
18
+ self.next value[i], typ.subtype
19
+ if i < value.size - 1
20
+ self.next
21
+ end
22
+ end
23
+ @text << ?]
24
+ return self
25
+ end
26
+
15
27
  case typ
16
28
  when TBoolean, TInt, TLong, TFloat, TDouble
17
29
  @text << value.to_s
@@ -20,19 +32,10 @@ module TopCoder
20
32
  when TString
21
33
  @text << ?" << value << ?"
22
34
  else
23
- if typ.is_a? TArray
24
- @text << ?[
25
- value.each_index do |i|
26
- self.next value[i], typ.subtype
27
- if i < value.size - 1
28
- self.next
29
- end
30
- end
31
- @text << ?]
32
- else
33
- raise UnrecognizedType.new typ
34
- end
35
+ raise UnsupportedType.new typ
35
36
  end
37
+
38
+ return self
36
39
  end
37
40
  def to_s
38
41
  return @text
@@ -87,6 +87,9 @@ class GettcRunner
87
87
  if ret != true
88
88
  @errors << n
89
89
  @log.debug "Error (cannot execute solver)\n"
90
+ elsif !File.exists? received
91
+ @errors << n
92
+ @log.debug "Error (solver did not produce an output file)\n"
90
93
  else
91
94
  system "#{@checker} #{expected} #{received}"
92
95
  ret = $?.exitstatus
@@ -0,0 +1,8 @@
1
+ include ../Makefile.common
2
+
3
+ OUTPUT_D = ../../build/javascript
4
+ SOLVER = ./<%= prob.name %>Solver.js
5
+
6
+ main: setup
7
+ @chmod +x $(SOLVER)
8
+
@@ -0,0 +1,7 @@
1
+ <%
2
+ engine = JavascriptEngine.new func, vars
3
+ %>module.exports = {
4
+ <%= func.name %> : function(<%= engine.arglist %>) {
5
+ return <%= func.type.dumb_javascript %>;
6
+ }
7
+ }
@@ -0,0 +1,30 @@
1
+ #! /usr/bin/env node
2
+ <% engine = JavascriptEngine.new func, vars %>
3
+ var fs = require("fs");
4
+ var path = require("path");
5
+ var tc = null;
6
+
7
+ function init() {
8
+ var gettcHome = process.env.GETTC_HOME || path.join(process.env.HOME, ".gettc");
9
+ var includeDir = path.join(gettcHome, "include", "javascript");
10
+ tc = require(path.join(includeDir, "topcoder"));
11
+ }
12
+
13
+ function main() { try {
14
+ var input = fs.readFileSync(process.argv[2], { encoding: "ascii" });
15
+ var reader = new tc.Reader(input);
16
+ <%= engine.input.gsub(/^/, " " * 4) %>
17
+
18
+ var <%= prob.name %> = require("./<%= prob.name %>");
19
+ var result = <%= prob.name %>.<%= func.name %>(<%= engine.arglist %>);
20
+
21
+ var writer = new tc.Writer();
22
+ writer.next(result, "<%= func.type.to_s %>");
23
+ fs.writeFileSync(process.argv[3], writer.toString(), { encoding: "ascii" });
24
+ } catch (err) {
25
+ console.log(err.toString());
26
+ console.trace();
27
+ }}
28
+
29
+ init();
30
+ main();
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettc
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.9'
4
+ version: '1.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-13 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hpricot
@@ -76,6 +76,11 @@ files:
76
76
  - dist/include/haskell/engine.rb
77
77
  - dist/include/java/TopCoder.jar
78
78
  - dist/include/java/engine.rb
79
+ - dist/include/javascript/engine.rb
80
+ - dist/include/javascript/topcoder.js
81
+ - dist/include/javascript/topcoder/errors.js
82
+ - dist/include/javascript/topcoder/reader.js
83
+ - dist/include/javascript/topcoder/writer.js
79
84
  - dist/include/python/engine.rb
80
85
  - dist/include/python/topcoder/__init__.py
81
86
  - dist/include/python/topcoder/__init__.pyc
@@ -110,6 +115,9 @@ files:
110
115
  - dist/template/solve/java/build.xml
111
116
  - dist/template/solve/java/{name}.java
112
117
  - dist/template/solve/java/{name}Solver.java
118
+ - dist/template/solve/javascript/Makefile
119
+ - dist/template/solve/javascript/{name}.js
120
+ - dist/template/solve/javascript/{name}Solver.js
113
121
  - dist/template/solve/python/Makefile
114
122
  - dist/template/solve/python/{name}.py
115
123
  - dist/template/solve/python/{name}Solver.py