red 4.0.5 → 4.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,11 +61,15 @@ module Red
61
61
  args_array += [options[:block_string]] if options[:block_string]
62
62
  arguments = args_array.join(",")
63
63
  self << "%s.m$%s(%s)" % [receiver, function, arguments]
64
- @@red_methods |= [function_sexp] unless @@red_import
64
+ unless @@red_import
65
+ @@red_methods |= [function_sexp]
66
+ @@red_methods |= ([arguments_array_sexp.last[1].last.to_sym] rescue []) if function_sexp == :send && arguments_array_sexp.last.is_sexp?(:array)
67
+ end
65
68
  end
66
69
  end
67
70
 
68
71
  class ImplicitReceiver < Method # :nodoc:
72
+ # [:vcall]
69
73
  # [:fcall, :foo, (:array, {expression}, {expression}, ...)]
70
74
  def initialize(function_sexp, *arguments_array_sexp)
71
75
  options = arguments_array_sexp.pop
@@ -84,7 +88,8 @@ module Red
84
88
  when :block_given?
85
89
  self << "m$blockGivenBool(%s._block)" % @@red_block_arg
86
90
  else
87
- self << "(this.m$%s||m$%s).call(this,%s)" % [function, function, arguments]
91
+ arguments = ','+arguments unless arguments.empty?
92
+ self << "(this.m$%s||m$%s).call(this%s)" % [function, function, arguments]
88
93
  @@red_methods |= [function_sexp] unless @@red_import
89
94
  end
90
95
  end
@@ -55,7 +55,7 @@ module Red
55
55
  block = (block_sexp.is_sexp?(:block) ? block_sexp : [:block, block_sexp]).red!(:force_return => options[:force_return])
56
56
  rescue_body = "else{%s;}" % rescue_body_sexp.red!(:force_return => options[:force_return]) if rescue_body_sexp
57
57
 
58
- self << "%sif(Red.LoopError.isA(_e,%s)){_eRescued=true;%s;}%s" % [exception_variable, exception_types_array, block, rescue_body]
58
+ self << "%sif($e(_e,%s)){_eRescued=true;%s;}%s" % [exception_variable, exception_types_array, block, rescue_body]
59
59
  end
60
60
  end
61
61
 
@@ -81,7 +81,7 @@ module Red
81
81
  options = arguments_array_sexp.pop
82
82
  arguments = arguments_array_sexp.map {|arg| arg.red!(:as_argument => true) }.join(",")
83
83
  keyword = self.class.to_s.split('::').last.downcase
84
- self << "$%s(%s)" % [keyword, arguments]
84
+ self << "Red.LoopError._%s(%s)" % [keyword, arguments]
85
85
  end
86
86
 
87
87
  class Break < Keyword # :nodoc:
@@ -33,7 +33,7 @@ module Red
33
33
 
34
34
  scope = scope_sexp.red!(:as_class_eval => true)
35
35
 
36
- self << "\n\nRed.class('%s',%s,function(){ var _=%s.prototype;\n %s;\n})" % [namespaced_class.gsub("c$",""), superclass, namespaced_class, scope]
36
+ self << "\n\nRed._class('%s',%s,function(){ var _=%s.prototype;\n %s;\n})" % [namespaced_class.gsub("c$",""), superclass, namespaced_class, scope]
37
37
 
38
38
  old_namespace_stack.nil? ? @@namespace_stack.pop : @@namespace_stack = old_namespace_stack
39
39
  end
@@ -61,7 +61,7 @@ module Red
61
61
 
62
62
  scope = scope_sexp.red!(:as_class_eval => true)
63
63
 
64
- self << "\n\nRed.module('%s',function(){ var _=%s.prototype;\n %s;\n})" % [namespaced_module.gsub("c$",""), namespaced_module, scope]
64
+ self << "\n\nRed._module('%s',function(){ var _=%s.prototype;\n %s;\n})" % [namespaced_module.gsub("c$",""), namespaced_module, scope]
65
65
 
66
66
  @@namespace_stack.pop
67
67
  end
@@ -150,7 +150,7 @@ module Red
150
150
  (options = contents_sexp) && (contents_sexp = [:block]) if contents_sexp.is_a?(::Hash)
