Skip to content

Commit

Permalink
add parse_term w/ specs
Browse files Browse the repository at this point in the history
  • Loading branch information
robertDurst committed Jan 9, 2024
1 parent 4bb89b7 commit 8e82e6c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
41 changes: 27 additions & 14 deletions spec/zodiac/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require './spec/spec_helper'
require './src/zodiac/parser'
require './src/zodiac/parse_error'

describe Zodiac::Parser do
describe '.parse' do
Expand All @@ -15,19 +16,31 @@
expect(actual).to eq(expected)
end
end
end

context 'when simple expression' do
xit 'returns simple expression' do
parser = described_class.new('1 + 2')
describe '.parse_term' do
it 'parses newline' do
parser = described_class.new("\n")

actual = parser.parse
expected = {
kind: 'PROGRAM',
cmp_stmts: []
}
actual = parser.parse_term
expected = { kind: 'TERM', value: "\n" }

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

it 'parses semicolon' do
parser = described_class.new(';')

actual = parser.parse_term
expected = { kind: 'TERM', value: ';' }

expect(actual).to eq(expected)
end

it 'raises error when not newline or semicolon' do
expect do
described_class.new('1').parse_term
end.to raise_error(Zodiac::ParseError)
end
end

Expand All @@ -39,7 +52,7 @@

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

Expand All @@ -53,7 +66,7 @@

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

Expand All @@ -67,7 +80,7 @@

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

Expand All @@ -81,7 +94,7 @@

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

Expand Down
2 changes: 1 addition & 1 deletion src/zodiac/character_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def string_start?(value)
end

def symbol?(value)
'=.:[]{}+-*/%&|^><@~$!?:'.include?(value)
'=.:[]{}+-*/%&|^><@~$!?;'.include?(value)
end

def op_assign_symbol?(value)
Expand Down
11 changes: 8 additions & 3 deletions src/zodiac/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require './src/zodiac/lexer'
require './src/zodiac/parse_error'

module Zodiac
# Base parsing class for the Zodiac language.
Expand Down Expand Up @@ -307,14 +308,18 @@ def parse_literal
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 }
if cur_token[:value].include?('.')
{ kind: 'LITERAL_DECIMAL', value: cur_token[:value].to_f }
else
{ kind: 'LITERAL_NUMBER', value: cur_token[:value].to_i }
end
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 }
{ kind: 'LITERAL_SYMBOL', value: @tokens[@cur_index + 1][:value].to_sym }
elsif cur_token[:kind] == 'STRING'
{ kind: 'LITERAL', value: cur_token[:value].gsub('"', '') }
{ kind: 'LITERAL_STRING', value: cur_token[:value].gsub('"', '') }
else
raise ParseError, "Expected a literal. Received #{cur_token[:value]}"
end
Expand Down

0 comments on commit 8e82e6c

Please sign in to comment.