allow comments, using '#'
This commit is contained in:
parent
777659da1c
commit
77afa709f4
3 changed files with 44 additions and 1 deletions
|
@ -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 ?)
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue