Skip to content
Snippets Groups Projects
Commit 24236a7e authored by oscja033's avatar oscja033
Browse files

...

parent bde35858
No related branches found
No related tags found
No related merge requests found
......@@ -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
"""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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment