Permalink
Browse files

parser: add tests for values

  • Loading branch information...
1 parent d377c1a commit 94eec3ad0718f84130a176a9c02ea002afb816ff @rlane committed Apr 5, 2012
Showing with 89 additions and 2 deletions.
  1. +9 −0 lib/scasm/isa.rb
  2. +14 −2 lib/scasm/parser.rb
  3. +66 −0 test/test_parser.rb
View
@@ -34,4 +34,13 @@ module SCASM
:J => 7,
}
+SPECIAL_REGISTERS = {
+ :pop => 0x18,
+ :peek => 0x19,
+ :push => 0x1a,
+ :sp => 0x1b,
+ :pc => 0x1c,
+ :o => 0x1d,
+}
+
end
View
@@ -14,7 +14,7 @@ def self.declare(map, start, string)
end
end
- HEX_RE = /^0x[0-9a-fA-F]+$/
+ HEX_RE = /^0[xX][0-9a-fA-F]+$/
INT_RE = /^\d+$/
REG_RE = /^[A-Z]+$/
LABEL_RE = /^[a-z]+$/
@@ -52,6 +52,18 @@ def dehex(token)
def parse_value token
token = dehex(token)
+ sym = token.downcase.to_sym
+
+ if SPECIAL_REGISTERS.member? sym
+ return case sym
+ when :pop then Pop.new
+ when :peek then Peek.new
+ when :push then Push.new
+ when :sp then SP.new
+ when :pc then PC.new
+ when :o then O.new
+ end
+ end
case token
when INT_RE
@@ -66,7 +78,7 @@ def parse_value token
when INT_RE
ImmediateMemory.new inner.to_i
when REG_RE
- Register.new inner.to_sym
+ RegisterMemory.new inner.to_sym
when LABEL_RE
ImmediateLabel.new inner
when INDIRECT_OFFSET_RE
View
@@ -34,4 +34,70 @@ def test_instructions
expect [Instruction.new(:jsr, Register.new(:A), nil)]
check "jsr A"
end
+
+ def test_reg
+ expect [
+ Instruction.new(:set, Register.new(:A), Register.new(:B)),
+ Instruction.new(:set, Register.new(:C), Register.new(:X)),
+ Instruction.new(:set, Register.new(:Y), Register.new(:Z)),
+ Instruction.new(:set, Register.new(:I), Register.new(:J)),
+ ]
+
+ check <<-EOS
+ set A, B
+ set C, X
+ set Y, Z
+ set I, J
+ EOS
+ end
+
+ def test_regmem
+ expect [
+ Instruction.new(:set, RegisterMemory.new(:I), RegisterMemory.new(:J)),
+ ]
+
+ check <<-EOS
+ set [I], [J]
+ EOS
+ end
+
+ def test_iregmem
+ expect [
+ Instruction.new(:set, OffsetRegisterMemory.new(:I, 1), OffsetRegisterMemory.new(:J, 42)),
+ ]
+
+ check <<-EOS
+ set [1+I], [0x2a+J]
+ EOS
+ end
+
+ def test_misc
+ expect [Instruction.new(:set, Pop.new, Pop.new)]
+ check 'set pop, pop'
+
+ expect [Instruction.new(:set, Peek.new, Peek.new)]
+ check 'set peek, peek'
+
+ expect [Instruction.new(:set, Push.new, Push.new)]
+ check 'set push, push'
+
+ expect [Instruction.new(:set, SP.new, SP.new)]
+ check 'set sp, sp'
+
+ expect [Instruction.new(:set, PC.new, PC.new)]
+ check 'set pc, pc'
+
+ expect [Instruction.new(:set, O.new, O.new)]
+ check 'set o, o'
+ end
+
+ def test_imem
+ expect [Instruction.new(:set, ImmediateMemory.new(1), ImmediateMemory.new(42))]
+ check 'set [1], [0x2a]'
+ end
+
+ def test_imm
+ expect [Instruction.new(:set, Immediate.new(1), Immediate.new(42))]
+ check 'set 1, 0x2a'
+ end
end

0 comments on commit 94eec3a

Please sign in to comment.