Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions spec/compiler/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ require "./spec_helper"
describe Crystal::Config do
it ".host_target" do
{% begin %}
# TODO: CRYSTAL_SPEC_COMPILER_BIN must be quoted (#11456)
{% compiler = (env("CRYSTAL_SPEC_COMPILER_BIN") || "bin/crystal").id %}
Crystal::Config.host_target.should eq Crystal::Codegen::Target.new({{ `#{compiler} --version`.lines[-1] }}.lchop("Default target: "))
{% host_triple = Crystal.constant("HOST_TRIPLE") || Crystal::DESCRIPTION.lines[-1].gsub(/^Default target: /, "") %}
Crystal::Config.host_target.should eq Crystal::Codegen::Target.new({{ host_triple }})
{% end %}
end

Expand Down
5 changes: 5 additions & 0 deletions spec/std/char/reader_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,18 @@ describe "Char::Reader" do
reader.pos.should eq(0)
reader.current_char.ord.should eq(0)
reader.has_previous?.should be_false
reader.has_next?.should be_false
end

it "gets previous char (ascii)" do
reader = Char::Reader.new(at_end: "hello")
reader.pos.should eq(4)
reader.current_char.should eq('o')
reader.has_previous?.should be_true
reader.has_next?.should be_true

reader.previous_char.should eq('l')
reader.has_next?.should be_true
reader.previous_char.should eq('l')
reader.previous_char.should eq('e')
reader.previous_char.should eq('h')
Expand All @@ -126,8 +129,10 @@ describe "Char::Reader" do
reader.pos.should eq(9)
reader.current_char.should eq('語')
reader.has_previous?.should be_true
reader.has_next?.should be_true

reader.previous_char.should eq('本')
reader.has_next?.should be_true
reader.previous_char.should eq('日')
reader.previous_char.should eq('á')
reader.previous_char.should eq('h')
Expand Down
30 changes: 12 additions & 18 deletions src/char/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct Char
@pos = pos.to_i
@current_char = '\0'
@current_char_width = 0
@end = false
decode_current_char
end

Expand All @@ -69,7 +68,6 @@ struct Char
@pos = @string.bytesize
@current_char = '\0'
@current_char_width = 0
@end = false
decode_previous_char
end

Expand All @@ -83,7 +81,7 @@ struct Char
# reader.peek_next_char # => '\0'
# ```
def has_next? : Bool
!@end
@pos < @string.bytesize
end

# Reads the next character in the string,
Expand Down Expand Up @@ -177,7 +175,7 @@ struct Char
# C
# ```
def each(&) : Nil
while @pos < @string.bytesize
while has_next?
yield current_char

@pos += @current_char_width
Expand Down Expand Up @@ -251,26 +249,22 @@ struct Char
private def decode_current_char
decode_char_at(@pos) do |code_point, width, error|
@current_char_width = width
@end = @pos == @string.bytesize
@error = error
@current_char = code_point.unsafe_chr
end
end

private def decode_previous_char
if @pos == 0
@end = @pos == @string.bytesize
else
while @pos > 0
@pos -= 1
break if (byte_at(@pos) & 0xC0) != 0x80
end
decode_char_at(@pos) do |code_point, width, error|
@current_char_width = width
@end = @pos == @string.bytesize
@error = error
@current_char = code_point.unsafe_chr
end
return if @pos == 0

while @pos > 0
@pos -= 1
break if (byte_at(@pos) & 0xC0) != 0x80
end
decode_char_at(@pos) do |code_point, width, error|
@current_char_width = width
@error = error
@current_char = code_point.unsafe_chr
end
end

Expand Down
9 changes: 8 additions & 1 deletion src/compiler/crystal/program.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module Crystal
# A `ProgressTracker` object which tracks compilation progress.
property progress_tracker = ProgressTracker.new

property codegen_target = Config.host_target
getter codegen_target = Config.host_target

getter predefined_constants = Array(Const).new

Expand Down Expand Up @@ -289,6 +289,8 @@ module Crystal
define_crystal_string_constant "LIBRARY_RPATH", Crystal::CrystalLibraryPath.default_rpath
define_crystal_string_constant "VERSION", Crystal::Config.version
define_crystal_string_constant "LLVM_VERSION", Crystal::Config.llvm_version
define_crystal_string_constant "HOST_TRIPLE", Crystal::Config.host_target.to_s
define_crystal_string_constant "TARGET_TRIPLE", Crystal::Config.host_target.to_s
end

private def define_crystal_string_constant(name, value)
Expand All @@ -307,6 +309,11 @@ module Crystal

property(target_machine : LLVM::TargetMachine) { codegen_target.to_target_machine }

def codegen_target=(@codegen_target : Codegen::Target) : Codegen::Target
crystal.types["TARGET_TRIPLE"].as(Const).value.as(StringLiteral).value = codegen_target.to_s
@codegen_target
end

# Returns the `Type` for `Array(type)`
def array_of(type)
array.instantiate [type] of TypeVar
Expand Down