parser a static method of Interpreter

This commit is contained in:
Gwenhael Le Moine 2022-02-09 16:33:47 +01:00
parent 6982deaefc
commit 9ebdb707c5
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 27 additions and 27 deletions

View file

@ -14,7 +14,7 @@ module Rpl
load_core
end
def parse( input )
def self.parse( input )
is_numeric = lambda do |elt|
begin
!Float(elt).nil?
@ -106,7 +106,7 @@ module Rpl
def run( input )
@dictionary.add_local_vars_layer
parse( input ).each do |elt|
Interpreter.parse( input ).each do |elt|
case elt[:type]
when :word
command = @dictionary.lookup( elt[:value] )

View file

@ -19,7 +19,7 @@ module Rpl
def from_string( stack, dictionary )
stack, args = Rpl::Lang.stack_extract( stack, [%i[string]] )
stack += Rpl::Interpreter.new.parse( args[0][:value] )
stack += Rpl::Interpreter.parse( args[0][:value] )
[stack, dictionary]
end

View file

@ -7,112 +7,112 @@ require_relative '../interpreter'
class TestParser < Test::Unit::TestCase
def test_number
result = Rpl::Interpreter.new.parse( '1' )
result = Rpl::Interpreter.parse( '1' )
assert_equal [{ value: 1, type: :numeric, base: 10 }], result
end
def test_word
result = Rpl::Interpreter.new.parse( 'dup' )
result = Rpl::Interpreter.parse( 'dup' )
assert_equal [{ value: 'dup', type: :word }], result
end
def test_string
result = Rpl::Interpreter.new.parse( '"test"' )
result = Rpl::Interpreter.parse( '"test"' )
assert_equal [{ value: 'test', type: :string }], result
result = Rpl::Interpreter.new.parse( '" test"' )
result = Rpl::Interpreter.parse( '" test"' )
assert_equal [{ value: ' test', type: :string }], result
result = Rpl::Interpreter.new.parse( '"test "' )
result = Rpl::Interpreter.parse( '"test "' )
assert_equal [{ value: 'test ', type: :string }], result
result = Rpl::Interpreter.new.parse( '" test "' )
result = Rpl::Interpreter.parse( '" test "' )
assert_equal [{ value: ' test ', type: :string }], result
result = Rpl::Interpreter.new.parse( '" « test » "' )
result = Rpl::Interpreter.parse( '" « test » "' )
assert_equal [{ value: ' « test » ', type: :string }], result
end
def test_name
result = Rpl::Interpreter.new.parse( "'test'" )
result = Rpl::Interpreter.parse( "'test'" )
assert_equal [{ value: 'test', type: :name }], result
end
def test_program
result = Rpl::Interpreter.new.parse( '« test »' )
result = Rpl::Interpreter.parse( '« test »' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl::Interpreter.new.parse( '«test »' )
result = Rpl::Interpreter.parse( '«test »' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl::Interpreter.new.parse( '« test»' )
result = Rpl::Interpreter.parse( '« test»' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl::Interpreter.new.parse( '«test»' )
result = Rpl::Interpreter.parse( '«test»' )
assert_equal [{ value: 'test', type: :program }], result
result = Rpl::Interpreter.new.parse( '« test test »' )
result = Rpl::Interpreter.parse( '« test test »' )
assert_equal [{ value: 'test test', type: :program }], result
result = Rpl::Interpreter.new.parse( '« test « test » »' )
result = Rpl::Interpreter.parse( '« test « test » »' )
assert_equal [{ value: 'test « test »', type: :program }], result
result = Rpl::Interpreter.new.parse( '« test "test" test »' )
result = Rpl::Interpreter.parse( '« test "test" test »' )
assert_equal [{ value: 'test "test" test', type: :program }], result
end
def test_number_number
result = Rpl::Interpreter.new.parse( '2 3' )
result = Rpl::Interpreter.parse( '2 3' )
assert_equal [{ value: 2, type: :numeric, base: 10 }, { value: 3, type: :numeric, base: 10 }], result
end
def test_number_number_word
result = Rpl::Interpreter.new.parse( '2 3 +' )
result = Rpl::Interpreter.parse( '2 3 +' )
assert_equal [{ value: 2, type: :numeric, base: 10 },
{ value: 3, type: :numeric, base: 10 },
{ value: '+', type: :word }], result
end
def test_number_string
result = Rpl::Interpreter.new.parse( '4 "test"' )
result = Rpl::Interpreter.parse( '4 "test"' )
assert_equal [{ value: 4, type: :numeric, base: 10 }, { value: 'test', type: :string }], result
end
def test_emptystring
result = Rpl::Interpreter.new.parse( '""' )
result = Rpl::Interpreter.parse( '""' )
assert_equal [{ value: '', type: :string }], result
end
def test_spacestring
result = Rpl::Interpreter.new.parse( '" "' )
result = Rpl::Interpreter.parse( '" "' )
assert_equal [{ value: ' ', type: :string }], result
end
def test_string_spacestring
result = Rpl::Interpreter.new.parse( '"test string" " "' )
result = Rpl::Interpreter.parse( '"test string" " "' )
assert_equal [{ value: 'test string', type: :string },
{ value: ' ', type: :string }], result
end
def test_string_word
result = Rpl::Interpreter.new.parse( '"test string" split' )
result = Rpl::Interpreter.parse( '"test string" split' )
assert_equal [{ value: 'test string', type: :string },
{ value: 'split', type: :word }], result
end
def test_spacestring_word
result = Rpl::Interpreter.new.parse( '" " split' )
result = Rpl::Interpreter.parse( '" " split' )
assert_equal [{ value: ' ', type: :string },
{ value: 'split', type: :word }], result
end
def test_program_name
result = Rpl::Interpreter.new.parse( "« 2 dup * » 'carré' sto" )
result = Rpl::Interpreter.parse( "« 2 dup * » 'carré' sto" )
assert_equal [{ value: '2 dup *', type: :program },
{ value: 'carré', type: :name },