diff --git a/language.rb b/language.rb new file mode 100644 index 0000000..6072b85 --- /dev/null +++ b/language.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require './lib/core' +require './lib/dictionary' +require './lib/parser' +require './lib/runner' + +module Rpl + class Language + attr_reader :stack + + def initialize( stack = [] ) + @stack = stack + @dictionary = Rpl::Lang::Dictionary.new + @parser = Rpl::Lang::Parser.new + @runner = Rpl::Lang::Runner.new + end + + def run( input ) + @stack, @dictionary = @runner.run_input( @parser.parse_input( input ), + @stack, @dictionary ) + end + end +end diff --git a/lib/core.rb b/lib/core.rb index 0fdf6e0..a22b1ee 100644 --- a/lib/core.rb +++ b/lib/core.rb @@ -1,14 +1,15 @@ require 'bigdecimal/math' -require_relative './language/branch' -require_relative './language/general' -require_relative './language/mode' -require_relative './language/operations' -require_relative './language/program' -require_relative './language/stack' -require_relative './language/string' -require_relative './language/test' -require_relative './language/time-date' +require_relative './core/branch' +require_relative './core/general' +require_relative './core/mode' +require_relative './core/operations' +require_relative './core/program' +require_relative './core/stack' +require_relative './core/store' +require_relative './core/string' +require_relative './core/test' +require_relative './core/time-date' module Rpl module Lang diff --git a/lib/language/branch.rb b/lib/core/branch.rb similarity index 100% rename from lib/language/branch.rb rename to lib/core/branch.rb diff --git a/lib/language/general.rb b/lib/core/general.rb similarity index 100% rename from lib/language/general.rb rename to lib/core/general.rb diff --git a/lib/language/mode.rb b/lib/core/mode.rb similarity index 100% rename from lib/language/mode.rb rename to lib/core/mode.rb diff --git a/lib/language/operations.rb b/lib/core/operations.rb similarity index 100% rename from lib/language/operations.rb rename to lib/core/operations.rb diff --git a/lib/language/program.rb b/lib/core/program.rb similarity index 78% rename from lib/language/program.rb rename to lib/core/program.rb index 1aeeed9..7233f90 100644 --- a/lib/language/program.rb +++ b/lib/core/program.rb @@ -11,7 +11,8 @@ module Rpl preparsed_input = args[0][:type] == :word ? args[0][:value] : args[0][:value][1..-2] parsed_input = Rpl::Lang::Parser.new.parse_input( preparsed_input ) - stack, _dictionary = Rpl::Lang::Runner.new.run_input( stack, dictionary, parsed_input ) + stack, _dictionary = Rpl::Lang::Runner.new.run_input( parsed_input, + stack, dictionary ) # TODO: check that STO actually updates dictionary stack diff --git a/lib/language/stack.rb b/lib/core/stack.rb similarity index 100% rename from lib/language/stack.rb rename to lib/core/stack.rb diff --git a/lib/language/string.rb b/lib/core/string.rb similarity index 100% rename from lib/language/string.rb rename to lib/core/string.rb diff --git a/lib/language/test.rb b/lib/core/test.rb similarity index 100% rename from lib/language/test.rb rename to lib/core/test.rb diff --git a/lib/language/time-date.rb b/lib/core/time-date.rb similarity index 100% rename from lib/language/time-date.rb rename to lib/core/time-date.rb diff --git a/lib/runner.rb b/lib/runner.rb index 775931d..e083108 100644 --- a/lib/runner.rb +++ b/lib/runner.rb @@ -1,11 +1,11 @@ -# coding: utf-8 +# frozen_string_literal: true module Rpl module Lang class Runner def initialize; end - def run_input( stack, dictionary, input ) + def run_input( input, stack, dictionary ) input.each do |elt| case elt[:type] when :word diff --git a/repl.rb b/repl.rb index 81a35ea..02ebb47 100644 --- a/repl.rb +++ b/repl.rb @@ -3,28 +3,9 @@ require 'readline' -require './lib/core' -require './lib/dictionary' -require './lib/parser' -require './lib/runner' +require './language' module Rpl - class Language - attr_reader :stack - - def initialize - @stack = [] - @dictionary = Dictionary.new - @parser = Parser.new - @runner = Runner.new - end - - def run( input ) - @stack, @dictionary = @runner.run_input( @stack, @dictionary, - @parser.parse_input( input ) ) - end - end - class Repl def initialize @lang = Rpl::Language.new @@ -47,8 +28,7 @@ module Rpl # Remove blank lines from history Readline::HISTORY.pop if input.empty? - @stack, @dictionary = @runner.run_input( @stack, @dictionary, - @parser.parse_input( input ) ) + @lang.run( input ) print_stack end @@ -73,9 +53,9 @@ module Rpl end def print_stack - stack_size = @stack.size + stack_size = @lang.stack.size - @stack.each_with_index do |elt, i| + @lang.stack.each_with_index do |elt, i| puts "#{stack_size - i}: #{format_element( elt )}" end end