diff --git a/julia/dowhile.jl b/julia/dowhile.jl index 96d3fb56ba8f599ca9e75f5399d4c2db2a3bf61f..f969d71c12cd2e95d1e42772cc47564ca6b731f0 100644 --- a/julia/dowhile.jl +++ b/julia/dowhile.jl @@ -3,8 +3,25 @@ macro doWhile(block, cond) println("cond ", cond) println("block ", block) res = quote - error("TODO: Your code here") + while !($cond) + $block + end end - println(res) # Should not print any lines referencing dowhile.jl - res + +res=trav(res, __source__) +println(res) # Should not print any lines referencing dowhile.jl + +function trav(expr, sub) +count = 1 + for i in expr.args + if isa(i, Expr) + trav(i, sub) + + elseif isa(i, LineNumberNode) && i.file != sub.file + expr.args[count] = sub + + end + count++ + end + return expr end diff --git a/julia/run.jl b/julia/run.jl index 3728a8fd904b525f62928f11252efcc8ea4eca79..4d00c6df5d353a61fa3c025a78a37b643633a91b 100644 --- a/julia/run.jl +++ b/julia/run.jl @@ -1,9 +1,10 @@ """Template programming is a sort of metaprogramming where you fill in the blanks, but most parts of the code are very similar.""" -for (name,op) in [(:add, :+), (:sub, :-), (:mul, :*)] +for (name, op) in [(:add, :+), (:sub, :-), (:mul, :*)] # Create a function that is named for example add and returns the # sum of its two arguments + @eval $(name)(x,y) = $(Symbol(op))(x,y) end @assert 3 == add(1, 2) @@ -19,6 +20,9 @@ module Problem1 f(a::Any) = 2 end # TODO: Delete the method f(a::Int) so that f(1) returns the value 2 instead of 1 + +Base.delete_method(which(Problem1.f,(Int,))) + @assert 2 == Problem1.f(1) # TODO: Make an until block for Julia, similar to do {} while (cond); in C @@ -26,6 +30,7 @@ end # Make sure that any assertions and error-messages refer to lines in the original code. # Hint: You can access a hidden input __source__ in a macro and use that to replace source information. # By having the macro in a separate file, you can check source locations that match dowhile.jl and replace them with __source__. + include("dowhile.jl") function testDoWhile() local a = 5 @@ -33,6 +38,6 @@ function testDoWhile() @assert a < 18 a += 1 println(a) - end a==100 + end a==18 end testDoWhile()