Class: Yadriggy::Algebra
- Inherits:
-
Object
- Object
- Yadriggy::Algebra
- Defined in:
- lib/yadriggy/algebra.rb
Overview
An interface inspired by object algebra (and tag-less final)
Instance Method Summary collapse
-
#array(elements) ⇒ Object
An array literal.
- #array_ref(array, index) ⇒ Object
-
#array_ref_field(array, index) ⇒ Object
Array reference as L-value.
- #assign(left, op, right) ⇒ Object
- #begin_end(body, rescue_clause) ⇒ Object
- #binary(left, op, right) ⇒ Object
-
#block(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) ⇒ Object
A block.
-
#break_out(op, values) ⇒ Object
break, next, redo, retry.
-
#call(receiver, op, name, args, block_arg, block) ⇒ Object
Method call.
- #class_def(name, superclass, body, rescue_clause) ⇒ Object
-
#command(receiver, op, name, args, block_arg, block) ⇒ Object
Method call without parentheses.
-
#conditional(op, cond, then_exprs, all_elsif, else_exprs) ⇒ Object
if, unless, modifier if/unless, and ternary if (?:) THEN_EXPRS is a thunk.
- #const(name, line_no, column) ⇒ Object
-
#const_path_field(scope, name) ⇒ Object
A constant value in a scope as a L-value.
-
#const_path_ref(scope, name) ⇒ Object
A constant value in a scope, such as Foo::Bar.
-
#define(name, params, optionals, rest_of_params, params_after_rest, keywords, rest_of_keywords, block_param, body, rescue_clause) ⇒ Object
def.
- #dots(left, op, right) ⇒ Object
-
#exprs(result, expr) ⇒ Object
exprs() sequentially processes each expression in a series of expressions.
-
#for_loop(var, set, body) ⇒ Object
for loop.
- #global_variable(name, line_no, column) ⇒ Object
-
#hash(pairs) ⇒ Object
PAIRS is an array of pairs.
- #identifier(name, line_no, column) ⇒ Object
- #identifier_or_call(name, line_no, column) ⇒ Object
-
#initialize(expr) ⇒ Algebra
constructor
A new instance of Algebra.
- #instance_variable(name, line_no, column) ⇒ Object
- #label(name, line_no, column) ⇒ Object
-
#lambda_expr(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) ⇒ Object
A lambda expression.
-
#loop(op, cond, body) ⇒ Object
while, until, and modifier while/until.
- #module_def(name, body, rescue_clause) ⇒ Object
- #name(name, line_no, column) ⇒ Object
- #nil_value ⇒ Object
- #number(value, line_no, column) ⇒ Object
-
#paren(element) ⇒ Object
An expression surrounded with ().
-
#program(elements) ⇒ Object
A whole program.
-
#rescue_end(types, parameter, body, nested_rescue, else_clause, ensure_clause) ⇒ Object
rescue-else-ensure-end.
- #reserved(name, line_no, column) ⇒ Object
-
#return_values(values) ⇒ Object
An expression with return.
- #singular_class_def(name, body, rescue_clause) ⇒ Object
-
#string_interpolation(contents) ⇒ Object
CONTENTS is an array of the results of evaluating each component.
- #string_literal(value, line_no, column) ⇒ Object
- #super_method(expr) ⇒ Object
- #symbol(name, line_no, column) ⇒ Object
- #unary(op, expr) ⇒ Object
- #variable_call(name, line_no, column) ⇒ Object
Constructor Details
#initialize(expr) ⇒ Algebra
Returns a new instance of Algebra.
11 12 13 |
# File 'lib/yadriggy/algebra.rb', line 11 def initialize(expr) EvalAlgebra.new(self).evaluate(expr) end |
Instance Method Details
#array(elements) ⇒ Object
An array literal. ELEMENTS is an array.
139 140 141 |
# File 'lib/yadriggy/algebra.rb', line 139 def array(elements) raise NotImplementedError.new('array') end |
#array_ref(array, index) ⇒ Object
120 121 122 |
# File 'lib/yadriggy/algebra.rb', line 120 def array_ref(array, index) raise NotImplementedError.new('array') end |
#array_ref_field(array, index) ⇒ Object
Array reference as L-value
126 127 128 |
# File 'lib/yadriggy/algebra.rb', line 126 def array_ref_field(array, index) array_ref(array, index) end |
#assign(left, op, right) ⇒ Object
116 117 118 |
# File 'lib/yadriggy/algebra.rb', line 116 def assign(left, op, right) binary(left, op, right) end |
#begin_end(body, rescue_clause) ⇒ Object
227 228 229 |
# File 'lib/yadriggy/algebra.rb', line 227 def begin_end(body, rescue_clause) raise NotImplementedError.new('begin_end') end |
#binary(left, op, right) ⇒ Object
108 109 110 |
# File 'lib/yadriggy/algebra.rb', line 108 def binary(left, op, right) raise NotImplementedError.new('binary') end |
#block(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) ⇒ Object
A block. BODY is a thunk.
204 205 206 207 |
# File 'lib/yadriggy/algebra.rb', line 204 def block(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) raise NotImplementedError.new('block') end |
#break_out(op, values) ⇒ Object
break, next, redo, retry.
191 192 193 |
# File 'lib/yadriggy/algebra.rb', line 191 def break_out(op, values) raise NotImplementedError.new('break_out') end |
#call(receiver, op, name, args, block_arg, block) ⇒ Object
Method call. ARGS is an array. BLOCK is a thunk.
154 155 156 |
# File 'lib/yadriggy/algebra.rb', line 154 def call(receiver, op, name, args, block_arg, block) raise NotImplementedError.new('call') end |
#class_def(name, superclass, body, rescue_clause) ⇒ Object
245 246 247 |
# File 'lib/yadriggy/algebra.rb', line 245 def class_def(name, superclass, body, rescue_clause) raise NotImplementedError.new('class_def') end |
#command(receiver, op, name, args, block_arg, block) ⇒ Object
Method call without parentheses. ARGS is an array. BLOCK is a thunk.
161 162 163 |
# File 'lib/yadriggy/algebra.rb', line 161 def command(receiver, op, name, args, block_arg, block) call(receiver, op, name, args, block_arg, block) end |
#conditional(op, cond, then_exprs, all_elsif, else_exprs) ⇒ Object
if, unless, modifier if/unless, and ternary if (?:) THEN_EXPRS is a thunk. ALL_ELSIF is an array of pairs of elsif condition and its body. Both are thunks. ELSE_EXPRS is a thiuk or nil.
171 172 173 |
# File 'lib/yadriggy/algebra.rb', line 171 def conditional(op, cond, then_exprs, all_elsif, else_exprs) raise NotImplementedError.new('conditional') end |
#const(name, line_no, column) ⇒ Object
35 36 37 |
# File 'lib/yadriggy/algebra.rb', line 35 def const(name, line_no, column) name(name, line_no, column) end |
#const_path_field(scope, name) ⇒ Object
A constant value in a scope as a L-value.
100 101 102 |
# File 'lib/yadriggy/algebra.rb', line 100 def const_path_field(scope, name) const_path_ref(scope, name) end |
#const_path_ref(scope, name) ⇒ Object
A constant value in a scope, such as Foo::Bar.
94 95 96 |
# File 'lib/yadriggy/algebra.rb', line 94 def const_path_ref(scope, name) raise NotImplementedError.new('const_path_ref') end |
#define(name, params, optionals, rest_of_params, params_after_rest, keywords, rest_of_keywords, block_param, body, rescue_clause) ⇒ Object
def.
BODY and RESCUE_CLAUSE are thunks.
235 236 237 238 239 |
# File 'lib/yadriggy/algebra.rb', line 235 def define(name, params, optionals, rest_of_params, params_after_rest, keywords, rest_of_keywords, block_param, body, rescue_clause) raise NotImplementedError.new('define') end |
#dots(left, op, right) ⇒ Object
112 113 114 |
# File 'lib/yadriggy/algebra.rb', line 112 def dots(left, op, right) binary(left, op, right) end |
#exprs(result, expr) ⇒ Object
exprs() sequentially processes each expression in a series of expressions. So, for example, { e1, e2, …, e_n } is processed by exprs(.. exprs(exprs(nil, e1), e2).., e_n). In other words, it is by [e1, e2, …, e_n].inject(nil) {|r,e| exprs(r, evaluate(e))}.
RESULT specifies the result of the previous expression. It is nil if EXPR is the first expression.
77 78 79 |
# File 'lib/yadriggy/algebra.rb', line 77 def exprs(result, expr) raise NotImplementedError.new('exprs') end |
#for_loop(var, set, body) ⇒ Object
for loop. BODY is a thunk.
185 186 187 |
# File 'lib/yadriggy/algebra.rb', line 185 def for_loop(var, set, body) raise NotImplementedError.new('for_loop') end |
#global_variable(name, line_no, column) ⇒ Object
47 48 49 |
# File 'lib/yadriggy/algebra.rb', line 47 def global_variable(name, line_no, column) name(name, line_no, column) end |
#hash(pairs) ⇒ Object
PAIRS is an array of pairs. Each pair is an array where the first element is a key and the second element is a value.
147 148 149 |
# File 'lib/yadriggy/algebra.rb', line 147 def hash(pairs) raise NotImplementedError.new('hash') end |
#identifier(name, line_no, column) ⇒ Object
27 28 29 |
# File 'lib/yadriggy/algebra.rb', line 27 def identifier(name, line_no, column) identifier_or_call(name, line_no, column) end |
#identifier_or_call(name, line_no, column) ⇒ Object
23 24 25 |
# File 'lib/yadriggy/algebra.rb', line 23 def identifier_or_call(name, line_no, column) name(name, line_no, column) end |
#instance_variable(name, line_no, column) ⇒ Object
51 52 53 |
# File 'lib/yadriggy/algebra.rb', line 51 def instance_variable(name, line_no, column) name(name, line_no, column) end |
#label(name, line_no, column) ⇒ Object
39 40 41 |
# File 'lib/yadriggy/algebra.rb', line 39 def label(name, line_no, column) name(name, line_no, column) end |
#lambda_expr(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) ⇒ Object
A lambda expression. BODY is a thunk.
212 213 214 215 216 |
# File 'lib/yadriggy/algebra.rb', line 212 def lambda_expr(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) block(params, optionals, rest_params, params_after_rest, keywords, rest_of_keywords, block_param, body) end |
#loop(op, cond, body) ⇒ Object
while, until, and modifier while/until. COND and BODY are thunks.
178 179 180 |
# File 'lib/yadriggy/algebra.rb', line 178 def loop(op, cond, body) raise NotImplementedError.new('loop') end |
#module_def(name, body, rescue_clause) ⇒ Object
241 242 243 |
# File 'lib/yadriggy/algebra.rb', line 241 def module_def(name, body, rescue_clause) raise NotImplementedError.new('module_def') end |
#name(name, line_no, column) ⇒ Object
19 20 21 |
# File 'lib/yadriggy/algebra.rb', line 19 def name(name, line_no, column) raise NotImplementedError.new('name') end |
#nil_value ⇒ Object
15 16 17 |
# File 'lib/yadriggy/algebra.rb', line 15 def nil_value() raise NotImplementedError.new('nil_value') end |
#number(value, line_no, column) ⇒ Object
63 64 65 |
# File 'lib/yadriggy/algebra.rb', line 63 def number(value, line_no, column) raise NotImplementedError.new('number') end |
#paren(element) ⇒ Object
An expression surrounded with ().
132 133 134 |
# File 'lib/yadriggy/algebra.rb', line 132 def paren(element) raise NotImplementedError.new('paren') end |
#program(elements) ⇒ Object
A whole program.
ELEMENTS is the result of processing the program elements.
257 258 259 |
# File 'lib/yadriggy/algebra.rb', line 257 def program(elements) raise NotImplementedError.new('program') end |
#rescue_end(types, parameter, body, nested_rescue, else_clause, ensure_clause) ⇒ Object
rescue-else-ensure-end. BODY, NESTED_RESCUE, ELSE_CLAUSE, and ENSURE_CLAUSE are thunks.
222 223 224 225 |
# File 'lib/yadriggy/algebra.rb', line 222 def rescue_end(types, parameter, body, nested_rescue, else_clause, ensure_clause) raise NotImplementedError.new('rescue_end') end |
#reserved(name, line_no, column) ⇒ Object
31 32 33 |
# File 'lib/yadriggy/algebra.rb', line 31 def reserved(name, line_no, column) name(name, line_no, column) end |
#return_values(values) ⇒ Object
An expression with return.
197 198 199 |
# File 'lib/yadriggy/algebra.rb', line 197 def return_values(values) raise NotImplementedError.new('return_values') end |
#singular_class_def(name, body, rescue_clause) ⇒ Object
249 250 251 |
# File 'lib/yadriggy/algebra.rb', line 249 def singular_class_def(name, body, rescue_clause) raise NotImplementedError.new('singular_class_def') end |
#string_interpolation(contents) ⇒ Object
CONTENTS is an array of the results of evaluating each component.
84 85 86 |
# File 'lib/yadriggy/algebra.rb', line 84 def string_interpolation(contents) raise NotImplementedError.new('string_interpolation') end |
#string_literal(value, line_no, column) ⇒ Object
88 89 90 |
# File 'lib/yadriggy/algebra.rb', line 88 def string_literal(value, line_no, column) raise NotImplementedError.new('string_literal') end |
#super_method(expr) ⇒ Object
59 60 61 |
# File 'lib/yadriggy/algebra.rb', line 59 def super_method(expr) raise NotImplementedError.new('super_method') end |
#symbol(name, line_no, column) ⇒ Object
43 44 45 |
# File 'lib/yadriggy/algebra.rb', line 43 def symbol(name, line_no, column) raise NotImplementedError.new('symbol') end |
#unary(op, expr) ⇒ Object
104 105 106 |
# File 'lib/yadriggy/algebra.rb', line 104 def unary(op, expr) raise NotImplementedError.new('unary') end |
#variable_call(name, line_no, column) ⇒ Object
55 56 57 |
# File 'lib/yadriggy/algebra.rb', line 55 def variable_call(name, line_no, column) identifier_or_call(name, line_no, column) end |