What I have done is the turtle clearing a full layer without issues. It also takes care of mobs and gravel/sand. I'm just having issues with the end bit of code, making it go down a layer and start again…and when it's all done, coming back to the surface. I've tried a few things to get it to work, but I just can't figure it out. it never works like it should. Ignore the bit about refueling, I haven't implemented that yet. I added comments to my code to explain each function. Any help would be appreciated.
tArgs = {...}
if #tArgs ~= 1 then
 print("Usage: Quarry <size>")
end
print("To Use: ")
print("Place enderchest in slot 16.")
print("Place fuel in slot 15 or fuel beforehand.")
x = tArgs[1]
depth = 0
k = 0
--functions
function forward() -- Going forward, gravel and mob avoidance.
 while not turtle.forward() do
   turtle.dig()  
   turtle.attack()
 end
end 
function dig()     --To dig a full line.
 turtle.select(1)
 for i = 1,x-1 do
  while not turtle.forward() do
   turtle.dig()  
   turtle.attack()
   chest() 
  end
 end
end
function left()  --Turn left to start a new line. (even lines)
 turtle.turnLeft()
 forward()
 turtle.turnLeft()
end
function right()  --Turn right to start a new line. (odd lines)
 turtle.turnRight()
 forward()
 turtle.turnRight()
end
function spin()  --Spins (just a neater look to my code having a function)
 turtle.turnRight()
 turtle.turnRight()
end
function back()  --To take the turtle back to the starting square of the layer.
 spin()
 for i = 1,x-1 do
  forward()
 end
 turtle.turnRight()
 for i = 1,x-1 do
  forward()
 end
 turtle.turnRight()
end 
function layer()  --Digs one layer of land.
 for i = 1,x do
  dig()
  if i%x == 0 then
   if i%2 == 0 then
    turtle.turnRight()
    for i = 1,x-1 do
     forward()
    end
    turtle.turnRight()
   else
    back()
   end
  elseif i%2 == 1 then
   right()
  else
   left()
  end
 end
end
function chest()  --Empties inventory when full.
 if turtle.getItemCount(14) > 0 then
  turtle.digUp()
  turtle.select(16)
  turtle.placeUp()
  for i = 1,14 do
   turtle.select(i)
   turtle.dropUp()
  end
  turtle.select(16)
  turtle.digUp()
  turtle.select(1)
 end
end
function quarry()  --Needs to move the turtle down to start a new layer and up when done(WIP)
 depth = depth + 1
 if not turtle.down() then
  turtle.digDown()
  turtle.down()
  if not turtle.digDown() then
   for i = 1, depth do
    turtle.up()
    k = k + 1
   end
  end
 end
end
*Edit* Fixed some of the spacing in the code, so it looked better. The indenting messed up while copying.
*Edit* Realized the code I pasted wasn't the most updated. I'm having the same issues, but it's my current code now.