Skip to content

Commit

Permalink
test methods that I may eventually make private in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
robertDurst committed Jan 9, 2024
1 parent 58c6eb3 commit 4bb89b7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
60 changes: 60 additions & 0 deletions spec/zodiac/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,64 @@
end
end
end

describe '.parse_literal' do
context 'when parse literal' do
context 'when number' do
it 'returns literal' do
parser = described_class.new('1')

actual = parser.parse_literal
expected = {
kind: 'LITERAL',
value: 1
}

expect(actual).to eq(expected)
end
end

context 'when decimal' do
it 'returns literal' do
parser = described_class.new('1.1')

actual = parser.parse_literal
expected = {
kind: 'LITERAL',
value: 1.1
}

expect(actual).to eq(expected)
end
end

context 'when string' do
it 'returns literal' do
parser = described_class.new('"hello"')

actual = parser.parse_literal
expected = {
kind: 'LITERAL',
value: 'hello'
}

expect(actual).to eq(expected)
end
end

context 'when symbol' do
it 'returns literal' do
parser = described_class.new(':hello')

actual = parser.parse_literal
expected = {
kind: 'LITERAL',
value: :hello
}

expect(actual).to eq(expected)
end
end
end
end
end
21 changes: 16 additions & 5 deletions src/zodiac/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def parse
parse_program
end

private

# PROGRAM : COMPSTMT
def parse_program
cmp_stmts = []
Expand Down Expand Up @@ -303,10 +301,23 @@ def parse_variable
# | SYMBOL
# | STRING
# | STRING2
# | HERE_DOC
# | REGEXP
# | HERE_DOC (TOD)
# | REGEXP (TODO)
def parse_literal
{ kind: 'LITERAL', value: nil }
cur_token = @tokens[@cur_index]

if cur_token[:kind] == 'NUMBER'
{ kind: 'LITERAL', value: cur_token[:value].include?('.') ? cur_token[:value].to_f : cur_token[:value].to_i }
elsif cur_token[:kind] == 'SYMBOL' &&
cur_token[:value].start_with?(':') &&
@tokens[@cur_index + 1][:kind] == 'IDENTIFIER'

{ kind: 'LITERAL', value: @tokens[@cur_index + 1][:value].to_sym }
elsif cur_token[:kind] == 'STRING'
{ kind: 'LITERAL', value: cur_token[:value].gsub('"', '') }
else
raise ParseError, "Expected a literal. Received #{cur_token[:value]}"
end
end
end
end

0 comments on commit 4bb89b7

Please sign in to comment.