Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created December 23, 2008 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanphx/39233 to your computer and use it in GitHub Desktop.
Save evanphx/39233 to your computer and use it in GitHub Desktop.
module Compiler::Plugins
class StaticConstantSet < Compiler::Plugin
register Compiler::Node::Call
def self.run(compiler, call)
if !call.object and
call.method == :static and
call.arguments.size == 1 and
call.arguments.first.is? Compiler::Node::ConstSet
cf = call.arguments.first
begin
value = constant_value(cf.value)
rescue RuntimeError
return call
end
compiler.plugin_data[:constants][cf.name] = value
return cf
end
call
end
def self.constant_value(node)
case node
when Compiler::Node::False
false
when Compiler::Node::True
true
when Compiler::Node::Nil
nil
when Compiler::Node::NumberLiteral
return node.value
else
raise "Not a constant value"
end
end
end
class StaticConstantRead < Compiler::Plugin
register Compiler::Node::ConstFind
def self.run(compiler, const_find)
if val = compiler.plugin_data[:constants][const_find.name]
new(val)
else
const_find
end
end
def initialize(val)
@value = val
end
def bytecode(g)
g.push @value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment