harmonization with Crystal syntax

This commit is contained in:
Gwenhael Le Moine 2021-10-31 09:18:34 +01:00
parent 67632c6fb4
commit edccbf1bda
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 45 additions and 41 deletions

View file

@ -7,52 +7,52 @@ class String
end
def parse_input( input )
splitted_input = input.split(' ')
splitted_input = input.split(" ")
parsed_tree = []
regrouping = false
splitted_input.each do |elt|
next if elt.length == 1 && elt[0] == '»'
next if elt.length == 1 && elt[0] == "»"
parsed_entry = { 'value' => elt }
parsed_entry = { "value" => elt }
if regrouping
parsed_entry = parsed_tree.pop
parsed_entry['value'] = "#{parsed_entry['value']} #{elt}".strip
parsed_entry["value"] = "#{parsed_entry["value"]} #{elt}".strip
else
parsed_entry['type'] = case elt[0]
when '«'
'PROGRAM'
when '"'
'STRING'
when '\''
'NAME'
parsed_entry["type"] = case elt[0]
when "«"
"PROGRAM"
when "\""
"STRING"
when "'"
"NAME"
else
if elt.numeric?
'NUMBER'
"NUMBER"
else
'WORD' # TODO: if word isn't known then it's a NAME
"WORD" # TODO: if word isn"t known then it"s a NAME
end
end
parsed_entry['value'] = elt[1..] if %W[PROGRAM STRING NAME].include?( parsed_entry['type'] )
parsed_entry["value"] = elt[1..] if %W[PROGRAM STRING NAME].include?( parsed_entry["type"] )
end
regrouping = ( (parsed_entry['type'] == 'NAME' && elt[-1] != '\'') ||
(parsed_entry['type'] == 'STRING' && elt[-1] != '"') ||
(parsed_entry['type'] == 'PROGRAM' && elt[-1] != '»') )
regrouping = ( (parsed_entry["type"] == "NAME" && elt[-1] != "'") ||
(parsed_entry["type"] == "STRING" && elt[-1] != "\"") ||
(parsed_entry["type"] == "PROGRAM" && elt[-1] != "»") )
parsed_entry['value'] = if ( (parsed_entry['type'] == 'NAME' && elt[-1] == '\'') ||
(parsed_entry['type'] == 'STRING' && elt[-1] == '"') ||
(parsed_entry['type'] == 'PROGRAM' && elt[-1] == '»') )
(parsed_entry['value'][..-2]).strip
elsif parsed_entry['type'] == 'NUMBER'
parsed_entry['value'].to_f
elsif parsed_entry['type'] == 'WORD'
parsed_entry['value']
parsed_entry["value"] = if ( (parsed_entry["type"] == "NAME" && elt[-1] == "'") ||
(parsed_entry["type"] == "STRING" && elt[-1] == "\"") ||
(parsed_entry["type"] == "PROGRAM" && elt[-1] == "»") )
(parsed_entry["value"][..-2]).strip
elsif parsed_entry["type"] == "NUMBER"
parsed_entry["value"].to_f
elsif parsed_entry["type"] == "WORD"
parsed_entry["value"]
else
parsed_entry['value']
parsed_entry["value"]
end
parsed_tree << parsed_entry

View file

@ -1,9 +1,9 @@
# coding: utf-8
# frozen_string_literal: true
require 'readline'
require "readline"
require_relative "./lib/parser"
require "./lib/parser.rb"
def run_REPL( stack )
Readline.completion_proc = proc do |s|
@ -39,7 +39,7 @@ end
def display_stack( stack )
stack_size = stack.size
stack.each_with_index { |elt, i| puts "#{stack_size - i}: #{elt['value']}"}
stack.each_with_index { |elt, i| puts "#{stack_size - i}: #{elt["value"]}"}
stack
end

View file

@ -1,50 +1,54 @@
# coding: utf-8
require_relative "../lib/parser"
require 'test/unit'
require "test/unit"
require_relative "../lib/parser"
class TestParser < Test::Unit::TestCase
def test_parse_input_number
result = parse_input( "1" )
assert_equal [ { 'value' => 1, 'type' => 'NUMBER' } ], result
assert_equal [ { "value" => 1, "type" => "NUMBER" } ], result
end
def test_parse_input_word
result = parse_input( "dup" )
assert_equal [ { 'value' => "dup", 'type' => 'WORD' } ], result
assert_equal [ { "value" => "dup", "type" => "WORD" } ], result
end
def test_parse_input_string
result = parse_input( "\"test\"" )
assert_equal [ { 'value' => "test", 'type' => 'STRING' } ], result
assert_equal [ { "value" => "test", "type" => "STRING" } ], result
end
def test_parse_input_name
result = parse_input( "'test'" )
assert_equal [ { 'value' => "test", 'type' => 'NAME' } ], result
assert_equal [ { "value" => "test", "type" => "NAME" } ], result
end
def test_parse_input_program
result = parse_input( "« test »" )
assert_equal [ { 'value' => "test", 'type' => 'PROGRAM' } ], result
assert_equal [ { "value" => "test", "type" => "PROGRAM" } ], result
result = parse_input( "«test »" )
assert_equal [ { 'value' => "test", 'type' => 'PROGRAM' } ], result
assert_equal [ { "value" => "test", "type" => "PROGRAM" } ], result
result = parse_input( "« test»" )
assert_equal [ { "value" => "test", "type" => "PROGRAM" } ], result
result = parse_input( "« test test »" )
assert_equal [ { 'value' => "test test", 'type' => 'PROGRAM' } ], result
assert_equal [ { "value" => "test test", "type" => "PROGRAM" } ], result
result = parse_input( "« test \"test\" test »" )
assert_equal [ { 'value' => "test \"test\" test", 'type' => 'PROGRAM' } ], result
assert_equal [ { "value" => "test \"test\" test", "type" => "PROGRAM" } ], result
end
def test_parse_input_number_number
result = parse_input( "2 3" )
assert_equal [ { 'value' => 2, 'type' => 'NUMBER' }, { 'value' => 3, 'type' => 'NUMBER' } ], result
assert_equal [ { "value" => 2, "type" => "NUMBER" }, { "value" => 3, "type" => "NUMBER" } ], result
end
def test_parse_input_number_string
result = parse_input( "4 \"test\"" )
assert_equal [ { 'value' => 4, 'type' => 'NUMBER' }, { 'value' => "test", 'type' => 'STRING' } ], result
assert_equal [ { "value" => 4, "type" => "NUMBER" }, { "value" => "test", "type" => "STRING" } ], result
end
end