diff options
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | src/parse.y | 1 | ||||
| -rw-r--r-- | src/pool.c | 14 | ||||
| -rw-r--r-- | src/pool.h | 19 | ||||
| -rw-r--r-- | test/t/hash.rb | 227 | ||||
| -rw-r--r-- | test/t/range.rb | 65 | ||||
| -rw-r--r-- | test/t/string.rb | 322 | ||||
| -rw-r--r-- | test/t/symbol.rb | 23 | ||||
| -rw-r--r-- | tools/mirb/mirb.c | 79 |
9 files changed, 681 insertions, 70 deletions
@@ -3,3 +3,4 @@ Original Authors "mruby developers" are: FUKUOKA CSK CORPORATION Kyushu Institute of Technology Network Applied Communication Laboratory, Inc. + Daniel Bovensiepen diff --git a/src/parse.y b/src/parse.y index 2b2dd6851..4b1f02106 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4634,6 +4634,7 @@ mrb_parser_parse(parser_state *p) p->cmd_start = TRUE; p->in_def = p->in_single = FALSE; p->nerr = p->nwarn = 0; + p->sterm = 0; yyparse(p); tree = p->tree; diff --git a/src/pool.c b/src/pool.c index 649790b2d..a367a30a5 100644 --- a/src/pool.c +++ b/src/pool.c @@ -4,9 +4,21 @@ ** See Copyright Notice in mruby.h */ -#include "pool.h" +#include "mruby.h" +#include <stddef.h> #include <string.h> +struct mrb_pool { + mrb_state *mrb; + struct mrb_pool_page { + struct mrb_pool_page *next; + size_t offset; + size_t len; + void *last; + char page[1]; + } *pages; +}; + #undef TEST_POOL #ifdef TEST_POOL #include <stdio.h> diff --git a/src/pool.h b/src/pool.h deleted file mode 100644 index 4d53ccd37..000000000 --- a/src/pool.h +++ /dev/null @@ -1,19 +0,0 @@ -/* -** pool.h - memory pool -** -** See Copyright Notice in mruby.h -*/ - -#include "mruby.h" -#include <stddef.h> - -typedef struct mrb_pool { - mrb_state *mrb; - struct mrb_pool_page { - struct mrb_pool_page *next; - size_t offset; - size_t len; - void *last; - char page[1]; - } *pages; -} mrb_pool; diff --git a/test/t/hash.rb b/test/t/hash.rb new file mode 100644 index 000000000..020c2aa3a --- /dev/null +++ b/test/t/hash.rb @@ -0,0 +1,227 @@ +## +# Hash ISO Test + +assert('Hash', '15.2.13') do + Hash.class == Class +end + +assert('Hash#==', '15.2.13.4.1') do + ({ 'abc' => 'abc' } == { 'abc' => 'abc' }) and + not ({ 'abc' => 'abc' } == { 'cba' => 'cba' }) +end + +assert('Hash#[]', '15.2.13.4.2') do + a = { 'abc' => 'abc' } + + a['abc'] == 'abc' +end + +assert('Hash#[]=', '15.2.13.4.3') do + a = Hash.new + a['abc'] = 'abc' + + a['abc'] == 'abc' +end + +assert('Hash#clear', '15.2.13.4.4') do + a = { 'abc' => 'abc' } + a.clear + + a == { } +end + +assert('Hash#default', '15.2.13.4.5') do + a = Hash.new + b = Hash.new('abc') + c = Hash.new {|s,k| s[k] = k} + + a.default == nil and b.default == 'abc' and + c.default == nil and c.default('abc') == 'abc' +end + +assert('Hash#default=', '15.2.13.4.6') do + a = { 'abc' => 'abc' } + a.default = 'cba' + + a['abc'] == 'abc' and a['notexist'] == 'cba' +end + +assert('Hash#default_proc', '15.2.13.4.7') do + a = Hash.new + b = Hash.new {|s,k| s[k] = k} + + a.default_proc == nil and b.default_proc.class == Proc +end + +assert('Hash#delete', '15.2.13.4.8') do + a = { 'abc' => 'abc' } + b = { 'abc' => 'abc' } + b_tmp_1 = false + b_tmp_2 = false + + a.delete('abc') + b.delete('abc') do |k| + b_tmp_1 = true + end + b.delete('abc') do |k| + b_tmp_2 = true + end + + a.delete('cba') == nil and not a.has_key?('abc') and + not b_tmp_1 and b_tmp_2 +end + +assert('Hash#each', '15.2.13.4.9') do + a = { 'abc_key' => 'abc_value' } + key = nil + value = nil + + a.each do |k,v| + key = k + value = v + end + + key == 'abc_key' and value == 'abc_value' +end + +assert('Hash#each_key', '15.2.13.4.10') do + a = { 'abc_key' => 'abc_value' } + key = nil + + a.each_key do |k| + key = k + end + + key == 'abc_key' +end + +assert('Hash#each_value', '15.2.13.4.11') do + a = { 'abc_key' => 'abc_value' } + value = nil + + a.each_value do |v| + value = v + end + + value == 'abc_value' +end + +assert('Hash#empty?', '15.2.13.4.12') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + not a.empty? and b.empty? +end + +assert('Hash#has_key?', '15.2.13.4.13') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.has_key?('abc_key') and not b.has_key?('cba') +end + +assert('Hash#has_value?', '15.2.13.4.14') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.has_value?('abc_value') and not b.has_value?('cba') +end + +assert('Hash#include?', '15.2.13.4.15') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.include?('abc_key') and not b.include?('cba') +end + +assert('Hash#initialize copy', '15.2.13.4.17') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new.initialize_copy(a) + + b == { 'abc_key' => 'abc_value' } +end + +assert('Hash#key?', '15.2.13.4.18') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.key?('abc_key') and not b.key?('cba') +end + +assert('Hash#keys', '15.2.13.4.19') do + a = { 'abc_key' => 'abc_value' } + + a.keys == ['abc_key'] +end + +assert('Hash#length', '15.2.13.4.20') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.length == 1 and b.length == 0 +end + +assert('Hash#member?', '15.2.13.4.21') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.member?('abc_key') and not b.member?('cba') +end + +assert('Hash#merge', '15.2.13.4.22') do + a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } + b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' } + + result_1 = a.merge b + result_2 = a.merge(b) do |key, original, new| + original + end + + result_1 == {'abc_key' => 'abc_value', 'cba_key' => 'XXX', + 'xyz_key' => 'xyz_value' } and + result_2 == {'abc_key' => 'abc_value', 'cba_key' => 'cba_value', + 'xyz_key' => 'xyz_value' } +end + +assert('Hash#replace', '15.2.13.4.23') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new.replace(a) + + b == { 'abc_key' => 'abc_value' } +end + +assert('Hash#shift', '15.2.13.4.24') do + a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } + # TODO Broken ATM (Hash instead of Array + b = a.shift + + a == { 'abc_key' => 'abc_value' } +end + +assert('Hash#size', '15.2.13.4.25') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.size == 1 and b.size == 0 +end + +assert('Hash#store', '15.2.13.4.26') do + a = Hash.new + a.store('abc', 'abc') + + a['abc'] == 'abc' +end + +assert('Hash#value?', '15.2.13.4.27') do + a = { 'abc_key' => 'abc_value' } + b = Hash.new + + a.value?('abc_value') and not b.value?('cba') +end + +assert('Hash#values', '15.2.13.4.28') do + a = { 'abc_key' => 'abc_value' } + + a.values == ['abc_value'] +end + diff --git a/test/t/range.rb b/test/t/range.rb new file mode 100644 index 000000000..42677e72e --- /dev/null +++ b/test/t/range.rb @@ -0,0 +1,65 @@ +## +# Range ISO Test + +assert('Range', '15.2.14') do + Range.class == Class +end + +assert('Range#==', '15.2.14.4.1') do + (1..10) == (1..10) and not (1..10) == (1..100) +end + +assert('Range#===', '15.2.14.4.2') do + a = (1..10) + + a === 5 and not a === 20 +end + +assert('Range#begin', '15.2.14.4.3') do + (1..10).begin == 1 +end + +assert('Range#each', '15.2.14.4.4') do + a = (1..3) + b = 0 + a.each {|i| b += i} + b == 6 +end + +assert('Range#end', '15.2.14.4.5') do + (1..10).end == 10 +end + +assert('Range#exclude_end?', '15.2.14.4.6') do + (1...10).exclude_end? and not (1..10).exclude_end? +end + +assert('Range#first', '15.2.14.4.7') do + (1..10).first == 1 +end + +assert('Range#include', '15.2.14.4.8') do + a = (1..10) + + a.include?(5) and not a.include?(20) +end + +# TODO SEGFAULT ATM +#assert('Range#initialize', '15.2.14.4.9') do +# a = Range.new(1, 10, true) +# b = Range.new(1, 10, false) +# +# a == (1..10) and a.exclude_end? and b == (1..10) and +# not b.exclude_end? +#end + +assert('Range#last', '15.2.14.4.10') do + (1..10).last == 10 +end + +assert('Range#member?', '15.2.14.4.11') do + a = (1..10) + + a.member?(5) and not a.member?(20) +end + diff --git a/test/t/string.rb b/test/t/string.rb new file mode 100644 index 000000000..34a834f50 --- /dev/null +++ b/test/t/string.rb @@ -0,0 +1,322 @@ +## +# String ISO Test + +assert('String', '15.2.10') do + String.class == Class +end + +assert('String#*', '15.2.10.5.1') do + 'a' * 5 == 'aaaaa' +end + +assert('String#+', '15.2.10.5.2') do + 'a' + 'b' == 'ab' +end + +assert('String#<=>', '15.2.10.5.3') do + a = '' <=> '' + b = '' <=> 'not empty' + c = 'not empty' <=> '' + d = 'abc' <=> 'cba' + e = 'cba' <=> 'abc' + + a == 0 and b == -1 and c == 1 and + d == -1 and e == 1 +end + +assert('String#==', '15.2.10.5.4') do + 'abc' == 'abc' and not 'abc' == 'cba' +end + +# TODO: SEGFAULT ATM assert('String#=~', '15.2.10.5.5') + +assert('String#[]', '15.2.10.5.6') do + # length of args is 1 + a = 'abc'[0] + b = 'abc'[-1] + c = 'abc'[10] + d = 'abc'[-10] + + # length of args is 2 + a1 = 'abc'[0, -1] + b1 = 'abc'[10, 0] + c1 = 'abc'[-10, 0] + d1 = 'abc'[0, 0] + e1 = 'abc'[1, 2] + + # args is RegExp + # TODO SEGFAULT ATM + + # args is String + a3 = 'abc'['bc'] + b3 = 'abc'['XX'] + + a == 'a' and b == 'c' and c == nil and d == nil and + a1 == nil and b1 == nil and c1 == nil and d1 == '' and + e1 == 'bc' and + a3 == 'bc' and b3 == nil +end + +assert('String#capitalize', '15.2.10.5.7') do + a = 'abc' + a.capitalize + + a == 'abc' and 'abc'.capitalize == 'Abc' +end + +assert('String#capitalize!', '15.2.10.5.8') do + a = 'abc' + a.capitalize! + + a == 'Abc' +end + +assert('String#chomp', '15.2.10.5.9') do + a = 'abc'.chomp + b = ''.chomp + c = "abc\n".chomp + d = "abc\n\n".chomp + e = "abc\t".chomp("\t") + f = "abc\n" + + f.chomp + + a == 'abc' and b == '' and c == 'abc' and + d == "abc\n" and e == 'abc' and f == "abc\n" +end + +assert('String#chomp', '15.2.10.5.10') do + a = 'abc' + b = '' + c = "abc\n" + d = "abc\n\n" + e = "abc\t" + + a.chomp! + b.chomp! + c.chomp! + d.chomp! + e.chomp!("\t") + + a == 'abc' and b == '' and c == 'abc' and + d == "abc\n" and e == 'abc' +end + +assert('String#chop', '15.2.10.5.11') do + a = ''.chop + b = 'abc'.chop + c = 'abc' + + c.chop + + a == '' and b == 'ab' and c == 'abc' +end + +assert('String#chop!', '15.2.10.5.12') do + a = '' + b = 'abc' + + a.chop! + b.chop! + + a == '' and b == 'ab' +end + +assert('String#downcase', '15.2.10.5.13') do + a = 'ABC'.downcase + b = 'ABC' + + b.downcase + + a == 'abc' and b == 'ABC' +end + +assert('String#downcase!', '15.2.10.5.14') do + a = 'ABC' + + a.downcase! + + a == 'abc' +end + +assert('String#each_line', '15.2.10.5.15') do + a = "first line\nsecond line\nthird line" + list = ["first line\n", "second line\n", "third line"] + n_list = [] + + a.each_line do |line| + n_list << line + end + + list == n_list +end + +assert('String#empty?', '15.2.10.5.16') do + a = '' + b = 'not empty' + + a.empty? and not b.empty? +end + +assert('String#eql?', '15.2.10.5.17') do + 'abc'.eql?('abc') and not 'abc'.eql?('cba') +end + +# TODO ATM broken assert('String#gsub', '15.2.10.5.18') do + +# TODO ATM broken assert('String#gsub!', '15.2.10.5.19') do + +assert('String#hash', '15.2.10.5.20') do + a = 'abc' + + a.hash == 'abc'.hash +end + +assert('String#include?', '15.2.10.5.21') do + 'abc'.include?(97) and not 'abc'.include?(100) and + 'abc'.include?('a') and not 'abc'.include?('d') +end + +assert('String#index', '15.2.10.5.22') do + 'abc'.index('a') == 0 and 'abc'.index('d') == nil and + 'abcabc'.index('a', 1) == 3 +end + +assert('String#initialize', '15.2.10.5.23') do + a = '' + a.initialize('abc') + + a == 'abc' +end + +assert('String#initialize_copy', '15.2.10.5.24') do + a = '' + a.initialize_copy('abc') + + a == 'abc' +end + +assert('String#intern', '15.2.10.5.25') do + 'abc'.intern == :abc +end + +assert('String#length', '15.2.10.5.26') do + 'abc'.length == 3 +end + +# TODO Broken ATM assert('String#match', '15.2.10.5.27') do + +assert('String#replace', '15.2.10.5.28') do + a = '' + a.replace('abc') + + a == 'abc' +end + +assert('String#reverse', '15.2.10.5.29') do + a = 'abc' + a.reverse + + a == 'abc' and 'abc'.reverse == 'cba' +end + +assert('String#reverse!', '15.2.10.5.30') do + a = 'abc' + a.reverse! + + a == 'cba' and 'abc'.reverse! == 'cba' +end + +assert('String#rindex', '15.2.10.5.31') do + 'abc'.rindex('a') == 0 and 'abc'.rindex('d') == nil and + 'abcabc'.rindex('a', 1) == 0 and 'abcabc'.rindex('a', 4) == 3 +end + +# TODO Broken ATM assert('String#scan', '15.2.10.5.32') do + +assert('String#size', '15.2.10.5.33') do + 'abc'.size == 3 +end + +assert('String#slice', '15.2.10.5.34') do + # length of args is 1 + a = 'abc'.slice(0) + b = 'abc'.slice(-1) + c = 'abc'.slice(10) + d = 'abc'.slice(-10) + + # length of args is 2 + a1 = 'abc'.slice(0, -1) + b1 = 'abc'.slice(10, 0) + c1 = 'abc'.slice(-10, 0) + d1 = 'abc'.slice(0, 0) + e1 = 'abc'.slice(1, 2) + + # args is RegExp + # TODO SEGFAULT ATM + + # args is String + a3 = 'abc'.slice('bc') + b3 = 'abc'.slice('XX') + + a == 'a' and b == 'c' and c == nil and d == nil and + a1 == nil and b1 == nil and c1 == nil and d1 == '' and + e1 == 'bc' and + a3 == 'bc' and b3 == nil +end + +# TODO Broken ATM +assert('String#split', '15.2.10.5.35') do + # without RegExp behavior is actually unspecified + a = 'abc abc abc'.split + + a == ['abc', 'abc', 'abc'] +end + +# TODO ATM broken assert('String#sub', '15.2.10.5.36') do + +# TODO ATM broken assert('String#sub!', '15.2.10.5.37') do + +assert('String#to_i', '15.2.10.5.38') do + a = ''.to_i + b = '123456789'.to_i + c = 'a'.to_i(16) + d = '100'.to_i(2) + + a == 0 and b == 123456789 and c == 10 and d == 4 +end + +assert('String#to_f', '15.2.10.5.39') do + a = ''.to_f + b = '123456789'.to_f + c = '12345.6789'.to_f + + check_float(a, 0.0) and check_float(b, 123456789.0) and + check_float(c, 12345.6789) +end + +assert('String#to_s', '15.2.10.5.40') do + 'abc'.to_s == 'abc' +end + +assert('String#to_sym', '15.2.10.5.41') do + 'abc'.to_sym == :abc +end + +assert('String#upcase', '15.2.10.5.42') do + a = 'abc'.upcase + b = 'abc' + + b.upcase + + a == 'ABC' and b == 'abc' +end + +assert('String#upcase!', '15.2.10.5.43') do + a = 'abc' + + a.upcase! + + a == 'ABC' +end + diff --git a/test/t/symbol.rb b/test/t/symbol.rb new file mode 100644 index 000000000..325c8d990 --- /dev/null +++ b/test/t/symbol.rb @@ -0,0 +1,23 @@ +## +# Symbol ISO Test + +assert('Symbol', '15.2.11') do + Symbol.class == Class +end + +assert('Symbol#===', '15.2.11.3.1') do + :abc === :abc and not :abc === :cba +end + +assert('Symbol#id2name', '15.2.11.3.2') do + :abc.id2name == 'abc' +end + +assert('Symbol#to_s', '15.2.11.3.3') do + :abc.to_s == 'abc' +end + +assert('Symbol#to_sym', '15.2.11.3.4') do + :abc.to_sym == :abc +end + diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index f09417ed0..a5285df0f 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -20,6 +20,31 @@ is_code_block_open(struct mrb_parser_state *parser) { int code_block_open = FALSE; + /* check for unterminated string */ + if (parser->sterm) return TRUE; + + /* check if parser error are available */ + if (0 < parser->nerr) { + const char *unexpected_end = "syntax error, unexpected $end"; + const char *message = parser->error_buffer[0].message; + + /* a parser error occur, we have to check if */ + /* we need to read one more line or if there is */ + /* a different issue which we have to show to */ + /* the user */ + + if (strncmp(message, unexpected_end, strlen(unexpected_end)) == 0) { + code_block_open = TRUE; + } + else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) { + code_block_open = TRUE; + } + else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) { + code_block_open = TRUE; + } + return code_block_open; + } + switch (parser->lstate) { /* all states which need more code */ @@ -27,7 +52,6 @@ is_code_block_open(struct mrb_parser_state *parser) case EXPR_BEG: /* an expression was just started, */ /* we can't end it like this */ - if (parser->nerr == 0) return FALSE; code_block_open = TRUE; break; case EXPR_DOT: @@ -80,52 +104,6 @@ is_code_block_open(struct mrb_parser_state *parser) break; } - if (!code_block_open) { - /* based on the last parser state the code */ - /* block seems to be closed */ - - /* now check if parser error are available */ - if (0 < parser->nerr) { - /* a parser error occur, we have to check if */ - /* we need to read one more line or if there is */ - /* a different issue which we have to show to */ - /* the user */ - - if (strcmp(parser->error_buffer[0].message, - "syntax error, unexpected $end, expecting ';' or '\\n'") == 0) { - code_block_open = TRUE; - } - else if (strcmp(parser->error_buffer[0].message, - "syntax error, unexpected $end, expecting keyword_end") == 0) { - code_block_open = TRUE; - } - else if (strcmp(parser->error_buffer[0].message, - "syntax error, unexpected $end, expecting '<' or ';' or '\\n'") == 0) { - code_block_open = TRUE; - } - else if (strcmp(parser->error_buffer[0].message, - "syntax error, unexpected keyword_end") == 0) { - code_block_open = TRUE; - } - else if (strcmp(parser->error_buffer[0].message, - "syntax error, unexpected $end, expecting keyword_then or ';' or '\\n'") == 0) { - code_block_open = TRUE; - } - else if (strcmp(parser->error_buffer[0].message, - "syntax error, unexpected tREGEXP_BEG") == 0) { - code_block_open = TRUE; - } - else if (strcmp(parser->error_buffer[0].message, - "unterminated string meets end of file") == 0) { - code_block_open = TRUE; - } - } - } - else { - /* last parser state suggest that this code */ - /* block is open, WE NEED MORE CODE!! */ - } - return code_block_open; } @@ -184,7 +162,7 @@ main(void) last_code_line[char_index] = '\0'; - if (strcmp(last_code_line, "exit") == 0) { + if (strcmp(last_code_line, "quit") == 0) { if (code_block_open) { /* cancel the current block and reset */ code_block_open = FALSE; @@ -199,18 +177,19 @@ main(void) } else { if (code_block_open) { + strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } else { memset(ruby_code, 0, sizeof(*ruby_code)); strcat(ruby_code, last_code_line); } - strcat(ruby_code, "\n"); /* parse code */ parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); parser->capture_errors = 1; + parser->lineno = 1; mrb_parser_parse(parser); code_block_open = is_code_block_open(parser); @@ -220,7 +199,7 @@ main(void) else { if (0 < parser->nerr) { /* syntax error */ - printf("%s\n", parser->error_buffer[0].message); + printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); } else { /* generate bytecode */ |