151
151
  variables = []
152
152
  contents_sexp.flatten.each_with_index do |x,i|
153
- variables.push(contents_sexp.flatten[i + 1]) if [:lasgn,:vcall].include?(x)
153
+ variables.push(contents_sexp.flatten[i + 1]) if x == :lasgn
154
154
  end
155
155
  variables -= (contents_sexp.delete(contents_sexp.assoc(:args)) || [])[1..-1] || [] # don't want to undefine the arguments in a method definition
156
156
  declare = "var %s" % variables.map {|x| "%s=$u" % x.red! }.uniq.join(",") unless variables.empty?
@@ -40,22 +40,29 @@ module Red
40
40
  end
41
41
 
42
42
  class Conjunction < LogicNode # :nodoc:
43
- # [:and, {expression}, {expression}]
44
- # [:or, {expression}, {expression}]
45
- def initialize(expression_a_sexp, expression_b_sexp, options)
46
- a = expression_a_sexp.red!(:as_argument => true)
47
- b = expression_b_sexp.red!(:as_argument => true)
48
- string = self.class::STRING
49
- self << string % [a,b]
50
- end
51
-
52
43
  class And < Conjunction # :nodoc:
44
+ # [:and, {expression}, {expression}]
53
45
  # FIX: nil && obj produces false instead of nil
54
- STRING = "(_a=$T(%s)?(_c=$T(_b=%s)?_b:_c):_a)"
46
+ def initialize(expression_a_sexp, expression_b_sexp, options)
47
+ a = expression_a_sexp.red!(:as_argument => true)
48
+ b = expression_b_sexp.red!(:as_argument => true)
49
+ d = @@red_boolean.succ!.dup
50
+ e = @@red_boolean.succ!.dup
51
+ f = @@red_boolean.succ!.dup
52
+ string = "($.%s=$T(%s)?($.%s=$T($.%s=%s)?$.%s:$.%s):$.%s)" % [d, '%s', f, e, '%s', e, f, d]
53
+ self << string % [a,b]
54
+ end
55
55
  end
56
56
 
57
57
  class Or < Conjunction # :nodoc:
58
- STRING = "($T(_a=%s)?_a:%s)"
58
+ # [:or, {expression}, {expression}]
59
+ def initialize(expression_a_sexp, expression_b_sexp, options)
60
+ a = expression_a_sexp.red!(:as_argument => true)
61
+ b = expression_b_sexp.red!(:as_argument => true)
62
+ c = @@red_boolean.succ!.dup
63
+ string = "($T($.%s=%s)?$.%s:%s)" % [c, '%s', c, '%s']
64
+ self << string % [a,b]
65
+ end
59
66
  end
60
67
  end
61
68
 
data/lib/red/version.rb CHANGED
@@ -2,7 +2,7 @@ module Red
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 4
4
4
  MINOR = 0
5
- TINY = 5
5
+ TINY = 6
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/red.rb CHANGED
@@ -99,7 +99,7 @@ module Red # :nodoc:
99
99
  :true => LogicNode::Boolean::True,
100
100
  :undef => DefinitionNode::Undef,
101
101
  :until => ControlNode::Loop::Until,
102
- :vcall => VariableNode::OtherVariable,
102
+ :vcall => CallNode::Method::ImplicitReceiver,
103
103
  :when => LogicNode::Case::When,
104
104
  :while => ControlNode::Loop::While,
105
105
  :xstr => LiteralNode::Uninterpreted,
@@ -219,11 +219,11 @@ module Red # :nodoc:
219
219
  @@red_singleton = nil
220
220
  @@red_block_arg = nil
221
221
  @@red_import = false
222
+ @@red_boolean = 'a'
222
223
  return true
223
224
  end
224
225
 
225
226
  def red!(options = {}, reset = false)
226
- Red.init if reset
227
227
  case self
228
228
  when Array
229
229
  raise(BuildError::UnknownNode, "Don't know how to handle sexp type :#{self.first}") unless ARRAY_NODES[self.first]
data/lib/source/ruby.rb CHANGED
@@ -6,7 +6,7 @@
6
6
  Red.donateMethodsToSingleton(superclass,newClass,true);
