diff --git a/Rakefile b/Rakefile index edb856f..daf07b1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,13 +1,15 @@ +# frozen_string_literal: true + require 'rake/testtask' Rake::TestTask.new do |t| - t.pattern = "spec/*_spec.rb" + t.pattern = 'spec/*_spec.rb' end task :gem do - sh "gem build rpl.gemspec" + sh 'gem build rpl.gemspec' end task :run do - sh "ruby -Ilib bin/rpl" + sh 'ruby -Ilib bin/rpl' end diff --git a/bin/rpl b/bin/rpl index 8139356..32c183d 100755 --- a/bin/rpl +++ b/bin/rpl @@ -78,7 +78,7 @@ class RplRepl end def persistence_filename - persistence_dir = ENV['XDG_DATA_HOME'] + persistence_dir = ENV.fetch('XDG_DATA_HOME', nil) persistence_dir ||= '~/.local/share' persistence_dir += '/rpl.rb' @@ -88,7 +88,7 @@ end options = { run_REPL: false, persistence: true, live_persistence: true, - persistence_filename: persistence_filename, + persistence_filename:, files: [], programs: [], verbosity: :critical } @@ -155,7 +155,7 @@ options[:programs].each do |program| end # third launch REPL if (explicitely or implicitely) asked -RplRepl.new( interpreter: interpreter ).run! if options[:run_REPL] +RplRepl.new( interpreter: ).run! if options[:run_REPL] interpreter.persist_state if options[:persistence] diff --git a/lib/rpl.rb b/lib/rpl.rb index ec671d1..ef9363a 100644 --- a/lib/rpl.rb +++ b/lib/rpl.rb @@ -15,7 +15,7 @@ class Rpl < Interpreter dictionary: Dictionary.new, persistence_filename: nil, live_persistence: true ) - super( stack: stack, dictionary: dictionary ) + super( stack:, dictionary: ) @persistence_filename = persistence_filename @live_persistence = live_persistence @@ -37,9 +37,7 @@ class Rpl < Interpreter def persist_state return if @persistence_filename.nil? - File.open( @persistence_filename, 'w' ) do |persistence_file| - persistence_file.write "#{export_vars}\n#{export_stack}" - end + File.write(@persistence_filename, "#{export_vars}\n#{export_stack}") end def run!( input ) diff --git a/lib/rpl/dictionary.rb b/lib/rpl/dictionary.rb index 730026d..c1a49ae 100644 --- a/lib/rpl/dictionary.rb +++ b/lib/rpl/dictionary.rb @@ -13,9 +13,9 @@ class Dictionary def add_word!( names, category, help, implementation ) names.each do |name| - @words[ name ] = { category: category, - help: help, - implementation: implementation } + @words[ name ] = { category:, + help:, + implementation: } end end diff --git a/lib/rpl/parser.rb b/lib/rpl/parser.rb index 143b876..eeba339 100644 --- a/lib/rpl/parser.rb +++ b/lib/rpl/parser.rb @@ -30,7 +30,7 @@ class Parser .join(' ') end - splitted_input = input.split(' ') + splitted_input = input.split # 2-passes: # 1. regroup strings, lists, complexes, names and programs @@ -90,7 +90,7 @@ class Parser end # 2. parse - parsed_input = regrouped_input.map do |element| + regrouped_input.map do |element| if RplBoolean.can_parse?( element ) Types.new_object( RplBoolean, element ) elsif RplNumeric.can_parse?( element ) @@ -107,7 +107,5 @@ class Parser Types.new_object( RplName, element ) end end - - parsed_input end end diff --git a/lib/rpl/types/boolean.rb b/lib/rpl/types/boolean.rb index ca861fd..78de046 100644 --- a/lib/rpl/types/boolean.rb +++ b/lib/rpl/types/boolean.rb @@ -25,7 +25,7 @@ module Types end def ==( other ) - other.class == RplBoolean and + other.class == RplBoolean && other.value == value end end diff --git a/lib/rpl/types/complex.rb b/lib/rpl/types/complex.rb index 026fc7c..8b8d610 100644 --- a/lib/rpl/types/complex.rb +++ b/lib/rpl/types/complex.rb @@ -20,20 +20,14 @@ module Types end def self.can_parse?( value ) - # we systematicalyl trim enclosing () - value = value[1..-2] if value.is_a?( String ) && value[0] == '(' && value[-1] == ')' + # we systematically trim enclosing () + value = value[1..-2] if value.is_a?( String ) && (value[0] == '(') && (value[-1] == ')') - begin - Complex( value ) - rescue ArgumentError - return false - end - - true + !Complex( value, exception: false ).nil? end def ==( other ) - other.class == RplComplex and + other.class == RplComplex && other.value == value end end diff --git a/lib/rpl/types/list.rb b/lib/rpl/types/list.rb index 23e1aeb..5d2840d 100644 --- a/lib/rpl/types/list.rb +++ b/lib/rpl/types/list.rb @@ -12,7 +12,7 @@ module Types @value = if value.instance_of?( Array ) value else - # we systematicalyl trim enclosing { } + # we systematically trim enclosing { } Parser.parse( value[2..-3] ) end end @@ -22,12 +22,13 @@ module Types end def self.can_parse?( value ) - value.instance_of?( Array ) or - value[0..1] == '{ ' && value[-2..] == ' }' + value.instance_of?( Array ) || + ( value[0..1] == '{ ' && + value[-2..] == ' }' ) end def ==( other ) - other.class == RplList and + other.class == RplList && other.value == value end end diff --git a/lib/rpl/types/name.rb b/lib/rpl/types/name.rb index 445e4d2..8122c71 100644 --- a/lib/rpl/types/name.rb +++ b/lib/rpl/types/name.rb @@ -22,14 +22,14 @@ module Types end def self.can_parse?( value ) - ( value.length > 2 and value[0] == "'" and value[-1] == "'" ) or - ( value != "''" and !value.match?(/^[0-9']+$/) and + ( value.length > 2 && value[0] == "'" && value[-1] == "'" ) || + ( value != "''" && !value.match?(/^[0-9']+$/) && # it's not any other type [RplBoolean, RplList, RplProgram, RplString, RplNumeric].reduce( true ) { |memo, type_class| memo && !type_class.can_parse?( value ) } ) end def ==( other ) - other.class == RplName and + other.class == RplName && other.value == value end end diff --git a/lib/rpl/types/numeric.rb b/lib/rpl/types/numeric.rb index d0cde95..8d05e74 100644 --- a/lib/rpl/types/numeric.rb +++ b/lib/rpl/types/numeric.rb @@ -32,9 +32,7 @@ module Types @base = value.base when BigDecimal @value = value - when Integer - @value = BigDecimal( value, @@precision ) - when Float + when Integer, Float @value = BigDecimal( value, @@precision ) when String begin @@ -94,19 +92,19 @@ module Types end def self.can_parse?( value ) - [RplNumeric, BigDecimal, Integer, Float].include?( value.class ) or - ( value.is_a?( String ) and ( value.match?(/^-?[0-9]*\.?[0-9]+$/) or - value.match?(/^-?∞$/) or - value.match?(/^0b[0-1]+$/) or - value.match?(/^0o[0-7]+$/) or - value.match?(/^0x[0-9a-f]+$/) or - ( value.match?(/^[0-9]+b[0-9a-z]+$/) and + [RplNumeric, BigDecimal, Integer, Float].include?( value.class ) || + ( value.is_a?( String ) && ( value.match?(/^-?[0-9]*\.?[0-9]+$/) || + value.match?(/^-?∞$/) || + value.match?(/^0b[0-1]+$/) || + value.match?(/^0o[0-7]+$/) || + value.match?(/^0x[0-9a-f]+$/) || + ( value.match?(/^[0-9]+b[0-9a-z]+$/) && value.split('_').first.to_i <= 36 ) ) ) end def ==( other ) - other.class == RplNumeric and - other.base == base and + other.class == RplNumeric && + other.base == base && other.value == value end end diff --git a/lib/rpl/types/program.rb b/lib/rpl/types/program.rb index 5641107..283a36f 100644 --- a/lib/rpl/types/program.rb +++ b/lib/rpl/types/program.rb @@ -7,7 +7,7 @@ module Types def initialize( value ) raise RplTypeError unless self.class.can_parse?( value ) - # we systematicalyl trim enclosing « » + # we systematically trim enclosing « » @value = value[2..-3] # TODO: parse each element ? end @@ -16,11 +16,14 @@ module Types end def self.can_parse?( value ) - value.length > 4 && value[0..1] == '« ' && value[-2..-1] == ' »' && !value[2..-3].strip.empty? + value.length > 4 && + value[0..1] == '« ' && + value[-2..] == ' »' && + !value[2..-3].strip.empty? end def ==( other ) - other.class == RplProgram and + other.class == RplProgram && other.value == value end end diff --git a/lib/rpl/types/string.rb b/lib/rpl/types/string.rb index 9318765..d289986 100644 --- a/lib/rpl/types/string.rb +++ b/lib/rpl/types/string.rb @@ -22,7 +22,7 @@ module Types end def ==( other ) - other.class == RplString and + other.class == RplString && other.value == value end end diff --git a/lib/rpl/words/logarithm.rb b/lib/rpl/words/logarithm.rb index d184157..084a975 100644 --- a/lib/rpl/words/logarithm.rb +++ b/lib/rpl/words/logarithm.rb @@ -10,7 +10,7 @@ module RplLang category = 'Logs on reals and complexes' - @dictionary.add_word!( ['ℇ', 'e'], + @dictionary.add_word!( %w[ℇ e], category, '( … -- ℇ ) push ℇ', proc do diff --git a/lib/rpl/words/operations-reals-complexes.rb b/lib/rpl/words/operations-reals-complexes.rb index b981b33..91e8f87 100644 --- a/lib/rpl/words/operations-reals-complexes.rb +++ b/lib/rpl/words/operations-reals-complexes.rb @@ -30,7 +30,7 @@ module RplLang new_list = if args[1].instance_of?( RplList ) RplList.new( args[0].to_s ).value.concat( args[1].value ) else - RplList.new( args[0].to_s ).value.concat( [args[1]] ) + RplList.new( args[0].to_s ).value.push(args[1]) end RplList.new( "{ #{new_list.join(' ')} }" ) @@ -47,9 +47,9 @@ module RplLang elsif args[0].instance_of?( RplString ) RplString.new( if args[1].instance_of?( RplString ) || args[1].instance_of?( RplName ) - "\"#{args[0].value}#{args[1].value}\"" - else - "\"#{args[0].value}#{args[1]}\"" + "\"#{args[0].value}#{args[1].value}\"" + else + "\"#{args[0].value}#{args[1]}\"" end ) elsif args[0].instance_of?( RplName ) @@ -189,11 +189,11 @@ module RplLang args = stack_extract( [[RplNumeric]] ) @stack << RplNumeric.new( if args[0].value.positive? - 1 - elsif args[0].value.negative? - -1 - else - 0 + 1 + elsif args[0].value.negative? + -1 + else + 0 end ) end ) end diff --git a/lib/rpl/words/operations-reals.rb b/lib/rpl/words/operations-reals.rb index 7532eaf..755fbc6 100644 --- a/lib/rpl/words/operations-reals.rb +++ b/lib/rpl/words/operations-reals.rb @@ -25,7 +25,7 @@ module RplLang proc do args = stack_extract( [[RplNumeric], [RplNumeric]] ) - @stack << RplNumeric.new( 100.0 * ( args[0].value / args[1].value ), args[1].base ) + @stack << RplNumeric.new( ( args[0].value / args[1].value ) * 100.0, args[1].base ) end ) @dictionary.add_word!( ['mod'], diff --git a/lib/rpl/words/string-list.rb b/lib/rpl/words/string-list.rb index cdeaa2e..da0c6d6 100644 --- a/lib/rpl/words/string-list.rb +++ b/lib/rpl/words/string-list.rb @@ -17,10 +17,10 @@ module RplLang args = stack_extract( [[RplString, RplList]] ) @stack << if args[0].is_a?( RplString ) - Types.new_object( RplString, "\"#{args[0].value.reverse}\"" ) - else - Types.new_object( args[0].class, "{ #{args[0].value.reverse.join(' ')} }" ) - end + Types.new_object( RplString, "\"#{args[0].value.reverse}\"" ) + else + Types.new_object( args[0].class, "{ #{args[0].value.reverse.join(' ')} }" ) + end end ) end end diff --git a/lib/rpl/words/time-date.rb b/lib/rpl/words/time-date.rb index 29afb20..59b2fdd 100644 --- a/lib/rpl/words/time-date.rb +++ b/lib/rpl/words/time-date.rb @@ -30,7 +30,7 @@ module RplLang proc do ticks_since_epoch = Time.utc( 1, 1, 1 ).to_i * 10_000_000 now = Time.now - @stack << Types.new_object( RplNumeric, now.to_i * 10_000_000 + now.nsec / 100 - ticks_since_epoch ) + @stack << Types.new_object( RplNumeric, (now.to_i * 10_000_000) + (now.nsec / 100) - ticks_since_epoch ) end ) end end diff --git a/lib/rpl/words/trig.rb b/lib/rpl/words/trig.rb index 7752a01..18681a5 100644 --- a/lib/rpl/words/trig.rb +++ b/lib/rpl/words/trig.rb @@ -12,7 +12,7 @@ module RplLang category = 'Trig on reals and complexes' - @dictionary.add_word!( ['𝛑', 'pi'], + @dictionary.add_word!( %w[𝛑 pi], category, '( … -- 𝛑 ) push 𝛑', proc do diff --git a/rpl.gemspec b/rpl.gemspec index 4ed2330..5657f9e 100644 --- a/rpl.gemspec +++ b/rpl.gemspec @@ -49,4 +49,5 @@ Gem::Specification.new do |s| s.executables << 'rpl' s.required_ruby_version = '> 2.7' + s.metadata['rubygems_mfa_required'] = 'true' end diff --git a/spec/types_spec.rb b/spec/types_spec.rb index d543d08..01ebbc2 100644 --- a/spec/types_spec.rb +++ b/spec/types_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_branch_spec.rb b/spec/words_branch_spec.rb index 412af8e..284a190 100644 --- a/spec/words_branch_spec.rb +++ b/spec/words_branch_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_filesystem_spec.rb b/spec/words_filesystem_spec.rb index 3b62dd7..1ef2ebf 100644 --- a/spec/words_filesystem_spec.rb +++ b/spec/words_filesystem_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' @@ -17,7 +16,7 @@ class TestLanguageFileSystem < MiniTest::Test interpreter.run! 'eval vars' assert_equal [Types.new_object( RplNumeric, 27 ), - Types.new_object( RplList, [ Types.new_object( RplName, 'trrr' ) ] )], + Types.new_object( RplList, [Types.new_object( RplName, 'trrr' )] )], interpreter.stack end @@ -25,7 +24,7 @@ class TestLanguageFileSystem < MiniTest::Test interpreter = Rpl.new interpreter.run! '"spec/test.rpl" feval vars' assert_equal [Types.new_object( RplNumeric, 27 ), - Types.new_object( RplList, [ Types.new_object( RplName, 'trrr' ) ] )], + Types.new_object( RplList, [Types.new_object( RplName, 'trrr' )] )], interpreter.stack end diff --git a/spec/words_list_spec.rb b/spec/words_list_spec.rb index 6a7c0d5..525e7b3 100644 --- a/spec/words_list_spec.rb +++ b/spec/words_list_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_logarithm_spec.rb b/spec/words_logarithm_spec.rb index bf2d7f1..7de1142 100644 --- a/spec/words_logarithm_spec.rb +++ b/spec/words_logarithm_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_operations-complexes_spec.rb b/spec/words_operations-complexes_spec.rb index 416ac21..fd36415 100644 --- a/spec/words_operations-complexes_spec.rb +++ b/spec/words_operations-complexes_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_operations-reals-complexes_spec.rb b/spec/words_operations-reals-complexes_spec.rb index 6c06098..dd86d41 100644 --- a/spec/words_operations-reals-complexes_spec.rb +++ b/spec/words_operations-reals-complexes_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_operations-reals_spec.rb b/spec/words_operations-reals_spec.rb index 7f27b3e..c93b099 100644 --- a/spec/words_operations-reals_spec.rb +++ b/spec/words_operations-reals_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_stack_spec.rb b/spec/words_stack_spec.rb index 327be07..a4dfcdd 100644 --- a/spec/words_stack_spec.rb +++ b/spec/words_stack_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_string-list_spec.rb b/spec/words_string-list_spec.rb index cfd226c..0df3867 100644 --- a/spec/words_string-list_spec.rb +++ b/spec/words_string-list_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_string_spec.rb b/spec/words_string_spec.rb index 9c7ae82..e223950 100644 --- a/spec/words_string_spec.rb +++ b/spec/words_string_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_test_spec.rb b/spec/words_test_spec.rb index b75384e..c4ad101 100644 --- a/spec/words_test_spec.rb +++ b/spec/words_test_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_time-date_spec.rb b/spec/words_time-date_spec.rb index 6a334c1..da8fdfc 100644 --- a/spec/words_time-date_spec.rb +++ b/spec/words_time-date_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun' diff --git a/spec/words_trig_spec.rb b/spec/words_trig_spec.rb index 137eaa2..0103926 100644 --- a/spec/words_trig_spec.rb +++ b/spec/words_trig_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 # frozen_string_literal: true require 'minitest/autorun'