From 77afa709f466072dd22d1dea8acbdbed9f820934 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Tue, 22 Feb 2022 15:53:22 +0100 Subject: [PATCH] allow comments, using '#' --- README.md | 1 - lib/rpl/interpreter.rb | 17 +++++++++++++++++ spec/parser_spec.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78e6340..c46cade 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ To run REPL locally: `ruby -Ilib bin/rpl` To run the test suite: `find ./spec/ -name \*.rb -exec ruby -Ilib {} \;` # TODO-list - * allow comments, using '#' * pseudo filesystem: subdir for variables * UI toolkit (based on https://github.com/AndyObtiva/glimmer-dsl-libui ?) diff --git a/lib/rpl/interpreter.rb b/lib/rpl/interpreter.rb index 3075a6a..00c60d7 100644 --- a/lib/rpl/interpreter.rb +++ b/lib/rpl/interpreter.rb @@ -41,6 +41,23 @@ class Interpreter end end + unless input.index("\n").nil? + input = input + .split("\n") + .map do |line| + comment_begin_index = line.index('#') + + if comment_begin_index.nil? + line + elsif comment_begin_index == 0 + '' + else + line[0..(comment_begin_index - 1)] + end + end + .join(' ') + end + splitted_input = input.split(' ') # 2-passes: diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 243928e..d2ba809 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -155,4 +155,31 @@ class TestParser < MiniTest::Test { value: 'sto', type: :word }], result end + + def test_with_multiline + result = Rpl.new.parse( "« 2 + +dup * » + + 'carré' sto" ) + + assert_equal [{ value: '2 dup *', type: :program }, + { value: 'carré', type: :name }, + { value: 'sto', type: :word }], + result + end + + def test_with_comments + result = Rpl.new.parse( "« 2 #deux +# on duplique le deux +dup * » + +# on va STOcker ce programme dans la variable 'carré' + 'carré' sto" ) + + assert_equal [{ value: '2 dup *', type: :program }, + { value: 'carré', type: :name }, + { value: 'sto', type: :word }], + result + end end