Skip to content

Commit

Permalink
Showing 4 changed files with 28 additions and 18 deletions.
27 changes: 17 additions & 10 deletions src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ module Crystal
# one LLVM module is created for each type in a program.
property? single_module = false

# If `true`, prints time and memory stats to STDOUT.
# If `true`, prints time and memory stats to `stdout`.
property? stats = false

# Target triple to use in the compilation.
@@ -100,6 +100,12 @@ module Crystal
# to `false` that cleanup is not performed.
property? cleanup = true

# Default standard output to use in a compilation.
property stdout : IO = STDOUT

# Default standard error to use in a compilation.
property stderr : IO = STDERR

# Compiles the given *source*, with *output_filename* as the name
# of the generated executable.
#
@@ -143,6 +149,7 @@ module Crystal
program.flags.merge @flags
program.wants_doc = wants_doc?
program.color = color?
program.stdout = stdout
program
end

@@ -170,9 +177,9 @@ module Crystal
parser.wants_doc = wants_doc?
parser.parse
rescue ex : InvalidByteSequenceError
print colorize("Error: ").red.bold
print colorize("file '#{Crystal.relative_filename(source.filename)}' is not a valid Crystal source file: ").bold
puts "#{ex.message}"
stdout.print colorize("Error: ").red.bold
stdout.print colorize("file '#{Crystal.relative_filename(source.filename)}' is not a valid Crystal source file: ").bold
stdout.puts "#{ex.message}"
exit 1
end

@@ -227,7 +234,7 @@ module Crystal

target_machine.emit_obj_to_file llvm_mod, object_name

puts "#{CC} #{object_name} -o #{output_filename} #{@link_flags} #{lib_flags}"
stdout.puts "#{CC} #{object_name} -o #{output_filename} #{@link_flags} #{lib_flags}"
end

private def codegen(program, units : Array(CompilationUnit), lib_flags, output_filename, output_dir)
@@ -312,9 +319,9 @@ module Crystal
TargetMachine.create(triple, @mcpu || "", @release)
end
rescue ex : ArgumentError
print colorize("Error: ").red.bold
print "llc: "
puts "#{ex.message}"
stdout.print colorize("Error: ").red.bold
stdout.print "llc: "
stdout.puts "#{ex.message}"
exit 1
end

@@ -353,7 +360,7 @@ module Crystal
end

private def system(command, args = nil)
puts "#{command} #{args.join " "}" if verbose?
stdout.puts "#{command} #{args.join " "}" if verbose?

::system(command, args)
unless $?.success?
@@ -364,7 +371,7 @@ module Crystal
end

private def error(msg, exit_code = 1)
Crystal.error msg, @color, exit_code
Crystal.error msg, @color, exit_code, stderr: stderr
end

private def colorize(obj)
10 changes: 5 additions & 5 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
@@ -42,12 +42,12 @@ module Crystal

if format
begin
puts Crystal::Formatter.format(@str.to_s)
@program.stdout.puts Crystal::Formatter.format(@str.to_s)
rescue
puts @str
@program.stdout.puts @str
end
else
puts @str
@program.stdout.puts @str
end

@last = Nop.new
@@ -77,7 +77,7 @@ module Crystal
def interpret_puts(node)
node.args.each do |arg|
arg.accept self
puts @last
@program.stdout.puts @last
end

@last = Nop.new
@@ -88,7 +88,7 @@ module Crystal
arg.accept self
print arg
print " = "
puts @last
@program.stdout.puts @last
end

@last = Nop.new
3 changes: 3 additions & 0 deletions src/compiler/crystal/program.cr
Original file line number Diff line number Diff line change
@@ -102,6 +102,9 @@ module Crystal
# The constant for ARGV_UNSAFE
getter! argv : Const

# Default standard output to use in a program, while compiling.
property stdout : IO = STDOUT

def initialize
super(self, self, "main")

6 changes: 3 additions & 3 deletions src/compiler/crystal/util.cr
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ module Crystal
filename
end

def self.error(msg, color, exit_code = 1)
STDERR.print "Error: ".colorize.toggle(color).red.bold
STDERR.puts msg.colorize.toggle(color).bright
def self.error(msg, color, exit_code = 1, stderr = STDERR)
stderr.print "Error: ".colorize.toggle(color).red.bold
stderr.puts msg.colorize.toggle(color).bright
exit(exit_code) if exit_code
end

0 comments on commit e86e4b2

Please sign in to comment.