Module: Generator

Included in:
SpecialGiggle
Defined in:
lib/special-giggle/generator.rb

Defined Under Namespace

Classes: GeneratorError, Token

Constant Summary collapse

BinaryOps =
{
  :Plus         => 50,
  :Minus        => 50,
  :Asterisk     => 60,
  :Slash        => 60,
  :Percent      => 60,
  :Bar          => 10,
  :Ampersand    => 30,
  :Caret        => 20,
  :LeftShift    => 40,
  :RightShift   => 40,
}
UnaryOps =
{
  :Tilde        => 70,
  :Negative     => 80,
}
Vars =
Array.new

Class Method Summary collapse

Class Method Details

.generate(token) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/special-giggle/generator.rb', line 110

def self.generate(token)
  array = Array.new
  token.child.each do |statement|
    array << generate_statement(statement)
  end

  output = %{
.section .data
__print_fmt:
.string "%ld\\n"

.section .text
.globl main
main:
pushq %rbp
movq %rsp, %rbp
subq <VARIABLE>, %rsp

<CODE>
movq %rbp, %rsp
popq %rbp
movq $0, %rax
retq
}
  text = ""
  array.flatten.each { |ops| ops.to_code.each { |line| text += " " * 2 + line + "\n" } }

  Vars.length.times do |i|
    regex = "(?<![\\w])#{Vars[i]}(?![\\w])"
    text.gsub!(/#{regex}/, i > 0 ? "#{8*i}(%rsp)" : "(%rsp)")
  end
  output.gsub('<VARIABLE>', "$#{Vars.length * 8}").gsub('<CODE>', text)
end

.generate_expression(token) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/special-giggle/generator.rb', line 73

def self.generate_expression(token)
  if token.type == :Number
    t = var_temp
    [t, [Token.new(:Move, token.child[0], t)]]
  elsif token.type == :Variable
    [token.child[0], []]
  elsif token.type == :Bracket
    var, array = generate_expression(token.child[0])
    t = var_temp
    [t, [array, Token.new(:Move, var, t)]]
  elsif UnaryOps.include?(token.type)
    var, array = generate_expression(token.child[0])
    t = var_temp
    [t, [array, Token.new(token.type, var, t)]]
  else
    var1, array1 = generate_expression(token.child[0])
    var2, array2 = generate_expression(token.child[1])
    t = var_temp
    [t, [array1, array2, Token.new(token.type, [var1, var2], t)]]
  end
end

.generate_statement(token) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/special-giggle/generator.rb', line 95

def self.generate_statement(token)
  array = Array.new
  if token.type == :Print
    var, garray = generate_expression(token.child[0])
    array << garray unless garray.empty?
    array << Token.new(:Print, var)
  elsif token.type == :Move
    Vars << token.child[0].child[0] unless Vars.include?(token.child[0].child[0])
    var, garray = generate_expression(token.child[1])
    array << garray unless garray.empty?
    array << Token.new(:Move, var, token.child[0].child[0])
  end
  array
end

.var_tempObject



66
67
68
69
70
71
# File 'lib/special-giggle/generator.rb', line 66

def self.var_temp
  #var = "#{8 * Vars.length}(%rsp)"
  var = "tmp_#{Vars.length}"
  Vars << var unless Vars.include?(var)
  var
end