Class: C64Asm::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/c64asm/basic.rb

Overview

C64 Basic block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program, origin = 0x801, align = true) ⇒ Basic

Create a new C64 basic block

Raises:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/c64asm/basic.rb', line 18

def initialize(program, origin = 0x801, align = true)
  raise BasicError, 'Program has to be a string' unless program.instance_of? String
  raise BasicError, 'Origin has to be a fixnum' unless origin.instance_of? Fixnum
  raise BasicError, 'Origin out of range' unless (origin >= 0 and origin <= 65535)

  @basic = program
  @code = Block.new

  @code.push(Align.new(origin)) if align
  @code.push(Data.new(parse(origin)))
end

Instance Attribute Details

#basicObject (readonly)

Returns the value of attribute basic.



15
16
17
# File 'lib/c64asm/basic.rb', line 15

def basic
  @basic
end

#codeObject (readonly)

Returns the value of attribute code.



15
16
17
# File 'lib/c64asm/basic.rb', line 15

def code
  @code
end

Instance Method Details

#parse(addr) ⇒ Object (private)

Parse basic code string given the origin address



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/c64asm/basic.rb', line 35

def parse(addr)
  state = :nextline
  bytes = []
  line = []
  token = ''

  @basic.upcase.each_char do |c|
    if c == "\n"
      state = :nextline
      next
    end

    if state == :nextline
      unless token.empty?
        token.codepoints.each{|p| bytes += [PETSCII[p]]}
        token = ''
      end

      unless bytes.empty?
        bytes += [0]
        addr += 1
      end

      addr += 2
      bytes += [addr.ls_byte, addr.ms_byte]
      state = :linenum
    end

    if state == :linenum
      if c.match(/\d/)
        token += c
        addr += 1
      else
        raise BasicError, 'Each line has to start with a line number' if token.empty?

        lineno = token.to_i
        raise BasicError, 'Line number out of range' unless (lineno >= 0 and lineno <= 65535)

        bytes += [lineno.ls_byte, lineno.ms_byte]
        addr += 2
        state = :out
        token = ''

        next if c == ' '
      end
    end

    if state == :out
      if c == '"'
        bytes += [PETSCII['"'.ord]]
        addr += 1
        state = :in

        next
      elsif c == ' '
        unless token.empty?
          token.codepoints.each{|p| bytes += [PETSCII[p]]}
          token = ''
        end

        bytes += [PETSCII[' '.ord]]
        addr += 1
      elsif BASIC.has_key? c
        token.codepoints.each{|p| bytes += [PETSCII[p]]} unless token.empty?
        bytes += [BASIC[c]]
        addr += 1
        token = ''
      else
        raise BasicError, 'Unknown character' unless PETSCII.has_key? c.codepoints.to_a.first

        token += c
        addr += 1

        if BASIC.has_key? token
          bytes += [BASIC[token]]
          token = ''
        end
      end
    end

    if state == :in
      raise BasicError, 'Unknown character' unless PETSCII.has_key? c.codepoints.to_a.first

      token += c
      addr += 1
      state = :out if c == '"'
    end
  end

  unless token.empty?
    token.codepoints.each{|p| bytes += [PETSCII[p]]}
    bytes += [0]
  end

  bytes += [0, 0]
end

#to_sObject

Return a pretty string representation



31
# File 'lib/c64asm/basic.rb', line 31

def to_s; "<Basic: #{@basic.lines.to_a.length}>"; end