split language and REPL; lib/language → lib/core

This commit is contained in:
Gwenhael Le Moine 2021-12-07 16:03:03 +01:00
parent cfcbcc7ce5
commit ff48cef22a
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
13 changed files with 42 additions and 36 deletions

24
language.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

28
repl.rb
View file

@ -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