AmberVM 0.0.19
Sign up to get free protection for your applications and to get access to all the features.
- data/README +38 -0
- data/bin/ambervm +278 -0
- data/lib/amber/acts_as_rvm_type.rb +157 -0
- data/lib/amber/classes/association.rb +36 -0
- data/lib/amber/classes/block.rb +52 -0
- data/lib/amber/classes/boolean.rb +40 -0
- data/lib/amber/classes/class.rb +50 -0
- data/lib/amber/classes/error.rb +22 -0
- data/lib/amber/classes/list.rb +96 -0
- data/lib/amber/classes/null.rb +35 -0
- data/lib/amber/classes/number.rb +95 -0
- data/lib/amber/classes/object.rb +56 -0
- data/lib/amber/classes/string.rb +79 -0
- data/lib/amber/classes.rb +113 -0
- data/lib/amber/environment.rb +251 -0
- data/lib/amber/fukubukuro/ecma_core.rb +409 -0
- data/lib/amber/fukubukuro.rb +866 -0
- data/lib/amber/functions/all.rb +3 -0
- data/lib/amber/functions/array/append.rb +50 -0
- data/lib/amber/functions/array/at.rb +50 -0
- data/lib/amber/functions/array/set_at.rb +50 -0
- data/lib/amber/functions/array.rb +30 -0
- data/lib/amber/functions/association/assoc_get.rb +55 -0
- data/lib/amber/functions/association/assoc_set.rb +56 -0
- data/lib/amber/functions/bitwise/bitwise_and.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_not.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_or.rb +41 -0
- data/lib/amber/functions/bitwise/bitwise_xor.rb +41 -0
- data/lib/amber/functions/bitwise.rb +3 -0
- data/lib/amber/functions/collection/get.rb +66 -0
- data/lib/amber/functions/collection/set.rb +67 -0
- data/lib/amber/functions/collection/size.rb +54 -0
- data/lib/amber/functions/general/cmp.rb +43 -0
- data/lib/amber/functions/general/eq.rb +45 -0
- data/lib/amber/functions/general/gt.rb +45 -0
- data/lib/amber/functions/general/gte.rb +45 -0
- data/lib/amber/functions/general/lt.rb +45 -0
- data/lib/amber/functions/general/lte.rb +45 -0
- data/lib/amber/functions/general/neq.rb +45 -0
- data/lib/amber/functions/general/type.rb +43 -0
- data/lib/amber/functions/general.rb +3 -0
- data/lib/amber/functions/io/print.rb +45 -0
- data/lib/amber/functions/io.rb +3 -0
- data/lib/amber/functions/list/align.rb +73 -0
- data/lib/amber/functions/list/join.rb +45 -0
- data/lib/amber/functions/list/map.rb +58 -0
- data/lib/amber/functions/list/split.rb +55 -0
- data/lib/amber/functions/list.rb +3 -0
- data/lib/amber/functions/logic/and.rb +55 -0
- data/lib/amber/functions/logic/not.rb +40 -0
- data/lib/amber/functions/logic/or.rb +50 -0
- data/lib/amber/functions/logic.rb +3 -0
- data/lib/amber/functions/math/abs.rb +39 -0
- data/lib/amber/functions/math/acos.rb +39 -0
- data/lib/amber/functions/math/add.rb +40 -0
- data/lib/amber/functions/math/asin.rb +39 -0
- data/lib/amber/functions/math/atan.rb +39 -0
- data/lib/amber/functions/math/ceil.rb +39 -0
- data/lib/amber/functions/math/cos.rb +39 -0
- data/lib/amber/functions/math/dec.rb +39 -0
- data/lib/amber/functions/math/div.rb +44 -0
- data/lib/amber/functions/math/exp.rb +39 -0
- data/lib/amber/functions/math/floor.rb +39 -0
- data/lib/amber/functions/math/inc.rb +39 -0
- data/lib/amber/functions/math/log.rb +39 -0
- data/lib/amber/functions/math/mod.rb +41 -0
- data/lib/amber/functions/math/mul.rb +43 -0
- data/lib/amber/functions/math/neg.rb +43 -0
- data/lib/amber/functions/math/power.rb +43 -0
- data/lib/amber/functions/math/rand.rb +36 -0
- data/lib/amber/functions/math/round.rb +39 -0
- data/lib/amber/functions/math/shl.rb +41 -0
- data/lib/amber/functions/math/shr.rb +41 -0
- data/lib/amber/functions/math/sin.rb +39 -0
- data/lib/amber/functions/math/sub.rb +43 -0
- data/lib/amber/functions/math/tan.rb +39 -0
- data/lib/amber/functions/math.rb +3 -0
- data/lib/amber/functions/objects/send.rb +22 -0
- data/lib/amber/functions/rails/print.rb +44 -0
- data/lib/amber/functions/rails.rb +3 -0
- data/lib/amber/functions/string/ansi.rb +24 -0
- data/lib/amber/functions/string/capstr.rb +23 -0
- data/lib/amber/functions/string/center.rb +25 -0
- data/lib/amber/functions/string/chr.rb +16 -0
- data/lib/amber/functions/string/ljust.rb +26 -0
- data/lib/amber/functions/string/regmatch.rb +34 -0
- data/lib/amber/functions/string/rjust.rb +26 -0
- data/lib/amber/functions/string.rb +3 -0
- data/lib/amber/functions.rb +103 -0
- data/lib/amber/interpreter.rb +1380 -0
- data/lib/amber/languages/brainfuck.rb +153 -0
- data/lib/amber/languages/ecma/compiler.rb +1661 -0
- data/lib/amber/languages/ecma/core-math.js +67 -0
- data/lib/amber/languages/ecma/core-objects.js +57 -0
- data/lib/amber/languages/ecma.rb +9 -0
- data/lib/amber/languages/ecma_fuku/compiler.rb +1622 -0
- data/lib/amber/languages/ecma_fuku/core-math.js +67 -0
- data/lib/amber/languages/ecma_fuku/core-objects.js +56 -0
- data/lib/amber/languages/ecma_fuku.rb +13 -0
- data/lib/amber/languages/math/compiler.rb +70 -0
- data/lib/amber/languages/math/tokenizer.rb +69 -0
- data/lib/amber/languages/math/tree.rb +110 -0
- data/lib/amber/languages/math.rb +26 -0
- data/lib/amber/languages.rb +99 -0
- data/lib/amber/library.rb +79 -0
- data/lib/amber/optimisation.rb +299 -0
- data/lib/amber/plugin.rb +337 -0
- data/lib/amber/rails.rb +90 -0
- data/lib/amber.rb +106 -0
- data/spec/amber/class_spec.rb +27 -0
- data/spec/amber/enviroment_spec.rb +61 -0
- data/spec/amber/function_spec.rb +25 -0
- data/spec/amber/functions/association/assoc_get_spec.rb +41 -0
- data/spec/amber/functions/association/assoc_set_spec.rb +43 -0
- data/spec/amber/functions/collection/get_spec.rb +12 -0
- data/spec/amber/functions/collection/set_spec.rb +10 -0
- data/spec/amber/functions/collection/size_spec.rb +10 -0
- data/spec/amber/functions/list/split_spec.rb +47 -0
- data/spec/amber/functions/string/ansi_spec.rb +44 -0
- data/spec/amber/functions/string/capstr_spec.rb +42 -0
- data/spec/amber/functions/string/center_spec.rb +49 -0
- data/spec/amber/functions/string/ljust_spec.rb +49 -0
- data/spec/amber/functions/string/regmatch_spec.rb +52 -0
- data/spec/amber/functions/string/rjust_spec.rb +49 -0
- data/spec/amber/interpreter/assignment_spec.rb +22 -0
- data/spec/amber/interpreter/condition_spec.rb +103 -0
- data/spec/amber/interpreter/constant_spec.rb +31 -0
- data/spec/amber/interpreter/core_call_spec.rb +72 -0
- data/spec/amber/interpreter/interpreter_spec.rb +11 -0
- data/spec/amber/interpreter/parameter_spec.rb +24 -0
- data/spec/amber/interpreter/sequence_spec.rb +47 -0
- data/spec/amber/interpreter/variable_spec.rb +24 -0
- data/spec/amber/plugin_spec.rb +10 -0
- data/spec/classes/atom/association_spec.rb +39 -0
- data/spec/classes/atom/block_spec.rb +25 -0
- data/spec/classes/atom/boolean_spec.rb +67 -0
- data/spec/classes/atom/error_spec.rb +43 -0
- data/spec/classes/atom/list_spec.rb +68 -0
- data/spec/classes/atom/number_spec.rb +132 -0
- data/spec/classes/atom/string_spec.rb +175 -0
- data/spec/languages/ecma/ecma_array_spec.rb +79 -0
- data/spec/languages/ecma/ecma_closure_spec.rb +38 -0
- data/spec/languages/ecma/ecma_literals_spec.rb +71 -0
- data/spec/languages/ecma/ecma_objects_spec.rb +165 -0
- data/spec/languages/ecma/ecma_old_spec.rb +540 -0
- data/spec/languages/ecma/ecma_spec.rb +64 -0
- data/spec/languages/ecma_fuku/ecma_array_spec.rb +61 -0
- data/spec/languages/ecma_fuku/ecma_closure_spec.rb +33 -0
- data/spec/languages/ecma_fuku/ecma_function_spec.rb +84 -0
- data/spec/languages/ecma_fuku/ecma_literals_spec.rb +55 -0
- data/spec/languages/ecma_fuku/ecma_objects_spec.rb +133 -0
- data/spec/languages/ecma_fuku/ecma_old_spec.rb +415 -0
- data/spec/languages/ecma_fuku/ecma_operator_spec.rb +33 -0
- data/spec/languages/ecma_fuku/ecma_spec.rb +52 -0
- data/spec/languages/math/compiler_spec.rb +49 -0
- data/spec/languages/math/tokenizer_spec.rb +73 -0
- data/spec/languages/math/tree_spec.rb +153 -0
- metadata +225 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
module AmberVM
|
2
|
+
module Classes
|
3
|
+
class List < AmberVM::Classes::Class
|
4
|
+
plugin_host Classes
|
5
|
+
register_for :list
|
6
|
+
@@type = :list
|
7
|
+
attr_reader :sepperator
|
8
|
+
|
9
|
+
def [] i
|
10
|
+
if not @array[i]
|
11
|
+
@array[i] = AmberVM::Interpreter::VariableStorage.new(nil)
|
12
|
+
end
|
13
|
+
@array[i]
|
14
|
+
end
|
15
|
+
|
16
|
+
def []= i,v
|
17
|
+
v = v.val if v.is_a? AmberVM::Interpreter::VariableStorage
|
18
|
+
if not @array[i]
|
19
|
+
@array[i] = AmberVM::Interpreter::VariableStorage.new(v)
|
20
|
+
else
|
21
|
+
@array[i].val = v
|
22
|
+
end
|
23
|
+
@array[i]
|
24
|
+
end
|
25
|
+
|
26
|
+
def data_type
|
27
|
+
:list
|
28
|
+
end
|
29
|
+
|
30
|
+
def array
|
31
|
+
@array
|
32
|
+
end
|
33
|
+
|
34
|
+
def variables
|
35
|
+
@variables ||= super.merge({
|
36
|
+
'size' => AmberVM::Interpreter::VariableStorageCallback.new(self,:size,false),
|
37
|
+
'length' => AmberVM::Interpreter::VariableStorageCallback.new(self,:size,false)
|
38
|
+
})
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize source = [], sepperator = ' '
|
42
|
+
super()
|
43
|
+
@array = Array.new()
|
44
|
+
if source.is_a?(::String) # Okay we've to hack here to get sure we get the /real/ String
|
45
|
+
source = source.split sepperator
|
46
|
+
elsif !source.is_a?(Array)
|
47
|
+
source = [source]
|
48
|
+
end
|
49
|
+
source.map! {|i| AmberVM::Interpreter::makevs(i)}
|
50
|
+
@array.concat source
|
51
|
+
@sepperator = sepperator.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
def resplit(sep)
|
55
|
+
sep = sep.to_s
|
56
|
+
if sep != @sepperator
|
57
|
+
t = @array.map{|i| i.val}.join(@sepperator)
|
58
|
+
@sepperator = sep
|
59
|
+
@array.clear
|
60
|
+
@array.concat(t.split(sep).map{|i| AmberVM::Interpreter::makevs(i)})
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def == v
|
65
|
+
if v.is_a? List
|
66
|
+
@array == v.array and @sepperator == v.sepperator
|
67
|
+
else
|
68
|
+
@array.map{|i| i.val} == v
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def + v
|
73
|
+
if v.is_a? List
|
74
|
+
@array + v.array
|
75
|
+
else
|
76
|
+
@array + v
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def size
|
81
|
+
@array.size
|
82
|
+
end
|
83
|
+
|
84
|
+
def sepperator= sep
|
85
|
+
@sepperator = sep.to_s
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_s
|
89
|
+
if @sepperator
|
90
|
+
@array.map{|i| i.val.to_s}.join(@sepperator)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AmberVM
|
2
|
+
module Classes
|
3
|
+
class Null < AmberVM::Classes::Class
|
4
|
+
register_for :null
|
5
|
+
|
6
|
+
def data_type
|
7
|
+
:null
|
8
|
+
end
|
9
|
+
|
10
|
+
def hash
|
11
|
+
nil.hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def eql?(o)
|
15
|
+
if o.nil? and o.is_a?(AmberVM::Classes::Null)
|
16
|
+
true
|
17
|
+
else
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize value
|
23
|
+
super()
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
'null'
|
28
|
+
end
|
29
|
+
|
30
|
+
def is_true?
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module AmberVM
|
2
|
+
module Classes
|
3
|
+
class Number < Class
|
4
|
+
register_for :number
|
5
|
+
@@type = :number
|
6
|
+
|
7
|
+
# Fix for comparing numbers.
|
8
|
+
# We need to have the values hash here.
|
9
|
+
def hash
|
10
|
+
@value.hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def eql?(o)
|
14
|
+
if o.is_a?(AmberVM::Classes::Number)
|
15
|
+
@value == o.value
|
16
|
+
elsif o.is_a?(@value.class)
|
17
|
+
@value == o
|
18
|
+
else
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
def initialize value
|
26
|
+
super()
|
27
|
+
if value.is_a? Numeric
|
28
|
+
@value = value
|
29
|
+
elsif value.respond_to?(:to_f) and ((v = value.to_f) != value.to_i)
|
30
|
+
@value = v
|
31
|
+
elsif value.respond_to?(:to_i)
|
32
|
+
@value = value.to_i
|
33
|
+
else
|
34
|
+
@value = "#{value}".to_f
|
35
|
+
if @value == @value.to_i
|
36
|
+
@value = @value.to_i
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def == v
|
42
|
+
@value == v
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
@value.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_i
|
50
|
+
@value.to_i
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_f
|
54
|
+
@value.to_f
|
55
|
+
end
|
56
|
+
|
57
|
+
def is_true?
|
58
|
+
@value != 0
|
59
|
+
end
|
60
|
+
|
61
|
+
def value
|
62
|
+
@value
|
63
|
+
end
|
64
|
+
|
65
|
+
def pretty_print(q)
|
66
|
+
q.text @value.to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
#alias :respond_to_number :respond_to?
|
70
|
+
#def respond_to?(symbol,include_private = false)
|
71
|
+
# respond_to_number(symbol,include_private)
|
72
|
+
#end
|
73
|
+
|
74
|
+
alias :method_missing_number :method_missing
|
75
|
+
|
76
|
+
def method_missing m, *args, &block
|
77
|
+
if @value.respond_to? m
|
78
|
+
args.map! do |a|
|
79
|
+
if a.is_a? Number
|
80
|
+
a.value
|
81
|
+
else
|
82
|
+
Number.new(a).value
|
83
|
+
end
|
84
|
+
end
|
85
|
+
r = @value.send(m,*args)
|
86
|
+
r = Number.new(r) if r.is_a? Numeric
|
87
|
+
r
|
88
|
+
else
|
89
|
+
#raise "beep!#{m}, #{args}, #{block}"
|
90
|
+
super(m, *args, &block)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module AmberVM
|
2
|
+
module Classes
|
3
|
+
class Object < AmberVM::Classes::Class
|
4
|
+
register_for :object
|
5
|
+
attr_reader :variables
|
6
|
+
attr_reader :object_class
|
7
|
+
|
8
|
+
def env
|
9
|
+
@env
|
10
|
+
end
|
11
|
+
|
12
|
+
class VariableProxy
|
13
|
+
def initialize env
|
14
|
+
@env = env
|
15
|
+
end
|
16
|
+
def [] n
|
17
|
+
@env[n]
|
18
|
+
end
|
19
|
+
|
20
|
+
def []= n,v
|
21
|
+
@env[n] = v
|
22
|
+
end
|
23
|
+
|
24
|
+
def each &block
|
25
|
+
@env.data[:locals].each(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def keys
|
29
|
+
@env.data[:locals].keys
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def initialize objectClass, parent_env = nil
|
35
|
+
super()
|
36
|
+
@object_class = objectClass
|
37
|
+
@env = AmberVM::Interpreter::Environment.new({:locals => {:self => self}}, parent_env)
|
38
|
+
@variables = VariableProxy.new(@env)
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def obj_send(method, params, env)
|
43
|
+
m = @variables[method] || @object_class.variables[method]
|
44
|
+
raise "Unknown method #{method} for object #{self}" if not m or not m.respond_to?(:call)
|
45
|
+
AmberVM::debug "Calling object function method #{method}." if $DEBUG
|
46
|
+
env = AmberVM::Interpreter::Environment.new({:params => params||[], :locals => @variables}, env)
|
47
|
+
m.call(params, env)
|
48
|
+
end
|
49
|
+
|
50
|
+
def data_type
|
51
|
+
:object
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module AmberVM
|
2
|
+
module Classes
|
3
|
+
class String < ::String
|
4
|
+
extend Plugin
|
5
|
+
plugin_host Classes
|
6
|
+
register_for :string
|
7
|
+
@@type = :string
|
8
|
+
ANSI_MAP = {
|
9
|
+
'h' => 1, 'H' => 22,
|
10
|
+
'u' => 4, 'U' => 24,
|
11
|
+
'f' => 5, 'f' => 25,
|
12
|
+
'i' => 7, 'I' => 27,
|
13
|
+
'n' => 0,
|
14
|
+
'x' => 30, 'X' => 40,
|
15
|
+
'r' => 31, 'R' => 41,
|
16
|
+
'g' => 32, 'G' => 42,
|
17
|
+
'y' => 33, 'Y' => 42,
|
18
|
+
'b' => 34, 'B' => 44,
|
19
|
+
'm' => 35, 'M' => 45,
|
20
|
+
'c' => 36, 'C' => 45,
|
21
|
+
'w' => 37, 'W' => 47,
|
22
|
+
} unless defined?(ANSI_MAP)
|
23
|
+
|
24
|
+
def initialize val = ""
|
25
|
+
super val.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
def is_true?
|
29
|
+
not empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
def data_type
|
33
|
+
:string
|
34
|
+
end
|
35
|
+
|
36
|
+
def center width, char = " "
|
37
|
+
c = char[0..0]
|
38
|
+
c = ' ' if c.empty?
|
39
|
+
width = width.to_i - self.length
|
40
|
+
width = 0 if width < 0
|
41
|
+
l = width / 2
|
42
|
+
r = width - l
|
43
|
+
self.class.new((c*l) + self + (c*r))
|
44
|
+
end
|
45
|
+
|
46
|
+
def ljust width, char = " "
|
47
|
+
c = char[0..0]
|
48
|
+
c = ' ' if c.empty?
|
49
|
+
width = width.to_i - self.length
|
50
|
+
width = 0 if width < 0
|
51
|
+
self.class.new(self + (c*width))
|
52
|
+
end
|
53
|
+
|
54
|
+
def rjust width, char = " "
|
55
|
+
c = char[0..0]
|
56
|
+
c = ' ' if c.empty?
|
57
|
+
width = width.to_i - self.length
|
58
|
+
width = 0 if width < 0
|
59
|
+
self.class.new((c*width) + self)
|
60
|
+
end
|
61
|
+
|
62
|
+
def + v
|
63
|
+
v = v.to_s if not v.is_a?(String)
|
64
|
+
self.class.new(super(v))
|
65
|
+
end
|
66
|
+
|
67
|
+
def ansi colorset
|
68
|
+
colorset.gsub!(/[^#{ANSI_MAP.keys.join('')}]/,'')
|
69
|
+
colorcode = colorset.split('').map { |c| ANSI_MAP[c].to_s}.join(';')
|
70
|
+
c = "\e[#{colorcode}m"
|
71
|
+
self.class.new("#{c}#{self.gsub("\e[0m",c)}\e[0m")
|
72
|
+
end
|
73
|
+
|
74
|
+
def value
|
75
|
+
to_s
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# = classes.rb - Class Plugin Library
|
2
|
+
#
|
3
|
+
# Copyright (C) 2008 Heinz N. Gies (heinz@licenser.net)
|
4
|
+
#
|
5
|
+
# This file is published under the MIT Licenser, see LICENSE for details.
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'amber/plugin'
|
9
|
+
require 'amber/functions'
|
10
|
+
module AmberVM
|
11
|
+
# This module is the basic class container, classes are supposed to be
|
12
|
+
# used by this as in: AmberVM::Classes[<class id>] this guarnatees that,
|
13
|
+
# in the case of overwriting a standard class the code still works.
|
14
|
+
#
|
15
|
+
# == Example
|
16
|
+
#
|
17
|
+
# === Creating a string calss
|
18
|
+
#
|
19
|
+
# require 'amber/classes'
|
20
|
+
# string = AmberVM::Classes[:string].new
|
21
|
+
module Classes
|
22
|
+
extend AmberVM::PluginHost
|
23
|
+
default :string
|
24
|
+
|
25
|
+
# The Parent for new classes, meant never to be used alone.
|
26
|
+
# It takes care of registering the calss to the PluginHost,
|
27
|
+
# as well of basic functionality.
|
28
|
+
#
|
29
|
+
# Also it offers access to the function plugins to be used
|
30
|
+
# as functions within the commands.
|
31
|
+
#
|
32
|
+
# == Examples
|
33
|
+
#
|
34
|
+
# === COMPLEX NUMBERS
|
35
|
+
#
|
36
|
+
# # file: amber/classes/math/complex.rb
|
37
|
+
# require 'amber/classes'
|
38
|
+
# module AmberVM
|
39
|
+
# module Classes
|
40
|
+
# class Complex < AmberVM::Classes::Class
|
41
|
+
# def initialize i, n
|
42
|
+
# @i = i
|
43
|
+
# @j = j
|
44
|
+
# end
|
45
|
+
# #...
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
class Class
|
50
|
+
|
51
|
+
# We make classes a Plugin and set the plugin host to Classes s it can
|
52
|
+
# handle the details for it.
|
53
|
+
extend AmberVM::Plugin
|
54
|
+
plugin_host Classes
|
55
|
+
|
56
|
+
# Basic functions hash for classes so every class at least can respond
|
57
|
+
# to a +functions+ call.
|
58
|
+
def functions
|
59
|
+
@functions ||= {}
|
60
|
+
end
|
61
|
+
|
62
|
+
# Basic variables hash for classes so every class at least can respond
|
63
|
+
# to a +variables+ call.
|
64
|
+
def variables
|
65
|
+
@variables ||= {:self => self}
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
# Every class is by default treated as beeing true when not defined
|
70
|
+
# otherwise.
|
71
|
+
# This makes sure that calling boolean functions on classes will always
|
72
|
+
# return a usefull result.
|
73
|
+
# So it can be redefinde to say, have 0 act as false if this is wanted.
|
74
|
+
def is_true?
|
75
|
+
true
|
76
|
+
end
|
77
|
+
|
78
|
+
# This defines the type of the class, it defaults to :any
|
79
|
+
# it is important for tying and type conversion, as long as it
|
80
|
+
# behaves like a string, it can look like a sting ;)
|
81
|
+
def data_type
|
82
|
+
:any
|
83
|
+
end
|
84
|
+
|
85
|
+
def obj_send(method, params, env)
|
86
|
+
@env ||= AmberVM::Interpreter::Environment.new({:params => params||[], :locals => variables, :functions => functions}, env)
|
87
|
+
m = @functions[method]
|
88
|
+
raise "Unknown method #{method} for object #{self}" if not m
|
89
|
+
AmberVM::debug "Calling object function method #{method}."
|
90
|
+
m.call(params, @env)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Redefinding the method to allow using self defined functions
|
94
|
+
# within classes - while this is discuraged it still is possible.
|
95
|
+
def method_missing(m, *args, &block)
|
96
|
+
# Check if the functions PluginHost knwos about the called function
|
97
|
+
if (AmberVM::Functions::has? m)
|
98
|
+
# Calls the function with given args and environment that is,
|
99
|
+
# hopefully defiend in the calling class.
|
100
|
+
AmberVM::Functions[m].execute args, @env
|
101
|
+
else
|
102
|
+
# If the function is not known we call the usual method missing
|
103
|
+
# method.
|
104
|
+
super(m, *args, &block)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
Dir[File.dirname(__FILE__) + '/classes/*.rb'].each do |c|
|
112
|
+
require 'amber/classes/' + File.basename(c)
|
113
|
+
end
|