Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a2ff2039df41
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c1fe6da9ee8c
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Jul 29, 2016

  1. Macros: use MemoryIO for building, because of {{debug()}}

    Ary Borenszweig committed Jul 29, 2016
    Copy the full SHA
    917ba96 View commit details
  2. Compiler: surround {{yield}} with begin/end

    Ary Borenszweig committed Jul 29, 2016
    1
    Copy the full SHA
    7afbc2f View commit details
  3. Revert "Object: use begin/end in getter/property macros that receive …

    …a block"
    
    This reverts commit 72662fe.
    
    This solution is not needed anymore, and the current compiler (0.18.7) works find without it.
    Ary Borenszweig committed Jul 29, 2016
    Copy the full SHA
    93edf52 View commit details
  4. Formatter: correctly indent unions after newline

    Ary Borenszweig committed Jul 29, 2016
    1
    Copy the full SHA
    c1fe6da View commit details
Showing with 33 additions and 16 deletions.
  1. +15 −0 spec/compiler/codegen/macro_spec.cr
  2. +2 −0 spec/compiler/formatter/formatter_spec.cr
  3. +5 −2 src/compiler/crystal/macros/interpreter.cr
  4. +7 −2 src/compiler/crystal/tools/formatter.cr
  5. +4 −12 src/object.cr
15 changes: 15 additions & 0 deletions spec/compiler/codegen/macro_spec.cr
Original file line number Diff line number Diff line change
@@ -1411,4 +1411,19 @@ describe "Code gen: macro" do
bar("hi")
)).to_string.should eq("hi")
end

it "surrounds {{yield}} with begin/end" do
run(%(
macro foo
a = {{yield}}
end
a = 0
foo do
1
2
end
a
)).to_i.should eq(2)
end
end
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -967,4 +967,6 @@ describe Crystal::Formatter do

assert_format %(puts(<<-FOO\n1\nFOO, 2))
assert_format %(puts <<-FOO\n1\nFOO, 2)

assert_format "x : Int32 |\nString", "x : Int32 |\n String"
end
7 changes: 5 additions & 2 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ module Crystal
def initialize(@program : Program,
@scope : Type, @path_lookup : Type, @location : Location?,
@vars = {} of String => ASTNode, @block : Block? = nil)
@str = String::Builder.new(512)
@str = MemoryIO.new(512) # Can't be String::Builder because of `{{debug()}}
@last = Nop.new
end

@@ -101,8 +101,11 @@ module Crystal
# In the caseof {{yield}}, we want to paste the block's body
# retaining the original node's location, so error messages
# are shown in the block instead of in the generated macro source
emit_loc_pragma = node.exp.is_a?(Yield) && !@last.is_a?(Nop)
is_yield = node.exp.is_a?(Yield) && !@last.is_a?(Nop)
@str << " begin " if is_yield
emit_loc_pragma = is_yield
@last.to_s(@str, emit_loc_pragma: emit_loc_pragma)
@str << " end " if is_yield
end

false
9 changes: 7 additions & 2 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
@@ -1040,6 +1040,7 @@ module Crystal
check_open_paren

paren_count = @paren_count
column = @column

node.types.each_with_index do |type, i|
if @token.type == :"?"
@@ -1059,8 +1060,12 @@ module Crystal
case @token.type
when :"|"
write " | "
next_token
skip_space_or_newline
next_token_skip_space
if @token.type == :NEWLINE
write_line
write_indent(column)
next_token_skip_space_or_newline
end
when :")"
if @paren_count > 0
@paren_count -= 1
16 changes: 4 additions & 12 deletions src/object.cr
Original file line number Diff line number Diff line change
@@ -283,15 +283,11 @@ class Object
@{{name.var.id}} : {{name.type}}?

def {{name.var.id}}
@{{name.var.id}} ||= begin
{{yield}}
end
@{{name.var.id}} ||= {{yield}}
end
{% else %}
def {{name.id}}
@{{name.id}} ||= begin
{{yield}}
end
@{{name.id}} ||= {{yield}}
end
{% end %}
{% else %}
@@ -745,15 +741,11 @@ class Object
@{{name.var.id}} : {{name.type}}?

def {{name.var.id}}
@{{name.var.id}} ||= begin
{{yield}}
end
@{{name.var.id}} ||= {{yield}}
end
{% else %}
def {{name.id}}
@{{name.id}} ||= begin
{{yield}}
end
@{{name.id}} ||= {{yield}}
end
{% end %}
{% else %}