Class: Lisp::ClassObject
- Inherits:
-
Atom
show all
- Defined in:
- lib/rubylisp/ffi_class.rb
Instance Attribute Summary
Attributes inherited from Atom
#value
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Atom
#alist?, #all?, #apply_to, #car, #cdr, #character?, #copy, #doc, #eq?, #evaluate, #frame?, #function?, #length, #lisp_object?, #list?, #macro?, #negative?, #number?, #object?, #pair?, #positive?, #primitive?, #print_string, #quoted, #set!, #special?, #string?, #symbol?, #vector?, #zero?
Constructor Details
Returns a new instance of ClassObject.
122
123
124
|
# File 'lib/rubylisp/ffi_class.rb', line 122
def initialize(c)
@value = c
end
|
Class Method Details
.add_method_impl(args, env) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/rubylisp/ffi_class.rb', line 79
def self.add_method_impl(args, env)
raise "'add-method' requires 3 arguments." if args.length != 3
class_name = args.car.evaluate(env)
raise "'add-method' requires a class name as it's first argument." unless class_name.string? || class_name.symbol?
target_class = Object.const_get(class_name.to_s)
raise "'add-method' requires the name of an existing class." if target_class.nil?
method_name = args.cadr.evaluate(env)
raise "'add-method' requires a method name as it's second argument." unless class_name.string? || class_name.symbol?
body = args.caddr.evaluate(env)
raise "'add-method' requires a function as it's third argument." unless body.function?
target_class.send(:define_method, method_name.to_s) do |*args|
local_env = Lisp::EnvironmentFrame.extending(env)
local_env.bind_locally(Symbol.named("self"), Lisp::NativeObject.with_value(self))
processed_args = args.map {|a| Lisp::ClassObject.convert_to_lisp(a)}
Lisp::ClassObject.convert_to_ruby(body.apply_to(Lisp::ConsCell.array_to_list(processed_args), in: local_env), in: local_env)
end
Lisp::String.with_value("OK")
end
|
.add_static_method_impl(args, env) ⇒ Object
107
108
109
|
# File 'lib/rubylisp/ffi_class.rb', line 107
def self.add_static_method_impl(args, env)
Lisp::String.with_value("NOT IMPLEMENTED")
end
|
.convert_to_lisp(value) ⇒ Object
.convert_to_ruby(a, env) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/rubylisp/ffi_class.rb', line 64
def self.convert_to_ruby(a, env)
if a.nil?
nil
elsif a.function?
proc do
a.apply_to(Lisp::ConsCell.new, env)
end
elsif a.list?
a.to_a.map {|i| convert_to_ruby(i, env)}
else
a.value
end
end
|
.extend_impl(args, env) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/rubylisp/ffi_class.rb', line 26
def self.extend_impl(args, env)
raise "'extend' requires 2 arguments." if args.length != 2
class_name = args.car.evaluate(env)
raise "'extend' requires a name as it's first argument." unless class_name.string? || class_name.symbol?
super_class = Object.const_get(class_name.to_s)
raise "'extend' requires the name of an existing class as it's first argument." if super_class.nil?
new_class_name = args.cadr.evaluate(env)
raise "'extend' requires a name as it's second argument." unless new_class_name.string? || new_class_name.symbol?
new_class = Class.new(super_class)
raise "'extend' requires the name of a new (i.e. nonexistant) class as it's second argument." if Object.const_defined?(new_class_name.to_s)
Object.const_set(new_class_name.to_s, new_class)
ClassObject.with_class(new_class)
end
|
.new_instance ⇒ Object
112
113
114
|
# File 'lib/rubylisp/ffi_class.rb', line 112
def self.new_instance
self.new(@value.alloc.init)
end
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/rubylisp/ffi_class.rb', line 6
def self.register
Primitive.register("extend", "(extend parent child)\n\nCreates a new class named child that extends (i.e. inherits from) the class parent. The names (parent and child can be either stirngs or symbols). The new class is accessible by name and is returned.") do |args, env|
Lisp::ClassObject::extend_impl(args, env)
end
Primitive.register("add-method", "(add-method class selector function)\n\nAdd a method named selector to the class named class using function as it’s body. function can be a reference to a named function or, more likely, a lambda expression.") do |args, env|
Lisp::ClassObject::add_method_impl(args, env)
end
Primitive.register("add-static-method", "Not Implemented.") do |args, env|
Lisp::ClassObject::add_static_method_impl(args, env)
end
Primitive.register("super", "Not implemented.") do |args, env|
Lisp::ClassObject::super_impl(args, env)
end
end
|
.super_impl(args, env) ⇒ Object
102
103
104
|
# File 'lib/rubylisp/ffi_class.rb', line 102
def self.super_impl(args, env)
Lisp::String.with_value("NOT IMPLEMENTED")
end
|
.with_class(c) ⇒ Object
117
118
119
|
# File 'lib/rubylisp/ffi_class.rb', line 117
def self.with_class(c)
self.new(c)
end
|
Instance Method Details
132
133
134
|
# File 'lib/rubylisp/ffi_class.rb', line 132
def class?
true
end
|
157
158
159
|
# File 'lib/rubylisp/ffi_class.rb', line 157
def false?
@value == nil
end
|
#native_type ⇒ Object
142
143
144
|
# File 'lib/rubylisp/ffi_class.rb', line 142
def native_type
@value.class
end
|
147
148
149
|
# File 'lib/rubylisp/ffi_class.rb', line 147
def to_s
"<a class: #{@value.name}>"
end
|
152
153
154
|
# File 'lib/rubylisp/ffi_class.rb', line 152
def true?
@value != nil
end
|
137
138
139
|
# File 'lib/rubylisp/ffi_class.rb', line 137
def type
:class
end
|
#with_value(&block) ⇒ Object
127
128
129
|
# File 'lib/rubylisp/ffi_class.rb', line 127
def with_value(&block)
block.call(@value)
end
|