Skip to content

Commit a037faf

Browse files
committed
Implement C_LABEL, C_GOTO, C_IF in the Parser
1 parent 9dbc71a commit a037faf

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/parser.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ class Parser
22
C_ARITHMETIC = Object.new
33
C_POP = Object.new
44
C_PUSH = Object.new
5+
C_LABEL = Object.new
6+
C_GOTO = Object.new
7+
C_IF = Object.new
58

69
def initialize(input)
710
@lines = split_lines(input)
@@ -16,10 +19,17 @@ def advance
1619
end
1720

1821
def command_type
19-
if current.start_with? 'pop'
22+
case current.split(' ').first
23+
when 'pop'
2024
C_POP
21-
elsif current.start_with? 'push'
25+
when 'push'
2226
C_PUSH
27+
when 'label'
28+
C_LABEL
29+
when 'goto'
30+
C_GOTO
31+
when 'if-goto'
32+
C_IF
2333
else
2434
C_ARITHMETIC
2535
end

spec/unit/parser_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
context "when the current command is #{command}" do
173173
let(:input) { command }
174174

175-
it "returns #{type}", pending: [:C_LABEL, :C_GOTO, :C_IF, :C_FUNCTION, :C_CALL, :C_RETURN].include?(type) do
175+
it "returns #{type}", pending: [:C_FUNCTION, :C_CALL, :C_RETURN].include?(type) do
176176
expect(parser.command_type).to eq Parser.const_get(type)
177177
end
178178
end

0 commit comments

Comments
 (0)