alphalang 0.2.3 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nodes/basenodes.rb +34 -41
- data/lib/nodes/scopemanager.rb +5 -1
- data/lib/nodes/stmtnodes.rb +14 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f0ae223746d2a819889026bde1c6ec27b0d331567aa0734fdbbc824b45d4625
|
4
|
+
data.tar.gz: d3b0e9a67edcd936217b50d931c3993ab32c5c8efca805cb1dfbe0a6b260fa3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5022eaae630f0fbc43485f5b5bf3d55e3c0bcc577818edc2f2aeb209f2f323ca233c2ae6d015f296a47caf45c5958626f5d69a6be9e8100fdf3b104f48ab2571
|
7
|
+
data.tar.gz: ae51f1d99f90e029b9a2569dd5cb3bb78c48f06bb080781b0c5a3631d05433d922eb990790f2a985dd0435fe4461f98cbbb1004e7b47e40399574271f96929b6
|
data/lib/nodes/basenodes.rb
CHANGED
@@ -2,30 +2,6 @@
|
|
2
2
|
|
3
3
|
####################################################
|
4
4
|
|
5
|
-
class TrueClass
|
6
|
-
def evaluate
|
7
|
-
self
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class FalseClass
|
12
|
-
def evaluate
|
13
|
-
self
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class String
|
18
|
-
def name
|
19
|
-
self
|
20
|
-
end
|
21
|
-
|
22
|
-
def evaluate
|
23
|
-
self
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
####################################################
|
28
|
-
|
29
5
|
class Node
|
30
6
|
attr_accessor :value
|
31
7
|
|
@@ -58,7 +34,6 @@ end
|
|
58
34
|
|
59
35
|
class BoolNode < Node
|
60
36
|
def initialize(value)
|
61
|
-
@value = false
|
62
37
|
@value = true if value == ScopeManager.true_value
|
63
38
|
@value = false if value == ScopeManager.false_value
|
64
39
|
end
|
@@ -69,32 +44,32 @@ class BoolNode < Node
|
|
69
44
|
end
|
70
45
|
|
71
46
|
class AndNode < Node
|
72
|
-
def initialize(
|
73
|
-
@
|
74
|
-
if @
|
75
|
-
if @
|
76
|
-
@
|
47
|
+
def initialize(lhs, rhs)
|
48
|
+
@lhs, @rhs = lhs, rhs
|
49
|
+
if @rhs.class.method_defined? (:lhs)
|
50
|
+
if @rhs.lhs == nil
|
51
|
+
@rhs.lhs = @lhs.lhs
|
77
52
|
end
|
78
53
|
end
|
79
54
|
end
|
80
55
|
|
81
56
|
def evaluate
|
82
|
-
@
|
57
|
+
@lhs.evaluate && @rhs.evaluate
|
83
58
|
end
|
84
59
|
end
|
85
60
|
|
86
61
|
class OrNode < Node
|
87
|
-
def initialize(
|
88
|
-
@
|
89
|
-
if @
|
90
|
-
if @
|
91
|
-
@
|
62
|
+
def initialize(lhs, rhs)
|
63
|
+
@lhs, @rhs = lhs, rhs
|
64
|
+
if @rhs.class.method_defined? (:lhs)
|
65
|
+
if @rhs.lhs == nil
|
66
|
+
@rhs.lhs = @lhs.lhs
|
92
67
|
end
|
93
68
|
end
|
94
69
|
end
|
95
70
|
|
96
71
|
def evaluate
|
97
|
-
@
|
72
|
+
@lhs.evaluate || @rhs.evaluate
|
98
73
|
end
|
99
74
|
end
|
100
75
|
|
@@ -113,8 +88,8 @@ end
|
|
113
88
|
class CompareNode < Node
|
114
89
|
attr_accessor :lhs, :op, :rhs
|
115
90
|
|
116
|
-
def initialize(
|
117
|
-
@lhs, @op, @rhs =
|
91
|
+
def initialize(lhs, op, rhs)
|
92
|
+
@lhs, @op, @rhs = lhs, op, rhs
|
118
93
|
end
|
119
94
|
|
120
95
|
def evaluate
|
@@ -137,6 +112,16 @@ class BinaryOperationNode < Node
|
|
137
112
|
return @value = @lhs.evaluate.append(@rhs.evaluate)
|
138
113
|
end
|
139
114
|
|
115
|
+
if @rhs.evaluate.is_a?(ArrayNode) && @op == '-'
|
116
|
+
return @value = @rhs.evaluate - @lhs.evaluate
|
117
|
+
elsif @lhs.evaluate.is_a?(Array) && @op == '-'
|
118
|
+
return @lhs.evaluate.reject { |v| v.evaluate == @rhs.evaluate }
|
119
|
+
end
|
120
|
+
|
121
|
+
if @rhs.evaluate.is_a?(ArrayNode) or @rhs.evaluate.is_a?(Array) and @lhs.evaluate.is_a?(Numeric)
|
122
|
+
raise SyntaxError, "You can't subtract and array from a number! #{@lhs.evaluate} #{@op} #{@rhs.name}"
|
123
|
+
end
|
124
|
+
|
140
125
|
if @op == '/'
|
141
126
|
@value = @lhs.evaluate.to_f.send(@op, @rhs.evaluate)
|
142
127
|
else
|
@@ -186,8 +171,16 @@ class ArrayNode < Node
|
|
186
171
|
puts @value.join(', ')
|
187
172
|
end
|
188
173
|
|
189
|
-
def
|
190
|
-
@value.
|
174
|
+
def to_s
|
175
|
+
@value.join(', ')
|
176
|
+
end
|
177
|
+
|
178
|
+
def +(value)
|
179
|
+
@value.append(value)
|
180
|
+
end
|
181
|
+
|
182
|
+
def -(value)
|
183
|
+
@value.reject { |v| v.evaluate == value }
|
191
184
|
end
|
192
185
|
|
193
186
|
def [](index)
|
data/lib/nodes/scopemanager.rb
CHANGED
@@ -56,7 +56,11 @@ class ScopeManager
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def self.add_to_current_scope(name, value)
|
59
|
-
|
59
|
+
if name.is_a?(String)
|
60
|
+
@@scopes[@@scope_lvl][name] = value.evaluate
|
61
|
+
else
|
62
|
+
@@scopes[@@scope_lvl][name.name] = value.evaluate
|
63
|
+
end
|
60
64
|
end
|
61
65
|
|
62
66
|
def self.add_func_to_global_scope(name, value, args)
|
data/lib/nodes/stmtnodes.rb
CHANGED
@@ -94,32 +94,28 @@ class IfNode < Node
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def evaluate
|
97
|
-
|
98
|
-
@value = @node.evaluate
|
99
|
-
else
|
100
|
-
@value = nil
|
101
|
-
end
|
102
|
-
@value
|
97
|
+
@value = @node.evaluate
|
103
98
|
end
|
104
99
|
end
|
105
100
|
|
106
101
|
class ElseifNode < Node
|
107
102
|
attr_accessor :argument
|
103
|
+
|
108
104
|
def initialize(argument, node)
|
109
105
|
@argument, @node = argument, node
|
110
106
|
end
|
107
|
+
|
111
108
|
def evaluate
|
112
|
-
|
113
|
-
@value = @node.evaluate
|
114
|
-
end
|
109
|
+
@value = @node.evaluate
|
115
110
|
end
|
116
111
|
end
|
117
112
|
|
118
113
|
class ElseNode < Node
|
119
114
|
attr_accessor :argument, :node
|
115
|
+
|
120
116
|
def initialize(node)
|
121
117
|
@node = node
|
122
|
-
@argument =
|
118
|
+
@argument = BoolNode.new(ScopeManager.true_value)
|
123
119
|
end
|
124
120
|
|
125
121
|
def evaluate
|
@@ -189,11 +185,18 @@ class PrintNode
|
|
189
185
|
def evaluate
|
190
186
|
if @value.evaluate.is_a?(ArrayNode)
|
191
187
|
print "Array #{@value.name}: "
|
192
|
-
@value.evaluate
|
188
|
+
puts @value.evaluate
|
193
189
|
elsif @value.evaluate.is_a?(Array)
|
194
190
|
print "Array #{@value.name}: "
|
195
191
|
puts @value.evaluate.join(', ')
|
196
192
|
else
|
193
|
+
if @value.evaluate == PrintNode
|
194
|
+
if @value.value.is_a?(VariableCallNode)
|
195
|
+
raise SyntaxError, "You have a duplicate 'print' statement! Printing #{@value.value.name} twice."
|
196
|
+
else
|
197
|
+
raise SyntaxError, "You have a duplicate 'print' statement! Printing #{@value.value} twice."
|
198
|
+
end
|
199
|
+
end
|
197
200
|
puts @value.evaluate
|
198
201
|
end
|
199
202
|
self.class
|