made a pass with rubocop
Signed-off-by: Gwenhael Le Moine <gwenhael.le.moine@gmail.com>
This commit is contained in:
parent
953eabc15c
commit
3666c19602
33 changed files with 65 additions and 84 deletions
8
Rakefile
8
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
|
||||
|
|
6
bin/rpl
6
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]
|
||||
|
||||
|
|
|
@ -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 )
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -25,7 +25,7 @@ module Types
|
|||
end
|
||||
|
||||
def ==( other )
|
||||
other.class == RplBoolean and
|
||||
other.class == RplBoolean &&
|
||||
other.value == value
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -22,7 +22,7 @@ module Types
|
|||
end
|
||||
|
||||
def ==( other )
|
||||
other.class == RplString and
|
||||
other.class == RplString &&
|
||||
other.value == value
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'],
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# coding: utf-8
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'minitest/autorun'
|
||||
|
|
Loading…
Reference in a new issue