s_calc 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/calc +41 -0
  3. data/lib/calc/calc.rb +450 -0
  4. data/lib/calc.rb +1 -0
  5. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 71e6238052352f288d04dd872eccba4f61fe316b2a350e9170f49068319ce534
4
+ data.tar.gz: 28d214b0aa8762938797b309cd2718065e0de072c9207b94f223e9a0c805c282
5
+ SHA512:
6
+ metadata.gz: f25daea5118727a36f0e5a12d1c73c54d882f15e613345d7dd669143de68e7dc2c226c369eb977ab98b93439d218460135719334af16f2268ebc6487c7e0e0e2
7
+ data.tar.gz: 21ee599c61b51656237c8056065460b67de6ccd546c9d68a584bda1b428656e43ca696ed2023a326fcf7a7b8bd88bfe003bcb358439afbe9128743fac02bec1c
data/bin/calc ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'readline'
4
+ require 'calc'
5
+
6
+ def usage
7
+ $stderr.print "Usage: calc [expression]\n"
8
+ usage_possible_calculation
9
+ end
10
+
11
+ def usage_possible_calculation
12
+ $stderr.print "Possible calculation:\n"
13
+ $stderr.print " Arithmetic such as +, -, *, /, (, ), - (unary minus) and ** (power).\n"
14
+ $stderr.print " Functions such as sqrt, sin, cos, tan, asin, acos, atan, exp, log and log10.\n"
15
+ $stderr.print " Variables (any alphabetical word) and the special variable 'v' that is the former result.\n"
16
+ end
17
+
18
+ calc = Calc.new
19
+ if ARGV[0] =~ /--help|-h/
20
+ usage
21
+ elsif ARGV.size == 1
22
+ begin
23
+ print "#{calc.run(ARGV[0])}\n"
24
+ rescue StandardError => evar
25
+ print "#{evar.message[1..-1]}\n" # remove the newline at the beginning of the message
26
+ end
27
+ else
28
+ while buf = Readline.readline("calc > ", true)
29
+ break if buf == "quit" || buf == "exit" || buf == "q"
30
+ if buf =="help"
31
+ $stderr.print "quit/q/exit => quit the program.\n"
32
+ usage_possible_calculation
33
+ else
34
+ begin
35
+ print "#{calc.run(buf)}\n"
36
+ rescue => evar
37
+ print "#{evar.message[1..-1]}\n" # remove the newline at the beginning of the message
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/calc/calc.rb ADDED
@@ -0,0 +1,450 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.6.2
4
+ # from Racc grammar file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+
10
+ require 'strscan'
11
+ include Math
12
+
13
+ class Calc < Racc::Parser
14
+
15
+ module_eval(<<'...end calc.y/module_eval...', 'calc.y', 45)
16
+
17
+ def initialize
18
+ @table = {}
19
+ end
20
+
21
+ def run(s)
22
+ @tokens = []
23
+ lex(s)
24
+ do_parse
25
+ end
26
+
27
+ def lex(s)
28
+ ss = StringScanner.new(s)
29
+ until ss.eos?
30
+ if ss.scan(/sqrt|sin|cos|tan|asin|acos|atan|exp|log|sqrt|PI|E|v/)
31
+ @tokens << [ss[0].to_sym, ss[0]]
32
+ elsif ss.scan(/[[:alpha:]]+/)
33
+ @tokens << [:ID, ss[0]]
34
+ elsif ss.scan(/[[:digit:]]+(\.[[:digit:]]*)?/)
35
+ @tokens << [:NUM, ss[0].to_f]
36
+ elsif ss.scan(/\*\*/)
37
+ @tokens << [:POWER,ss[0]]
38
+ elsif ss.scan(/[+\-*\/()=;]/)
39
+ @tokens << [ss[0],ss[0]]
40
+ elsif ss.scan(/\s+/)
41
+ # ignore spaces
42
+ else
43
+ raise "Unexpected character."
44
+ end
45
+ end
46
+ @tokens << [false, nil] # end mark
47
+ end
48
+
49
+ def next_token
50
+ @tokens.shift
51
+ end
52
+
53
+ ...end calc.y/module_eval...
54
+ ##### State transition tables begin ###
55
+
56
+ racc_action_table = [
57
+ 24, 25, 22, 23, 24, 25, 22, 23, 24, 25,
58
+ 22, 23, 24, 25, 22, 23, 24, 25, 22, 23,
59
+ 21, 48, 24, 25, 26, 59, 24, 25, 29, 60,
60
+ 31, 32, 33, 61, 34, 35, 36, 62, 24, 25,
61
+ 22, 23, 24, 25, 22, 23, 24, 25, 22, 23,
62
+ 24, 25, 22, 23, 24, 25, 22, 23, 37, 63,
63
+ 38, 39, 40, 64, 41, 29, 29, 65, nil, nil,
64
+ nil, 66, nil, nil, nil, 67, 24, 25, 22, 23,
65
+ nil, 4, 3, 7, 8, 9, 10, 11, 12, 13,
66
+ 14, 15, 16, 17, 18, 19, 20, 68, 6, 4,
67
+ 28, 7, 8, 9, 10, 11, 12, 13, 14, 15,
68
+ 16, 17, 18, 19, 20, nil, 6, 4, 28, 7,
69
+ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
70
+ 18, 19, 20, nil, 6, 4, 28, 7, 8, 9,
71
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
72
+ 20, nil, 6, 4, 28, 7, 8, 9, 10, 11,
73
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, nil,
74
+ 6, 4, 28, 7, 8, 9, 10, 11, 12, 13,
75
+ 14, 15, 16, 17, 18, 19, 20, nil, 6, 4,
76
+ 28, 7, 8, 9, 10, 11, 12, 13, 14, 15,
77
+ 16, 17, 18, 19, 20, nil, 6, 4, 28, 7,
78
+ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
79
+ 18, 19, 20, nil, 6, 4, 28, 7, 8, 9,
80
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
81
+ 20, nil, 6, 4, 28, 7, 8, 9, 10, 11,
82
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, nil,
83
+ 6, 4, 28, 7, 8, 9, 10, 11, 12, 13,
84
+ 14, 15, 16, 17, 18, 19, 20, nil, 6, 4,
85
+ 28, 7, 8, 9, 10, 11, 12, 13, 14, 15,
86
+ 16, 17, 18, 19, 20, nil, 6, 4, 28, 7,
87
+ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
88
+ 18, 19, 20, nil, 6, 4, 28, 7, 8, 9,
89
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
90
+ 20, nil, 6, 4, 28, 7, 8, 9, 10, 11,
91
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, nil,
92
+ 6, 4, 28, 7, 8, 9, 10, 11, 12, 13,
93
+ 14, 15, 16, 17, 18, 19, 20, nil, 6, 4,
94
+ 28, 7, 8, 9, 10, 11, 12, 13, 14, 15,
95
+ 16, 17, 18, 19, 20, nil, 6, 28, 7, 8,
96
+ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
97
+ 19, 20, nil, 6, 28, 7, 8, 9, 10, 11,
98
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, nil,
99
+ 6, 24, 25, 22, 23, 24, 25, 22, 23 ]
100
+
101
+ racc_action_check = [
102
+ 30, 30, 30, 30, 49, 49, 49, 49, 50, 50,
103
+ 50, 50, 51, 51, 51, 51, 52, 52, 52, 52,
104
+ 1, 30, 42, 42, 3, 49, 43, 43, 5, 50,
105
+ 10, 11, 12, 51, 13, 14, 15, 52, 53, 53,
106
+ 53, 53, 54, 54, 54, 54, 55, 55, 55, 55,
107
+ 56, 56, 56, 56, 57, 57, 57, 57, 16, 53,
108
+ 17, 18, 19, 54, 21, 27, 47, 55, nil, nil,
109
+ nil, 56, nil, nil, nil, 57, 58, 58, 58, 58,
110
+ nil, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111
+ 0, 0, 0, 0, 0, 0, 0, 58, 0, 6,
112
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
113
+ 6, 6, 6, 6, 6, nil, 6, 22, 22, 22,
114
+ 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
115
+ 22, 22, 22, nil, 22, 23, 23, 23, 23, 23,
116
+ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
117
+ 23, nil, 23, 24, 24, 24, 24, 24, 24, 24,
118
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, nil,
119
+ 24, 25, 25, 25, 25, 25, 25, 25, 25, 25,
120
+ 25, 25, 25, 25, 25, 25, 25, nil, 25, 26,
121
+ 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
122
+ 26, 26, 26, 26, 26, nil, 26, 31, 31, 31,
123
+ 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
124
+ 31, 31, 31, nil, 31, 32, 32, 32, 32, 32,
125
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
126
+ 32, nil, 32, 33, 33, 33, 33, 33, 33, 33,
127
+ 33, 33, 33, 33, 33, 33, 33, 33, 33, nil,
128
+ 33, 34, 34, 34, 34, 34, 34, 34, 34, 34,
129
+ 34, 34, 34, 34, 34, 34, 34, nil, 34, 35,
130
+ 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
131
+ 35, 35, 35, 35, 35, nil, 35, 36, 36, 36,
132
+ 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
133
+ 36, 36, 36, nil, 36, 37, 37, 37, 37, 37,
134
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
135
+ 37, nil, 37, 38, 38, 38, 38, 38, 38, 38,
136
+ 38, 38, 38, 38, 38, 38, 38, 38, 38, nil,
137
+ 38, 39, 39, 39, 39, 39, 39, 39, 39, 39,
138
+ 39, 39, 39, 39, 39, 39, 39, nil, 39, 40,
139
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
140
+ 40, 40, 40, 40, 40, nil, 40, 4, 4, 4,
141
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
142
+ 4, 4, nil, 4, 29, 29, 29, 29, 29, 29,
143
+ 29, 29, 29, 29, 29, 29, 29, 29, 29, nil,
144
+ 29, 2, 2, 2, 2, 46, 46, 46, 46 ]
145
+
146
+ racc_action_pointer = [
147
+ 74, 20, 417, 1, 379, 26, 92, nil, nil, nil,
148
+ 6, 7, 8, 10, 11, 12, 34, 36, 37, 38,
149
+ nil, 64, 110, 128, 146, 164, 182, 63, nil, 396,
150
+ -4, 200, 218, 236, 254, 272, 290, 308, 326, 344,
151
+ 362, nil, 18, 22, nil, nil, 421, 64, nil, 0,
152
+ 4, 8, 12, 34, 38, 42, 46, 50, 72, nil,
153
+ nil, nil, nil, nil, nil, nil, nil, nil, nil ]
154
+
155
+ racc_action_default = [
156
+ -26, -26, -1, -11, -26, -8, -26, -12, -13, -14,
157
+ -26, -26, -26, -26, -26, -26, -26, -26, -26, -26,
158
+ -25, -26, -26, -26, -26, -26, -26, -7, -11, -26,
159
+ -26, -26, -26, -26, -26, -26, -26, -26, -26, -26,
160
+ -26, 69, -3, -4, -5, -6, -2, -9, -10, -26,
161
+ -26, -26, -26, -26, -26, -26, -26, -26, -26, -15,
162
+ -16, -17, -18, -19, -20, -21, -22, -23, -24 ]
163
+
164
+ racc_goto_table = [
165
+ 2, 1, 27, nil, nil, nil, 30, nil, nil, nil,
166
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
167
+ nil, nil, 42, 43, 44, 45, 46, 47, nil, nil,
168
+ nil, 49, 50, 51, 52, 53, 54, 55, 56, 57,
169
+ 58 ]
170
+
171
+ racc_goto_check = [
172
+ 2, 1, 3, nil, nil, nil, 2, nil, nil, nil,
173
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
174
+ nil, nil, 2, 2, 2, 2, 2, 3, nil, nil,
175
+ nil, 2, 2, 2, 2, 2, 2, 2, 2, 2,
176
+ 2 ]
177
+
178
+ racc_goto_pointer = [
179
+ nil, 1, 0, -2 ]
180
+
181
+ racc_goto_default = [
182
+ nil, nil, nil, 5 ]
183
+
184
+ racc_reduce_table = [
185
+ 0, 0, :racc_error,
186
+ 1, 27, :_reduce_1,
187
+ 3, 27, :_reduce_2,
188
+ 3, 28, :_reduce_3,
189
+ 3, 28, :_reduce_4,
190
+ 3, 28, :_reduce_5,
191
+ 3, 28, :_reduce_6,
192
+ 2, 28, :_reduce_7,
193
+ 1, 28, :_reduce_none,
194
+ 3, 29, :_reduce_9,
195
+ 3, 29, :_reduce_10,
196
+ 1, 29, :_reduce_11,
197
+ 1, 29, :_reduce_none,
198
+ 1, 29, :_reduce_13,
199
+ 1, 29, :_reduce_14,
200
+ 4, 29, :_reduce_15,
201
+ 4, 29, :_reduce_16,
202
+ 4, 29, :_reduce_17,
203
+ 4, 29, :_reduce_18,
204
+ 4, 29, :_reduce_19,
205
+ 4, 29, :_reduce_20,
206
+ 4, 29, :_reduce_21,
207
+ 4, 29, :_reduce_22,
208
+ 4, 29, :_reduce_23,
209
+ 4, 29, :_reduce_24,
210
+ 1, 29, :_reduce_25 ]
211
+
212
+ racc_reduce_n = 26
213
+
214
+ racc_shift_n = 69
215
+
216
+ racc_token_table = {
217
+ false => 0,
218
+ :error => 1,
219
+ :POWER => 2,
220
+ :UMINUS => 3,
221
+ "*" => 4,
222
+ "/" => 5,
223
+ "+" => 6,
224
+ "-" => 7,
225
+ :ID => 8,
226
+ :NUM => 9,
227
+ :PI => 10,
228
+ :E => 11,
229
+ :sqrt => 12,
230
+ :sin => 13,
231
+ :cos => 14,
232
+ :tan => 15,
233
+ :asin => 16,
234
+ :acos => 17,
235
+ :atan => 18,
236
+ :exp => 19,
237
+ :log => 20,
238
+ :log10 => 21,
239
+ :v => 22,
240
+ "=" => 23,
241
+ "(" => 24,
242
+ ")" => 25 }
243
+
244
+ racc_nt_base = 26
245
+
246
+ racc_use_result_var = false
247
+
248
+ Racc_arg = [
249
+ racc_action_table,
250
+ racc_action_check,
251
+ racc_action_default,
252
+ racc_action_pointer,
253
+ racc_goto_table,
254
+ racc_goto_check,
255
+ racc_goto_default,
256
+ racc_goto_pointer,
257
+ racc_nt_base,
258
+ racc_reduce_table,
259
+ racc_token_table,
260
+ racc_shift_n,
261
+ racc_reduce_n,
262
+ racc_use_result_var ]
263
+
264
+ Racc_token_to_s_table = [
265
+ "$end",
266
+ "error",
267
+ "POWER",
268
+ "UMINUS",
269
+ "\"*\"",
270
+ "\"/\"",
271
+ "\"+\"",
272
+ "\"-\"",
273
+ "ID",
274
+ "NUM",
275
+ "PI",
276
+ "E",
277
+ "sqrt",
278
+ "sin",
279
+ "cos",
280
+ "tan",
281
+ "asin",
282
+ "acos",
283
+ "atan",
284
+ "exp",
285
+ "log",
286
+ "log10",
287
+ "v",
288
+ "\"=\"",
289
+ "\"(\"",
290
+ "\")\"",
291
+ "$start",
292
+ "statement",
293
+ "expression",
294
+ "primary" ]
295
+
296
+ Racc_debug_parser = false
297
+
298
+ ##### State transition tables end #####
299
+
300
+ # reduce 0 omitted
301
+
302
+ module_eval(<<'.,.,', 'calc.y', 11)
303
+ def _reduce_1(val, _values)
304
+ @v = val[0]
305
+ end
306
+ .,.,
307
+
308
+ module_eval(<<'.,.,', 'calc.y', 12)
309
+ def _reduce_2(val, _values)
310
+ @v = @table[val[0]] = val[2]
311
+ end
312
+ .,.,
313
+
314
+ module_eval(<<'.,.,', 'calc.y', 13)
315
+ def _reduce_3(val, _values)
316
+ val[0] + val[2]
317
+ end
318
+ .,.,
319
+
320
+ module_eval(<<'.,.,', 'calc.y', 14)
321
+ def _reduce_4(val, _values)
322
+ val[0] - val[2]
323
+ end
324
+ .,.,
325
+
326
+ module_eval(<<'.,.,', 'calc.y', 15)
327
+ def _reduce_5(val, _values)
328
+ val[0] * val[2]
329
+ end
330
+ .,.,
331
+
332
+ module_eval(<<'.,.,', 'calc.y', 16)
333
+ def _reduce_6(val, _values)
334
+ if (val[2] != 0.0) then val[0] / val[2] else raise("Division by zero.") end
335
+ end
336
+ .,.,
337
+
338
+ module_eval(<<'.,.,', 'calc.y', 17)
339
+ def _reduce_7(val, _values)
340
+ -(val[1])
341
+ end
342
+ .,.,
343
+
344
+ # reduce 8 omitted
345
+
346
+ module_eval(<<'.,.,', 'calc.y', 19)
347
+ def _reduce_9(val, _values)
348
+ val [0] ** val[2]
349
+ end
350
+ .,.,
351
+
352
+ module_eval(<<'.,.,', 'calc.y', 20)
353
+ def _reduce_10(val, _values)
354
+ val[1]
355
+ end
356
+ .,.,
357
+
358
+ module_eval(<<'.,.,', 'calc.y', 21)
359
+ def _reduce_11(val, _values)
360
+ if @table[val[0]] then @table[val[0]] else raise("#{val[0]} not found.") end
361
+ end
362
+ .,.,
363
+
364
+ # reduce 12 omitted
365
+
366
+ module_eval(<<'.,.,', 'calc.y', 23)
367
+ def _reduce_13(val, _values)
368
+ Math::PI
369
+ end
370
+ .,.,
371
+
372
+ module_eval(<<'.,.,', 'calc.y', 24)
373
+ def _reduce_14(val, _values)
374
+ Math::E
375
+ end
376
+ .,.,
377
+
378
+ module_eval(<<'.,.,', 'calc.y', 25)
379
+ def _reduce_15(val, _values)
380
+ sqrt(val[2] )
381
+ end
382
+ .,.,
383
+
384
+ module_eval(<<'.,.,', 'calc.y', 26)
385
+ def _reduce_16(val, _values)
386
+ sin(val[2] )
387
+ end
388
+ .,.,
389
+
390
+ module_eval(<<'.,.,', 'calc.y', 27)
391
+ def _reduce_17(val, _values)
392
+ cos(val[2] )
393
+ end
394
+ .,.,
395
+
396
+ module_eval(<<'.,.,', 'calc.y', 28)
397
+ def _reduce_18(val, _values)
398
+ tan(val[2] )
399
+ end
400
+ .,.,
401
+
402
+ module_eval(<<'.,.,', 'calc.y', 29)
403
+ def _reduce_19(val, _values)
404
+ asin(val[2] )
405
+ end
406
+ .,.,
407
+
408
+ module_eval(<<'.,.,', 'calc.y', 30)
409
+ def _reduce_20(val, _values)
410
+ acos(val[2] )
411
+ end
412
+ .,.,
413
+
414
+ module_eval(<<'.,.,', 'calc.y', 31)
415
+ def _reduce_21(val, _values)
416
+ atan(val[2] )
417
+ end
418
+ .,.,
419
+
420
+ module_eval(<<'.,.,', 'calc.y', 32)
421
+ def _reduce_22(val, _values)
422
+ exp(val[2] )
423
+ end
424
+ .,.,
425
+
426
+ module_eval(<<'.,.,', 'calc.y', 33)
427
+ def _reduce_23(val, _values)
428
+ log(val[2] )
429
+ end
430
+ .,.,
431
+
432
+ module_eval(<<'.,.,', 'calc.y', 34)
433
+ def _reduce_24(val, _values)
434
+ log10(val[2] )
435
+ end
436
+ .,.,
437
+
438
+ module_eval(<<'.,.,', 'calc.y', 35)
439
+ def _reduce_25(val, _values)
440
+ @v
441
+ end
442
+ .,.,
443
+
444
+ def _reduce_none(val, _values)
445
+ val[0]
446
+ end
447
+
448
+ end # class Calc
449
+
450
+ #
data/lib/calc.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'calc/calc.rb'
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s_calc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Toshio Sekiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: CUI Scientific calculator
14
+ email: lxboyjp@gmail.com
15
+ executables:
16
+ - calc
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/calc
21
+ - lib/calc.rb
22
+ - lib/calc/calc.rb
23
+ homepage: https://github.com/ToshioCP/calc
24
+ licenses:
25
+ - GPL-3.0
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.4.10
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Scientific Calculator
46
+ test_files: []