7
7
  Red.donateMethodsToClass(superclass.prototype,newClass.prototype,true);
8
8
  if(newClass==c$Module){delete(newClass.prototype.m$initialize);};
9
- if(newClass!==Number&&newClass!==Array){newClass.prototype.toString==superclass.prototype.toString;};
9
+ if(newClass!==Number&&newClass!==Array){newClass.prototype.toString=superclass.prototype.toString;};
10
10
  },
11
11
 
12
12
  donateMethodsToSingleton: function(donor,recipient,overwrite) {
@@ -52,6 +52,7 @@
52
52
  newClass.__id__ = Red.id++;
53
53
  newClass.__modules__ = {};
54
54
  newClass.__children__ = {};
55
+ newClass.__class__ = c$Class;
55
56
  newClass.prototype.__class__=newClass;
56
57
  Red.donateMethodsToSingleton(c$Class.prototype,newClass,true)
57
58
  },
@@ -68,7 +69,7 @@
68
69
  return context[0]['c$'+context[1]];
69
70
  },
70
71
 
71
- module: function(longName,block){
72
+ _module: function(longName,block){
72
73
  var newModule,context=Red.interpretNamespace(longName),namespace=context[0],name=context[1];
73
74
  if(namespace['c$'+name]) {
74
75
  newModule = namespace['c$'+name];
@@ -80,7 +81,7 @@
80
81
  if(typeof(block)=='function') { block.call(newModule); };
81
82
  },
82
83
 
83
- class: function(longName,superclass,block){
84
+ _class: function(longName,superclass,block){
84
85
  var newClass,context=Red.interpretNamespace(longName),namespace=context[0],name=context[1];
85
86
  if(namespace['c$'+name]) {
86
87
  if(name!=='Object' && superclass!==namespace['c$'+name].__superclass__){m$raise(c$TypeError,$q('superclass mismatch for class '+longName));};
@@ -107,18 +108,17 @@
107
108
  };
108
109
  if(typeof(block)=='function') { block.call(newClass); };
109
110
  Red.updateChildren(newClass);
110
- if((typeof(c$TrueClass)!='undefined'&&newClass==c$TrueClass)||(typeof(c$FalseClass)!='undefined'&&newClass==c$FalseClass)) { Red.donateMethodsToClass(newClass.prototype,Boolean.prototype); };
111
+ if((typeof(c$TrueClass)!='undefined'&&newClass==c$TrueClass)||(typeof(c$FalseClass)!='undefined'&&newClass==c$FalseClass)) { Red.donateMethodsToClass(newClass.prototype,Boolean.prototype,true); };
111
112
  },
112
113
 
113
114
  LoopError: {
114
- break:function(value){var e=new(Error);e.__keyword__='break';e._value=value==null?nil:value;throw(e);},
115
- next:function(value){var e=new(Error);e.__keyword__='next';e._value=value==null?nil:value;throw(e);},
116
- redo:function(){var e=new(Error);e.__keyword__='redo';throw(e);},
117
- isA:function(e,ary){for(var i=0,l=ary.length;i<l;++i){if(e.m$isABool(ary[i])){return true;};};return false;}
115
+ _break:function(value){var e=new(Error);e.__keyword__='break';e._value=value==null?nil:value;throw(e);},
116
+ _next:function(value){var e=new(Error);e.__keyword__='next';e._value=value==null?nil:value;throw(e);},
117
+ _redo:function(){var e=new(Error);e.__keyword__='redo';throw(e);},
118
118
  }
119
119
  };
120
120
 
121
- var $u=undefined,nil=null;
121
+ var $u=undefined,nil=null,$={};
122
122
 
123
123
  c$Class = function(){this.__id__=Red.id++};c$Class.__name__='Class';c$Class.__children__={};
124
124
  c$Module = function(){this.__id__=Red.id++};c$Module.__name__='Module';c$Module.__children__={};c$Class.__superclass__=c$Module;
@@ -132,6 +132,7 @@ window.__children__={'Object':true};
132
132
  window.m$include=function(){for(var i=0,modules=arguments,l=modules.length;i<l;++i){var mp=modules[i].prototype;for(var x in mp){if(x.slice(0,2)=='m$'){var f=function(){return arguments.callee._source[arguments.callee._name].apply(window,arguments) };f._source=mp;f._name=x;window[x]=f;};};modules[i].m$included(window);modules[i].__includers__['window']=true;};if(modules[0]!=c$Kernel){Red.donateMethodsToClass(window,c$Object.prototype);Red.updateChildren(c$Object);};return window;};
133
133
  window.m$blockGivenBool=function(){typeof(arguments[0])=='function'}
134
134
 
135
+ function $e(e,ary){if(e.m$isABool){for(var i=0,l=ary.length;i<l;++i){if(e.m$isABool(ary[i])){return true;};};};return false;};
135
136
  function $Q(){for(var i=1,s=arguments[0],l=arguments.length;i<l;++i){s+=$q(arguments[i]).m$toS()._value;};return $q(s);};
136
137
  function $q(obj){if(typeof obj!=='string'){return obj;};return c$String.m$new(obj);};
137
138
  function $r(value,options){return c$Regexp.m$new(value,options);};
@@ -223,7 +224,9 @@ class Object
223
224
  # k.__send__(:hello, "gentle", "readers") #=> "Hello gentle readers"
224
225
  #
225
226
  def __send__(method,*args)
226
- `this[method].apply(this,args)`
227
+ `method=this['m$'+sym._value.replace('=','Eql')]`
228
+ `if(!method){m$raise(c$NoMethodError,$q('undefined method "'+sym._value+'" for '+this));}`
229
+ `method.apply(this,args)`
227
230
  end
228
231
 
229
232
  # call-seq:
@@ -527,10 +530,10 @@ class Object
527
530
  # FIX: Incomplete
528
531
  def is_a?(klass)
529
532
  `if(this.m$class()==klass||c$Object==klass){return true;}` # true if instance_of? or if klass is Object
530
- `if(this.m$class()._modules[klass]){return true;}` # true if klass is included in obj's class
533
+ `if(this.m$class().__modules__[klass]){return true;}` # true if klass is included in obj's class
531
534
  `if(this.m$class()==c$Object){return false;}` # false if module check fails and obj is Object
532
535
  `var bubble=this.m$class(),result=false`
533
- `while(bubble!=c$Object){if(klass==bubble||bubble._modules[klass]!=null){result=true;};if(result){break;};bubble=bubble.__superclass__;}`
536
+ `while(bubble!=c$Object){if(klass==bubble||bubble.__modules__[klass]!=null){result=true;};if(result){break;};bubble=bubble.__superclass__;}`
534
537
  return `result`
535
538
  end
536
539
 
@@ -560,10 +563,10 @@ class Object
560
563
  # FIX: Incomplete
561
564
  def kind_of?(klass)
562
565
  `if(this.m$class()==klass||c$Object==klass){return true;}` # true if instance_of? or if klass is Object
563
- `if(this.m$class()._modules[klass]){return true;}` # true if klass is included in obj's class
566
+ `if(this.m$class().__modules__[klass]){return true;}` # true if klass is included in obj's class
564
567
  `if(this.m$class()==c$Object){return false;}` # false if module check fails and obj is Object
565
568
  `var bubble=this.m$class(),result=false`
566
- `while(bubble!=c$Object){if(klass==bubble||bubble._modules[klass]!=null){result=true;};if(result){break;};bubble=bubble.__superclass__;}`
569
+ `while(bubble!=c$Object){if(klass==bubble||bubble.__modules__[klass]!=null){result=true;};if(result){break;};bubble=bubble.__superclass__;}`
567
570
  return `result`
568
571
  end
569
572
 
@@ -668,7 +671,7 @@ class Object
668
671
  # Returns +true+ if _obj_ responds to the given method.
669
672
  #
670
673
  def respond_to?(method)
671
- `typeof this[method]=='function'`
674
+ `typeof(this['m$'+method._value])=='function'`
672
675
  end
673
676
 
674
677
  # call-seq:
@@ -688,8 +691,10 @@ class Object
688
691
  # k = Klass.new
689
692
  # k.send(:hello, "gentle", "readers") #=> "Hello gentle readers"
690
693
  #
691
- def send(method,*args)
692
- `this['m$'+method._value.replace('=','Eql')].apply(this,args)`
694
+ def send(sym,*args)
695
+ `method=this['m$'+sym._value.replace('=','Eql')]`
696
+ `if(!method){m$raise(c$NoMethodError,$q('undefined method "'+sym._value+'" for '+this));}`
697
+ `method.apply(this,args)`
693
698
  end
694
699
 
695
700
  # FIX: Incomplete
@@ -786,7 +791,8 @@ class Module
786
791
  def <=>(other_module)
787
792
  end
788
793
 
789
- def ===(object)
794
+ def ===(obj)
795
+ `obj.m$isABool(this)`
790
796
  end
791
797
 
792
798
  def >(other_module)
@@ -1071,7 +1077,7 @@ class Class < Module
1071
1077
  #
1072
1078
  # FIX: Incomplete
1073
1079
  def self.new(class_name, superclass = Object)
1074
- `Red.class(className._value,superclass,function(){})`
1080
+ `Red._class(className._value,superclass,function(){})`
1075
1081
  return `window['c$'+className._value]`
1076
1082
  end
1077
1083
 
@@ -2948,12 +2954,16 @@ class Exception
2948
2954
  `this._message==null?$q(this.m$class().__name__.replace(/\\./g,'::')):$q(this._message)`
2949
2955
  end
2950
2956
  end
2951
-
2957
+ `
2958
+ c$Exception.prototype.toString=function(){var class_name=this.m$class().__name__.replace(/\\./g,'::'),str=class_name+': '+(this._message||class_name);console.log(str+(this._stack!=null?'\\n from '+this.m$backtrace().join('\\n from '):''));return '#<'+str+'>';}
2959
+ `
2952
2960
  class StandardError < Exception ; end
2953
2961
  class ArgumentError < StandardError ; end
2954
2962
  class IndexError < StandardError ; end
2955
2963
  class RangeError < StandardError ; end
2956
2964
  class RuntimeError < StandardError ; end
2965
+ class NameError < StandardError ; end
2966
+ class NoMethodError < NameError ; end
2957
2967
  class TypeError < StandardError ; end
2958
2968
 
2959
2969
  # The global value +false+ is the only instance of class +FalseClass+ and
@@ -5605,7 +5615,7 @@ class String
5605
5615
  # 'abcdef'.include?(?c) #=> true
5606
5616
  #
5607
5617
  def include?(obj)
5608
- `new(RegExp)(typeof(obj)=='number'?String.fromCharCode(obj):obj._value).test(this._value)`
5618
+ `new(RegExp)(typeof(obj)=='number'?String.fromCharCode(obj):obj._value.replace(/([-.*+?^${}()|[\\]\\/\\\\])/g, '\\\\$1')).test(this._value)`
5609
5619
  end
5610
5620
 
5611
5621
  # FIX: Incomplete
@@ -6762,10 +6772,8 @@ class TrueClass
6762
6772
 
6763
6773
  undef initialize
6764
6774
  end
6765
-
6766
6775
  `
6767
6776
 
6768
- c$Exception.prototype.toString=function(){var class_name=this.m$class().__name__.replace(/\\./g,'::'),str=class_name+': '+(this._message||class_name);console.log(str+(this._stack!=null?'\\n from '+this.m$backtrace().join('\\n from '):''));return '#<'+str+'>';}
6769
6777
  c$NilClass.prototype.toString=function(){return 'nil';};
6770
6778
  c$Range.prototype.toString=function(){return ''+this._start+(this._exclusive?'...':'..')+this._end;};
6771
6779
  c$Regexp.prototype.toString=function(){return '/'+this._source+'/'+(/s/.test(this._options)?'m':'')+(/i/.test(this._options)?'i':'')+(/x/.test(this._options)?'x':'');};
@@ -6774,4 +6782,3 @@ c$Symbol.prototype.toString=function(){var v=this._value,str=/\\s/.test(v)?'"'+v
6774
6782
  c$Time.prototype.toString=function(){return ''+this._value;};
6775
6783
 
6776
6784
  `
6777
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.5
4
+ version: 4.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Sielaff
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-16 00:00:00 -04:00
12
+ date: 2008-10-19 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency