allow comments, using '#'

This commit is contained in:
Gwenhael Le Moine 2022-02-22 15:53:22 +01:00
parent 777659da1c
commit 77afa709f4
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 44 additions and 1 deletions

View file

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

View file

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

View file

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