forked from proglangclass/interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.rb
More file actions
52 lines (43 loc) · 1.48 KB
/
runtime.rb
File metadata and controls
52 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require "runtime/object"
require "runtime/class"
require "runtime/method"
require "runtime/context"
# Bootstrap the runtime. This is where we assemble all the classes and objects together
# to form the runtime.
rclass = RClass.new # Class
rclass.runtime_class = rclass # Class.class = Class
object_class = RClass.new # Object = Class.new
object_class.runtime_class = rclass # Object.class = Class
# self.print(1)
Runtime = Context.new(object_class.new) # Object.new
Runtime["Class"] = rclass
Runtime["Object"] = object_class
Runtime["Number"] = RClass.new
Runtime["String"] = RClass.new
Runtime["TrueClass"] = RClass.new
Runtime["FalseClass"] = RClass.new
Runtime["NilClass"] = RClass.new
Runtime["true"] = Runtime["TrueClass"].new_with_value(true)
Runtime["false"] = Runtime["FalseClass"].new_with_value(false)
Runtime["nil"] = Runtime["NilClass"].new_with_value(nil)
# Object.new
Runtime["Class"].runtime_methods["new"] = proc do |receiver, arguments|
receiver.new
end
# print("hi there!")
Runtime["Object"].runtime_methods["print"] = proc do |receiver, arguments|
puts arguments.first.ruby_value
Runtime["nil"]
end
# 1 + 2, same as 1.+(2)
Runtime["Number"].runtime_methods["+"] = proc do |receiver, arguments|
a = receiver.ruby_value
b = arguments.first.ruby_value
Runtime["Number"].new_with_value(a + b)
end
# 1 * 2
Runtime["Number"].runtime_methods["*"] = proc do |receiver, arguments|
a = receiver.ruby_value
b = arguments.first.ruby_value
Runtime["Number"].new_with_value(a * b)
end