Browse files

Add optimize option to orange script.

Handle parser error.
  • Loading branch information...
1 parent 6cab059 commit fa1f10e879253b152dcf3fb17522bb3037d95faf @macournoyer committed Oct 23, 2008
Showing with 41 additions and 4 deletions.
  1. +2 −1 bin/orange
  2. +12 −3 lib/orange.rb
  3. +27 −0 lib/orange/generator.rb
View
3 bin/orange
@@ -18,9 +18,10 @@ if $h
end
file = ARGV.first
-abort "Usage: orange [-hi] [-c=file.o] file.or" unless file
+abort "Usage: orange [-hiO] [-c=file.o] file.or" unless file
g = Orange.compile(File.read(file))
+g.optimize unless $O
case
when $i: puts g.inspect
View
15 lib/orange.rb
@@ -13,9 +13,18 @@
end
module Orange
+ class ParserError < RuntimeError; end
+
def self.compile(code)
- g = Orange::Generator.new
- OrangeParser.new.parse(code).compile(g)
- g
+ generator = Orange::Generator.new
+ parser = OrangeParser.new
+
+ if node = parser.parse(code)
+ node.compile(generator)
+ else
+ raise ParserError, parser.failure_reason
+ end
+
+ generator
end
end
View
27 lib/orange/generator.rb
@@ -8,6 +8,33 @@ class Generator
PCHAR = Type.pointer(Type::Int8Ty)
INT = Type::Int32Ty
+ class Value
+ STRUCT = LLVM::Type.struct([INT, INT, PCHAR])
+ TYPES = [:int, :string, :ptr]
+ attr_reader :type, :value, :ptr
+
+ def initialize(value, type)
+ @value = value
+ @type = type
+ end
+
+ def alloc(b)
+ struct = b.alloca(STRUCT, 0)
+ v = b.struct_gep(struct, 0)
+ b.store(TYPES.index(@type).llvm, v)
+ v = case @type
+ when :int: b.struct_gep(struct, 1)
+ when :string: b.struct_gep(struct, 2)
+ end
+ b.store(@value, v)
+ @ptr = struct
+ end
+
+ def load_str(b)
+
+ end
+ end
+
def initialize(mod = LLVM::Module.new("orange"), function=nil)
@module = mod
@locals = {}

0 comments on commit fa1f10e

Please sign in to comment.