comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/core/BasicObjectNodes.java @ 13514:0fbee3eb71f0

Ruby: import project.
author Chris Seaton <chris.seaton@oracle.com>
date Mon, 06 Jan 2014 17:12:09 +0000
parents
children
comparison
equal deleted inserted replaced
13513:64a23ce736a0 13514:0fbee3eb71f0
1 /*
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
4 * redistribute it and/or modify it under the terms of the:
5 *
6 * Eclipse Public License version 1.0
7 * GNU General Public License version 2
8 * GNU Lesser General Public License version 2.1
9 */
10 package com.oracle.truffle.ruby.nodes.core;
11
12 import java.math.*;
13 import java.util.*;
14
15 import com.oracle.truffle.api.*;
16 import com.oracle.truffle.api.dsl.*;
17 import com.oracle.truffle.ruby.runtime.*;
18 import com.oracle.truffle.ruby.runtime.core.*;
19 import com.oracle.truffle.ruby.runtime.objects.*;
20
21 @CoreClass(name = "BasicObject")
22 public abstract class BasicObjectNodes {
23
24 @CoreMethod(names = "!", needsSelf = false, maxArgs = 0)
25 public abstract static class NotNode extends CoreMethodNode {
26
27 public NotNode(RubyContext context, SourceSection sourceSection) {
28 super(context, sourceSection);
29 }
30
31 public NotNode(NotNode prev) {
32 super(prev);
33 }
34
35 @Specialization
36 public boolean not() {
37 return false;
38 }
39
40 }
41
42 @CoreMethod(names = "==", minArgs = 1, maxArgs = 1)
43 public abstract static class EqualNode extends CoreMethodNode {
44
45 public EqualNode(RubyContext context, SourceSection sourceSection) {
46 super(context, sourceSection);
47 }
48
49 public EqualNode(EqualNode prev) {
50 super(prev);
51 }
52
53 @Specialization
54 public boolean equal(Object a, Object b) {
55 // TODO(CS) ideally all classes would do this in their own nodes
56 return a.equals(b);
57 }
58
59 }
60
61 @CoreMethod(names = "!=", minArgs = 1, maxArgs = 1)
62 public abstract static class NotEqualNode extends CoreMethodNode {
63
64 public NotEqualNode(RubyContext context, SourceSection sourceSection) {
65 super(context, sourceSection);
66 }
67
68 public NotEqualNode(NotEqualNode prev) {
69 super(prev);
70 }
71
72 @Specialization
73 public boolean notEqual(Object a, Object b) {
74 // TODO(CS) ideally all classes would do this in their own nodes
75 return !a.equals(b);
76 }
77
78 }
79
80 @CoreMethod(names = "equal?", minArgs = 1, maxArgs = 1)
81 public abstract static class ReferenceEqualNode extends CoreMethodNode {
82
83 public ReferenceEqualNode(RubyContext context, SourceSection sourceSection) {
84 super(context, sourceSection);
85 }
86
87 public ReferenceEqualNode(ReferenceEqualNode prev) {
88 super(prev);
89 }
90
91 @Specialization(order = 1)
92 public boolean equal(@SuppressWarnings("unused") NilPlaceholder a, @SuppressWarnings("unused") NilPlaceholder b) {
93 return true;
94 }
95
96 @Specialization(order = 2)
97 public boolean equal(int a, int b) {
98 return a == b;
99 }
100
101 @Specialization(order = 3)
102 public boolean equal(double a, double b) {
103 return a == b;
104 }
105
106 @Specialization(order = 4)
107 public boolean equal(BigInteger a, BigInteger b) {
108 return a.compareTo(b) == 0;
109 }
110
111 @Specialization(order = 5)
112 public boolean equal(RubyBasicObject a, RubyBasicObject b) {
113 return a == b;
114 }
115 }
116
117 @CoreMethod(names = "initialize", needsSelf = false, maxArgs = 0)
118 public abstract static class InitializeNode extends CoreMethodNode {
119
120 public InitializeNode(RubyContext context, SourceSection sourceSection) {
121 super(context, sourceSection);
122 }
123
124 public InitializeNode(InitializeNode prev) {
125 super(prev);
126 }
127
128 @Specialization
129 public NilPlaceholder initiailze() {
130 return NilPlaceholder.INSTANCE;
131 }
132
133 }
134
135 @CoreMethod(names = {"send", "__send__"}, needsSelf = true, needsBlock = true, minArgs = 1, isSplatted = true)
136 public abstract static class SendNode extends CoreMethodNode {
137
138 public SendNode(RubyContext context, SourceSection sourceSection) {
139 super(context, sourceSection);
140 }
141
142 public SendNode(SendNode prev) {
143 super(prev);
144 }
145
146 @Specialization
147 public Object send(RubyBasicObject self, Object[] args, @SuppressWarnings("unused") UndefinedPlaceholder block) {
148 final String name = args[0].toString();
149 final Object[] sendArgs = Arrays.copyOfRange(args, 1, args.length);
150 return self.send(name, null, sendArgs);
151 }
152
153 @Specialization
154 public Object send(RubyBasicObject self, Object[] args, RubyProc block) {
155 final String name = args[0].toString();
156 final Object[] sendArgs = Arrays.copyOfRange(args, 1, args.length);
157 return self.send(name, block, sendArgs);
158 }
159
160 }
161
162 }